I am encrypting with PHP a JSON file like this:
0: {ID: 0, NAME: "London", REGION: "ENGLAND", …}
1: {ID: 1, NAME: "Rome", REGION: "ITALY", …}
And I am using this library crypt in PHP decrypt in JS AES
function cryptoJsAesEncrypt($passphrase, $value){
$salt = openssl_random_pseudo_bytes(8);
$salted = '';
$dx = '';
while (strlen($salted) < 48) {
$dx = md5($dx.$passphrase.$salt, true);
$salted .= $dx;
}
$key = substr($salted, 0, 32);
$iv = substr($salted, 32,16);
$encrypted_data = openssl_encrypt(json_encode($value), 'aes-256-cbc', $key, true, $iv);
$data = array("ct" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "s" => bin2hex($salt));
return json_encode($data);
}
I can easily encrypt in PHP and write to server (writing a JSON file) with a 128bit key like
$key = "TjWnZq4t7w!z%C*F";
$ForecastArray_crypt= cryptoJsAesEncrypt($key, $ForecastArray);
file_put_contents('ukforecastlist_example_crypt.json', $ForecastArray_crypt);
The problem is that when I try to decrypt in JavaScript with the call
var ukforecastlist_decrypt = JSON.parse(CryptoJS.AES.decrypt(myjson.ct, "TjWnZq4t7w!z%C*F", {format: CryptoJSAesJson}).toString(CryptoJS.enc.Utf8));
The console returns:
Uncaught SyntaxError: Unexpected token V in JSON at position 0
at JSON.parse (<anonymous>)
at Object.parse (core_uk.js:33)
at Object._parse (aes.js:30)
at Object.decrypt (aes.js:31)
at Object.decrypt (aes.js:25)
at <anonymous>:1:54
Where V
is indeed the first letter of myjson.ct
.
What am I doing wrong? I did get the JSON with jQuery and parsed and stored it in myjson variable.