【发布时间】:2019-11-04 06:26:55
【问题描述】:
我有一个 nodejs 服务,它对一些需要在 Python 中解密的数据使用 AES 加密。 无论我做什么,我都无法让它发挥作用。 NodeJS 代码:
const algorithm = 'aes-128-ctr';
function encryptScript(data, key) {
const cipher = crypto.createCipher(algorithm, key);
let crypted = cipher.update(data, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
我已经在 Python 中尝试过:
counter = Counter.new(128)
cipher = AES.new(key, AES.MODE_CTR, counter=counter)
print cipher.decrypt(enc.decode("hex"))
但它不起作用。
我的第一个问题是 Python 代码不接受长度超过 32 字节的密钥(Nodejs 代码可以)。
如果我使用 NodeJS 加密模块,则解密工作正常:
function decryptScript(data, key) {
const decipher = crypto.createDecipher(algorithm, key);
let dec = decipher.update(data, 'hex', 'utf8');
dec += decipher.final('utf8');
return dec;
}
我不知道节点在做什么,但它可能与数据的一些填充有关。
我怎样才能让它工作?
(我更喜欢不需要更改 NodeJS 代码而只需要更改 Python 脚本的解决方案)。
【问题讨论】:
标签: python node.js encryption aes