自学内容网 自学内容网

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;

}

原文地址:https://blog.csdn.net/qq_42547733/article/details/145283987

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!