php hex2bin nodejs,python(pycrypto)和nodejs(crypto)之间的加密(aes128cbc)不匹配

我得到了这段python代码,需要将其翻译成nodejs。python代码使用加密中的pycrypto。在nodejs方面,我使用的是本机加密模块。加密字符串之间似乎存在不匹配。在from Crypto.Cipher import AES

from binascii import b2a_hex, a2b_hex

import json

raw_key = [0x58, 0x86, 0x17, 0x6d, 0x88, 0x7c, 0x9a, 0xa0, 0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x28, 0xa4, 0x5a]

key = str(bytearray(raw_key))

raw_iv = [0x34, 0x2e, 0x17, 0x99, 0x6d, 0x19, 0x3d, 0x28, 0xdd, 0xb3, 0xa2, 0x69, 0x5a, 0x2e, 0x6f, 0x1b]

iv = str(bytearray(raw_iv))

text = json.dumps({ "a": 1, "b": 2 })

cryptor = AES.new(key, AES.MODE_CBC, iv)

length = 16

count = len(text)

add = length - (count % length)

text = text + ('\0' * add)

encrypted = cryptor.encrypt(text);

print b2a_hex(encrypted)

上面的python代码输出

^{pr2}$

而下面的nodejs代码const crypto = require('crypto');

const KEY = Buffer.from([0x58, 0x86, 0x17, 0x6d, 0x88, 0x7c, 0x9a, 0xa0, 0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x28, 0xa4, 0x5a]);

const IV = Buffer.from([0x34, 0x2e, 0x17, 0x99, 0x6d, 0x19, 0x3d, 0x28, 0xdd, 0xb3, 0xa2, 0x69, 0x5a, 0x2e, 0x6f, 0x1b]);

const text = JSON.stringify({ a: 1, b: 2 });

const cipher = crypto.createCipheriv('aes-128-cbc', KEY, IV);

cipher.setAutoPadding(true);

const encrypted = Buffer.concat([cipher.update(text, 'utf-8'), cipher.final()]);

console.log(encrypted.toString('hex'));

输出d6a0dbc6df2a1038036e4db985f9ca10

为什么它们不匹配?我做错什么了吗?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值