简体   繁体   中英

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.

Php side:

$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 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. Hence, you need to consider this in Node.js as well.

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


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);```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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