nodejs java aes-128-ecb

nodejs代码
const buf = Buffer.from(‘123456’, ‘hex’);
hex - 将每个字节编码为两个十六进制字符。

export function aesDecrypt(hexStr: string, hexKey: string) {    	
    const buffer = new Buffer(hexStr, "hex");    const keyBuf = new Buffer(hexKey, "hex");
    const aes = crypto.createDecipheriv('aes-128-ecb', keyBuf, Buffer.alloc(0));    
    aes.setAutoPadding(true);    
    const result = aes.update(buffer).toString("hex") + aes.final("hex");    
    return result;
 }

java代码

private static final String ALGORITHM = "AES";

private static final String CIPHER_ALGORITHM_PADDING = "AES/ECB/PKCS5Padding";

public static String encrypt(String hexStr, String hexKey) {
    try {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_PADDING);
        SecretKeySpec skeySpec = new SecretKeySpec(HexByteUtil.hexToBytes(hexKey), ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encryptData = cipher.doFinal(HexByteUtil.hexToBytes(hexStr));
        return HexByteUtil.parseByte2HexStr(encryptData).toLowerCase();
    } catch (Exception ex) {
        log.info("encryptPKCS5 error encrypt data = {}", hexStr, ex);
        return "";
    }
}

HexByteUtil

public static byte[] hexToBytes(String hex) {
    byte[] buf = new byte[hex.length() / 2];
    for (int i = 0; i < hex.length() / 2; i++) {
        String sub = hex.substring(i * 2, i * 2 + 2);
        buf[i] = (byte) Integer.parseInt(sub, 16);
    }
    return buf;
}
public static int uint8Array(int num) {
    int j = num;
    if (j < 0) {
        j = num + Math.abs(num) / 256 * 256 + 256;
    }
    if (j > 255) {
        j = num - num / 256 * 256;
    }
    return j;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值