I would like to send some encrypted data between two applications via JSONP. I'm using a PHP page to generate my JSONP. I'm trying to encrypt the value of one of my JSONP keys using mcrypt as described in one of the answers to this question:
$result_arr['logged_in'] = true;
$key = 'SuperSecretKey';
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, 'I want to encrypt this', MCRYPT_MODE_ECB);
$result_arr['payload'] = $encrypted;
print_r($result_arr);
echo 'authCallback(' . json_encode( $result_arr, JSON_UNESCAPED_UNICODE ) . ')';
The print_r line outputs the value of $result_arr['payload']
with a lot of Unicode-type symbols. The line echoing out the json_encoded value is null. Presumably json_encode doesn't like the non-ASCII characters. Can anyone tell me where I'm going wrong? Is there an alternate way of encrypting that json_encode will accept?
use base64_encoding
before json encoding.
$result_arr['payload'] = base64_encode($encrypted);
At receiving end don't forget to base64_decode