SecureUtil.aes数据加密工具类
数据加密、解密工具类
包含map和vo的数据转换
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import java.util.HashMap;
import java.util.Map;
/**
* 数据解析
*
*/
public class ParamUtils {
/**
* 数据解密
*
* @param params 参数
* @param secretKey key
* @return 结果
*/
public static BatchApprovalParamVO dataDecrypt(String params, String secretKey) {
// AES密⽂解密 params = batchNo=HJ-PLSP-202412-00044&approvalType=02&userId=114&tenantId=2
String decrypt = SecureUtil.aes(secretKey.getBytes()).decryptStr(params);
Map<String, Object> paramsMap = new HashMap<>();
String[] pairs = decrypt.split(LOGICAL);
for (String pair : pairs) {
String[] keyValue = pair.split(EQUAL, 2);
if (keyValue.length > 1) {
paramsMap.put(keyValue[0], keyValue[1]);
} else {
paramsMap.put(keyValue[0], "");
}
}
return BeanUtil.mapToBean(paramsMap, BatchApprovalParamVO.class, true);
}
/**
* 数据加密
*
* @param vo 数据
* @param secretKey key
* @return 结果
*/
public static String dataEncrypt(BatchApprovalParamVO vo, String secretKey) {
// AES密⽂加密 params = batchNo=HJ-PLSP-202412-00044&approvalType=02&userId=114&tenantId=2
Map<String, Object> map = BeanUtil.beanToMap(vo);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (sb.length() > 0) {
sb.append(LOGICAL);
}
sb.append(entry.getKey()).append(EQUAL).append(entry.getValue());
}
return SecureUtil.aes(secretKey.getBytes()).encryptHex(sb.toString());
}
}
实体类VO:
@Data
public class BatchApprovalParamVO {
@Schema(description = "审批批次号")
private String batchNo;
@Schema(description = "审批类型:通过、驳回")
private String approvalType;
@Schema(description = "审批人id")
private Long userId;
@Schema(description = "租户id")
private Long tenantId;
}