package com.zc.business.controller; import cn.hutool.json.JSONArray; import com.google.gson.Gson; 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 com.zc.business.enums.UniversalEnum; 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.Arrays; 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 chargeOperationAuthority= configService.selectConfigByKey("chargeOperationAuthority");// 收费运营权限 // 创建Gson对象 Gson gson = new Gson(); String plainText=""; // 将JSON字符串转换为Integer类型的数组 String[] numberArray = gson.fromJson(chargeOperationAuthority, String[].class); if (SecurityUtils.getUserId()==UniversalEnum.ONE.getNumber()){ plainText="100"; }else { int size = SecurityUtils.getLoginUser().getUser().getRoles().size(); for (int i = 0; i < size; i++) { String roleKey = SecurityUtils.getLoginUser().getUser().getRoles().get(i).getRoleKey(); // 检查数组中是否包含 if (Arrays.stream(numberArray).anyMatch(num -> num.equals(roleKey))) { plainText=roleKey; break; } } } // String plainText = SecurityUtils.getLoginUser().getUser().getRoles().get(UniversalEnum.ZERO.getNumber()).getRoleKey(); // 要加密的文本 // 转换密钥和IV为字节数组 byte[] keyBytes = secretKey.getBytes(StandardCharsets.UTF_8); byte[] ivBytes = iv.getBytes(StandardCharsets.UTF_8); // 创建密钥规格 SecretKeySpec keySpec = new SecretKeySpec(keyBytes, UniversalEnum.AES.getValue()); // 创建初始化向量规格 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); } }