I'm trying to convert my symmetric AES-256-CBC encryption from PHP to NodeJS. But i think i'm not converting correctly the IV, and i tried different things, but not worked.
My PHP Code:
<?php
class Encryption{
const method = 'AES-256-CBC';
private static $passwordString;
private static $iv;
private static $password;
public static function encryptString($string){
self::setEncryptionPasswordString();
self::setEncryptionIV();
self::setEncryptionPassword();
$encrypted = openssl_encrypt($string, self::method, self::$password, OPENSSL_RAW_DATA, self::$iv);
$encrypted = base64_encode($encrypted);
$encrypted = str_replace(['+', '/', '='], ['-', '_', ''], $encrypted);
return $encrypted;
}
public static function decryptString($string){
self::setEncryptionPasswordString();
self::setEncryptionIV();
self::setEncryptionPassword();
$string = str_replace(['-', '_'], ['+', '/'], $string);
$string = base64_decode($string);
$decrypted = openssl_decrypt($string, self::method, self::$password, OPENSSL_RAW_DATA, self::$iv);
return $decrypted;
}
private static function setEncryptionPasswordString(){
self::$passwordString = getenv("ENC_PASS");
}
private static function setEncryptionIV(){
self::$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
}
private static function setEncryptionPassword(){
self::$password = substr(hash('sha256', self::$passwordString, true), 0, 32);
}
}
?>
My NodeJS code:
const crypto = require("crypto");
const method = "AES-256-CBC";
const iv = Buffer.alloc(16);
const password = "my_password";
function encrypt(string){
//let key = crypto.createHash("SHA-256").update(String(password)).digest("base64").substr(0, 32); Not Working
let key = "my_iv";
let cipher = crypto.createCipheriv(method, key.substr(0, 32), iv);
let encrypted = cipher.update(string);
let result = encrypted.toString("base64").replace(/[=]/g, "").replace(/[+]/g, "-").replace(/[\/]/g, "_");
return result;
}
function decrypt(string){
}
module.exports.encrypt = (string) => encrypt(string);
module.exports.decrypt = (string) => decrypt(string);
I think, Buffer.alloc(16) is not working as i expect, and i don't know how to change it. Can someone help-me please!