解密returns 空结果 - nodejs crypto
Decryption returns empty result - nodejs crypto
function encrypt() {
const iv = '3af545da025d5b07319cd9b2571670ca'
, payload = '01000000000000000000000000000000'
, key = 'c1602e4b57602e48d9a3ffc1b578d9a3';
const cipher = crypto.createCipheriv('aes128', new Buffer(key, 'hex'), new Buffer(iv, 'hex'));
const encryptedPayload = cipher.update(new Buffer(payload, 'hex'));
let encryptedPayloadHex = encryptedPayload.toString('hex');
console.log(encryptedPayloadHex); // returns 'ae47475617f38b4731e8096afa5a59b0'
};
function decrypt() {
const iv = '3af545da025d5b07319cd9b2571670ca'
, key = 'c1602e4b57602e48d9a3ffc1b578d9a3'
, payload = 'ae47475617f38b4731e8096afa5a59b0';
const decipher = crypto.createDecipheriv('aes128', new Buffer(key, 'hex'), new Buffer(iv, 'hex'));
const decryptedPayload = decipher.update(new Buffer(payload, 'hex'), 'hex', 'hex');
console.log(decryptedPayload); // returns empty string
// decipher.update(new Buffer(payload, 'hex')) // returns empty buffer
const decryptedPayloadHex = decipher.final('hex'); // returns 'EVP_DecryptFinal_ex:bad decrypt' error
// console.log(decryptedPayloadHex);
};
但是解密结果总是空的。
nodejs 文档声明 update
returns 在给定编码中作为字符串的值(如果提供),否则作为缓冲区。尽管如此,我也尝试使用 final
,但没有成功。
P.S。事实上,我从外部来源收到 encryptedPayload
值和 iv
(它们不是我加密和生成的),但我决定测试加密(我有普通的 payload
值)和我的加密 returns 与我从外部接收的结果相同。
好的,原来是填充问题。我得到了灵感 from here. 我只是添加了
decipher.setAutoPadding(false);
在创建 decipher
对象之后。
虽然这很奇怪,因为当使用一种语言进行加密并使用另一种语言进行解密时可能会出现填充问题,但当使用同一种语言进行加密和解密时则不会出现填充问题(正如我在此处进行的测试)。 .. 如果有人对填充问题有意见 - 请添加它们,以便未来的观众(以及我)获得知识。
function encrypt() {
const iv = '3af545da025d5b07319cd9b2571670ca'
, payload = '01000000000000000000000000000000'
, key = 'c1602e4b57602e48d9a3ffc1b578d9a3';
const cipher = crypto.createCipheriv('aes128', new Buffer(key, 'hex'), new Buffer(iv, 'hex'));
const encryptedPayload = cipher.update(new Buffer(payload, 'hex'));
let encryptedPayloadHex = encryptedPayload.toString('hex');
console.log(encryptedPayloadHex); // returns 'ae47475617f38b4731e8096afa5a59b0'
};
function decrypt() {
const iv = '3af545da025d5b07319cd9b2571670ca'
, key = 'c1602e4b57602e48d9a3ffc1b578d9a3'
, payload = 'ae47475617f38b4731e8096afa5a59b0';
const decipher = crypto.createDecipheriv('aes128', new Buffer(key, 'hex'), new Buffer(iv, 'hex'));
const decryptedPayload = decipher.update(new Buffer(payload, 'hex'), 'hex', 'hex');
console.log(decryptedPayload); // returns empty string
// decipher.update(new Buffer(payload, 'hex')) // returns empty buffer
const decryptedPayloadHex = decipher.final('hex'); // returns 'EVP_DecryptFinal_ex:bad decrypt' error
// console.log(decryptedPayloadHex);
};
但是解密结果总是空的。
nodejs 文档声明 update
returns 在给定编码中作为字符串的值(如果提供),否则作为缓冲区。尽管如此,我也尝试使用 final
,但没有成功。
P.S。事实上,我从外部来源收到 encryptedPayload
值和 iv
(它们不是我加密和生成的),但我决定测试加密(我有普通的 payload
值)和我的加密 returns 与我从外部接收的结果相同。
好的,原来是填充问题。我得到了灵感 from here. 我只是添加了
decipher.setAutoPadding(false);
在创建 decipher
对象之后。
虽然这很奇怪,因为当使用一种语言进行加密并使用另一种语言进行解密时可能会出现填充问题,但当使用同一种语言进行加密和解密时则不会出现填充问题(正如我在此处进行的测试)。 .. 如果有人对填充问题有意见 - 请添加它们,以便未来的观众(以及我)获得知识。