I am using this function to encrypt strings with a custom key in Laravel
use IlluminateEncryptionEncrypter;
public static function aes_256_cbc($str, $key){
$Encrypter = new Encrypter($key, 'AES-256-CBC');
return $Encrypter->encrypt($str);
}
Then, use CryptoJS to decrypt it, I have searched and used many ways but still getting this problem:
var encrypted_json = JSON.parse(Base64.decode(encrypted));
// JSON Parsed
// {
// iv: xxx==,
// value: xxx+5+bUEKbwGaUHB6…f2/xxx/qaphls6ybd/xxx==,
// mac: xxx4c3aa002712ef5d7e80226bxxx,
// tag:
// }
var key = CryptoJS.enc.Utf8.parse(mykey);
var iv = CryptoJS.enc.Utf8.parse(encrypted_json.iv);
var decrypted = CryptoJS.AES.decrypt(encrypted_json.value, key, {
iv : iv,
mode: CryptoJS.mode.CBC
});
let res = decrypted.toString(CryptoJS.enc.Utf8)
Got this error:
Error: Malformed UTF-8 data
This problem can be easy to solve if I encrypt string by a custom function in Laravel, but it will be better if I can get a solution with CryptoJS. Can you help me?