1

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?

Community
  • 1
  • 1
And Finally
  • 5,602
  • 14
  • 70
  • 110

2 Answers2

1

use base64_encoding before json encoding.

$result_arr['payload'] = base64_encode($encrypted);

At receiving end don't forget to base64_decode

Harikrishnan
  • 9,688
  • 11
  • 84
  • 127
1

It might be important to note that the OP's code is now deprecated - I came across this Stackoverflow question from Google and after a deeper search, mcrypt_encrypt is now considered deprecated as of PHP 7.1.0.

Full details available here - http://php.net/manual/en/function.mcrypt-encrypt.php

Pantoflarz
  • 76
  • 7