1function CryptoJSAesDecrypt(passphrase,encrypted_json_string){
2
3 var obj_json = JSON.parse(encrypted_json_string);
4
5 var encrypted = obj_json.ciphertext;
6 var salt = CryptoJS.enc.Hex.parse(obj_json.salt);
7 var iv = CryptoJS.enc.Hex.parse(obj_json.iv);
8
9 var key = CryptoJS.PBKDF2(passphrase, salt, { hasher: CryptoJS.algo.SHA512, keySize: 64/8, iterations: 999});
10
11
12 var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv});
13
14 return decrypted.toString(CryptoJS.enc.Utf8);
15}
16
17console.log(CryptoJSAesDecrypt('your passphrase','<?php echo $string_json_fromPHP?>'));
1function CryptoJSAesEncrypt($passphrase, $plain_text){
2
3 $salt = openssl_random_pseudo_bytes(256);
4 $iv = openssl_random_pseudo_bytes(16);
5 //on PHP7 can use random_bytes() istead openssl_random_pseudo_bytes()
6 //or PHP5x see : https://github.com/paragonie/random_compat
7
8 $iterations = 999;
9 $key = hash_pbkdf2("sha512", $passphrase, $salt, $iterations, 64);
10
11 $encrypted_data = openssl_encrypt($plain_text, 'aes-256-cbc', hex2bin($key), OPENSSL_RAW_DATA, $iv);
12
13 $data = array("ciphertext" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "salt" => bin2hex($salt));
14 return json_encode($data);
15}
16
17$string_json_fromPHP = CryptoJSAesEncrypt("your passphrase", "your plain text");