简体   繁体   English

如何在nodejs中用aes-128-cbc算法解密php加密的字符串?

[英]How to decrypt string in nodejs that encrypted in php with aes-128-cbc algorithm?

I need to encrypt string, send http request with this encrypted string and then decrypt it in nodejs server.我需要加密字符串,用这个加密的字符串发送 http 请求,然后在 nodejs 服务器中解密它。

Php side: Php 方:

$var  = openssl_encrypt('string', "aes-128-cbc", 'stringstringstri');

Nodejs side:节点端:

let decipher = crypto.createDecipher('aes-128-cbc', 'stringstringstri');
let decrypted = decipher.update(encrypted, 'utf8', 'utf8') + decipher.final('utf8');

also tried也试过

const initVector = crypto.randomBytes(32);
const decipher = crypto.createDecipheriv('aes-128-cbc', 'stringstringstri', initVector)
let decryptedData = decipher.update(encrypted, 'utf8', 'utf-8')
decryptedData += decipher.final('utf-8');

and recieved error: wrong final block length or this[kHandle].initiv(cipher, credential, iv, authTagLength); TypeError: Invalid initialization vector并收到错误: wrong final block lengththis[kHandle].initiv(cipher, credential, iv, authTagLength); TypeError: Invalid initialization vector this[kHandle].initiv(cipher, credential, iv, authTagLength); TypeError: Invalid initialization vector

Since you're not passing any iv in PHP, it used the default iv which is an empty string.由于您没有在 PHP 中传递任何iv ,因此它使用默认的iv ,它是一个空字符串。 Hence, you need to consider this in Node.js as well.因此,您也需要在 Node.js 中考虑这一点。

So the changes would be like this:所以变化是这样的:

const key = 'string';
const cipher = crypto.createDecipheriv('aes-128-cbc', key, Buffer.alloc(16));
const cipherEncrypted = Buffer.from(encrypted, 'base64');
const decrypted = Buffer.concat([cipher.update(cipherEncrypted), cipher.final()]);
console.log(decrypted.toString());

Could you implement something like this post suggest?你能实施像这篇文章建议的东西吗? https://ashish-dhodare.medium.com/implementing-aes-cbc-encryption-in-node.js-java-and-c-cross-language-encryption-42d1844119b9 https://ashish-dhodare.medium.com/implementing-aes-cbc-encryption-in-node.js-java-and-c-cross-language-encryption-42d1844119b9


function encrypt(plainString, AesKey, AesIV) {
    const cipher = crypto.createCipheriv("aes-128-cbc", AesKey, AesIV);
    let encrypted = Buffer.concat([cipher.update(Buffer.from(plainString, "utf8")), cipher.final()]);
    return encrypted.toString("base64");
}

function decrypt(base64String, AesKey, AesIV) {
    const decipher = crypto.createDecipheriv("aes-128-cbc", AesKey, AesIV);
    const deciphered = Buffer.concat([decipher.update(Buffer.from(base64String, "base64")), decipher.final()]);
    return deciphered.toString("utf8");
}

// const key = crypto.randomBytes(32); //Need 32 bytes (256 bits) key as we are using AES-256 encryption
const key = Buffer.from("J/PYjc1ftDFK5+77U1PB80v2TamokGap5yCIP2YI6tQ=", "base64");
// const iv = crypto.randomBytes(16); //Need 16 bytes (128 bits) Initialization vector as default block size is 128 bits
const iv = Buffer.from("gaOr3uvhZEwFeSbRHwlHcg==", "base64");

// Its better to pass iv and key in bytes/buffer
var encryptedData = encrypt("some data to encrypt", key, iv);
console.log(encryptedData);
// Need same key and iv for decryption otherwise it won't work
var decryptedData = decrypt(encryptedData, key, iv)
console.log(decryptedData);```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM