From ab56f755de3376d632b6ec82d39da93f5bac4100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=85=B4=E7=90=B3?= <1911390090@qq.com> Date: Thu, 9 May 2024 17:35:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E8=B4=B9=E8=BF=90=E8=90=A5=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AESEncryptionController.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 zc-business/src/main/java/com/zc/business/controller/AESEncryptionController.java diff --git a/zc-business/src/main/java/com/zc/business/controller/AESEncryptionController.java b/zc-business/src/main/java/com/zc/business/controller/AESEncryptionController.java new file mode 100644 index 00000000..9d2cf0b0 --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/controller/AESEncryptionController.java @@ -0,0 +1,53 @@ +package com.zc.business.controller; + +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.utils.SecurityUtils; +import com.ruoyi.system.service.ISysConfigService; +import io.swagger.annotations.Api; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +/** + * + */ +@Api(tags = "收费运营接口") +@RestController +@RequestMapping("/AESEncryption") +public class AESEncryptionController extends BaseController { + @Autowired + private ISysConfigService configService; + + @GetMapping() + public AjaxResult AESEncryption() throws Exception{ + String secretKey= configService.selectConfigByKey("AESKey");//密钥 + String iv= configService.selectConfigByKey("AESIv");// 偏移量(初始化向量) + String plainText = SecurityUtils.getLoginUser().getUser().getRoles().get(0).getRoleKey(); // 要加密的文本 + // 转换密钥和IV为字节数组 + byte[] keyBytes = secretKey.getBytes(StandardCharsets.UTF_8); + byte[] ivBytes = iv.getBytes(StandardCharsets.UTF_8); + // 创建密钥规格 + SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); + // 创建初始化向量规格 + IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); + // 创建并初始化Cipher对象,指定为AES/CBC/PKCS5Padding模式 + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); + // 将要加密的文本转换为字节数组 + byte[] plainBytes = plainText.getBytes(StandardCharsets.UTF_8); + // 执行加密操作 + byte[] encryptedBytes = cipher.doFinal(plainBytes); + // 将加密后的字节数组使用Base64编码,并输出为字符串 + String encryptedBase64 = Base64.getEncoder().encodeToString(encryptedBytes); + String url=configService.selectConfigByKey("AESEncryption")+encryptedBase64; + return AjaxResult.success(url); + } +}