1 changed files with 53 additions and 0 deletions
@ -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); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue