充电桩互联互通 AES 参数加解密(Hutool实现)

充电桩互联互通 AES 参数加解密(Hutool实现)

abdulla1992
2023-12-07 / 0 评论 / 104 阅读 / 正在检测是否收录...

PileSignUtils 充电桩互联互通 AES 参数加解密

import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import cn.hutool.crypto.symmetric.AES;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class PileSignUtils {
    private static Logger logger = LoggerFactory.getLogger(PileSignUtils.class);
    /**
     * aes 加密
     * @param params 待加密参数
     * @param dataSecret 密钥
     * @param dataSecretIV 密钥向量
     * @return 解密结果
     */
    public static String signParams(String params,String dataSecret,String dataSecretIV){
        AES aes = new AES("CBC","PKCS5Padding",dataSecret.getBytes(),dataSecretIV.getBytes());
        return aes.encryptBase64(params);
    }
    /**
     * aes 加密
     * @param params 待加密参数
     * @param dataSecret 密钥
     * @param dataSecretIV 密钥向量
     * @return 解密结果
     */
    public static String decryptParams(String params,String dataSecret,String dataSecretIV){
        AES aes = new AES("CBC","PKCS5Padding",dataSecret.getBytes(),dataSecretIV.getBytes());
        return aes.decryptStr(params);
    }

    public static String getSign(String data,String signSecret,String platformId){
        long timeStamp = new Date().getTime();
        int nextSequence = SequenceGenerator.getNextSequence();
        int len  = 64-signSecret.length();
        if (signSecret.length()<64){
            for (int i = 0; i < len; i++) {
               signSecret = signSecret + "0";
            }
        }
        logger.info(signSecret);
        byte[] key = signSecret.getBytes();
        HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
        String str =  mac.digestHex(data);
        return str;
    }
0

评论 (0)

取消