I want to send the "message" to php as json data. But The "message" has to be a String. If I "stringfy" my json data in Javascript and encrypt them with "CryptoJS.AES.encrypt", I cannot get the single contents in PHP because "json_decode" always returns NULL.
I have used "json_last_error" and it returned 3. When I encode it with "utf8_encode" and json_encode them, it returns 0.
"mcrypt_decrypt" is the PHP AES-decryptor.
I really don't know what to do. Please help me and thanks in advance!
//JAVASCRIPT
var encrypted = CryptoJS.AES.encrypt(
JSON.stringfy({'message':'message','messageA':'messageA','messageB':'messageB'}),
key512Bits500Iterations, {iv:iv});
var data_base64 = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
var iv_base64 = encrypted.iv.toString(CryptoJS.enc.Base64);
var key_base64 = encrypted.key.toString(CryptoJS.enc.Base64);
$.ajax({
url: 'http://localhost/workspace/messageAppl.php',
type: 'POST',
data: {
'data_base64':data_base64,
'iv_base64':iv_base64,
'key_base64':key_base64 //key_base64 will be encrypted with RSA
},
success: function(data){
alert(data);
},
error: function(){
alert('Index-Error');
}
});
// PHP
// I can get the jsonString but I can't get the single message like 'message', 'messageA' or 'messageB'
...
//Decryption in PHP
public function jsMessage($data_base64, $iv_base64, $key_base64){
$data_enc = base64_decode($data_base64); // data_base64 from JS
$iv = base64_decode($iv_base64); // iv_base64 from JS
$key = base64_decode($key_base64); // key_base64 from JS
$plaintext = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $key, $data_enc, MCRYPT_MODE_CBC, $iv ), "\t\0 " );
return $plaintext;
}
$json_string = aes_decrypt($_POST['data_base64'], $_POST['iv_base64'], $_POST['key_base64']);
// json_decode returns NULL but WHY?
$array=json_decode($json_string);
$message=$array->message
$messageA=$array->messageA
$messageB=$array->messageB
**Edit 1**
The error message I get is:
**"Control character error, possibly incorrectly encoded"**
but the Json which I get in php after the decryption is valid:
{"message":"blablabalbalbalaballab","messageA":"blablabalbalbalaballab" ,"messageB":"blablabalbalbalaballab"}
and to be sure I'have tested the json again and again here
**Edit 2 **
I cannot post it with these signs that's the reason I've made a photo.
at Artjom B. : I have added the decrypt method.
Thanks for your answers! – Hungry2Learn Apr 11 '15 at 08:53