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
2
Answers
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
Since you’re not passing any
iv
in PHP, it used the defaultiv
which is an empty string. Hence, you need to consider this in Node.js as well.So the changes would be like this: