supercoolville
supercoolville

Reputation: 9086

PHP: JSON not working when using urldecode and mcrypt

When I run json code through json_decode it works fine, but when I encrypt with mcrypt and encode with urlencode then decode and decrypt, it doesn't work.

Does anyone know what's wrong?

The decrypted json looks exactly like the json before being encrypted.

My code:

<?
    $json = '{"entry1":{"name":"bob","age":"15"},"entry2":{"name":"bill","age":"50"}}';

    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $key = "abcdefghijkl";
    $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $json, MCRYPT_MODE_ECB, $iv);
    $urlencoded = urlencode($encrypted);
    $urldecoded = urldecode($urlencoded);
    $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $urldecoded, MCRYPT_MODE_ECB, $iv);

    // json and decrypted json comparison
    echo "<h3>JSON & Decrypted JSON look the same:</h3>";
    echo $json . " // json<br>";
    echo $decrypted . " // decrypted json<br>"; 

    // json - works!
    echo "<h3>JSON works:</h3>";
    $data = json_decode($json); 
    $i = 1;

    while ($i <= 2) {
        $entrynumber = "entry" . $i;
        echo "name ----- " . $data->$entrynumber->name . "<br>";
        echo "age ------- " . $data->$entrynumber->age . "<br>";
        $i++;
    }

    // decrypted json - doesnt work!
    echo "<h3>Decrypted JSON doesnt work:</h3>";
    $data = json_decode($decrypted);
    $i = 1;

    while ($i <= 2) {
        $entrynumber = "entry" . $i;
        echo "name ----- " . $data->$entrynumber->name . "<br>";
        echo "age ------- " . $data->$entrynumber->age . "<br>";
        $i++;
    }
?>

If you paste that code into a php document you will see what I mean.

Screenshot:

enter image description here

Upvotes: 2

Views: 1622

Answers (2)

ontrack
ontrack

Reputation: 3043

Your encryption/decryption algorithm is adding padding to conform to the block-size. You should remove null-characters from the end, for example:

rtrim($decrypted, "\0");

Upvotes: 10

Ayush
Ayush

Reputation: 42440

Did some troubleshooting and this is what I got:

URL Encode/decode is not the problem. String before encoding and after decoding are the same:

Before encoding:

string 'è?¡OùpU4ˆß•ý£ÉGÒô½åLqe 2w¨”—Ô¢§|MþWþxÎZ±8“єЩɓ ŽšÁkèíSòøÓ€¥ðÒ(³!§¬žIê\&' (length=96)

After decoding:

string 'è?¡OùpU4ˆß•ý£ÉGÒô½åLqe 2w¨”—Ô¢§|MþWþxÎZ±8“єЩɓ ŽšÁkèíSòøÓ€¥ðÒ(³!§¬žIê\&' (length=96)

The problem is in the encryption / decryption process.

Json string before encrypting:

string '{"entry1":{"name":"bob","age":"15"},"entry2":{"name":"bill","age":"50"}}' (length=72)

Json string after decrypting:

string '{"entry1":{"name":"bob","age":"15"},"entry2":{"name":"bill","age":"50"}}������������������������' (length=96)

See the extra garbage characters added to the end.

After decrypting if you do this to trim out the garbage characters, your json decodes fine:

json_decode(substr($decrypted,0,72));

I'm not very familiar with mcrypt, so hopefully someone can help you figure out why you're getting the additional characters at the end.

Upvotes: 3

Related Questions