so I have the following code on Node.js
var crypto = require('crypto');
function encrypt (key = "9055935C641A3CD243337FD149C793DF", data) {
var key = (key instanceof Buffer) ? key : new Buffer(key, 'hex');
var iv = crypto.randomBytes(16);
var cipher = crypto.createCipheriv( "aes-128-cbc", key, iv);
var result = Buffer.concat([iv, cipher.update(data), cipher.final()]);
return new Buffer( result ).toString('base64');
};
And in Laravel I have:
<?php
function encrypt($key = "9055935C641A3CD243337FD149C793DF", $data) {
$encrypter = new Encrypter($key, 'AES-128-CBC');
$dataEncrypted = $encrypter->encryptString($data);
return $dataEncrypted;
}
The problem is that I get an error on Laravel states: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.
How can I use the key that I use on Node.js in Laravel?
I see two issues with the PHP code:
$this->key
instead of $key
Encrypter()
appears to be expecting a binary string (not a string of hex characters), so you need to decode the hex first (e.g. using hex2bin()
).