I want to create a $state string, and send it in a URL to another app. The other app will do it's thing (OAuth verification) before redirecting back to the orignal app with the state returned once more in the URL (e.g. state=789sdgydfg9d...)
Below I'm simply testing the encrypt/decrypt with mcrypt functions:
$secret_key = 'e6aeee1c80c231bf724b31d3c4cc77dd';
$string = 'This is a string to encode';
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
// this will be sent with the first redirect...
$state = rawurlencode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $string, MCRYPT_MODE_CBC, $iv));
// .. this will be received when the client is redirected back here
// in another HTTP request
$decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secret_key, rawurldecode($state), MCRYPT_MODE_CBC, $iv);
var_dump($decrypted_string); // Outputs: "This is a string to encode"
If I do this test, it works coz it's the same request, and I guess $iv is the same for the mcrypt_encrypt and mcrypt_decrypt.
However, if I split this between two HTTP requests as I intend:
$secret_key = 'e6aeee1c80c231bf724b31d3c4cc77dd';
$string = 'This is a string to encode';
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
// this will be sent with the first redirect...
$state = rawurlencode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $string, MCRYPT_MODE_CBC, $iv));
Then the client is redirected back with the state passed back in the URL:
$secret_key = 'e6aeee1c80c231bf724b31d3c4cc77dd';
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secret_key, rawurldecode($state), MCRYPT_MODE_CBC, $iv);
// Outputs: "�|% <�-��+^�%6��7�rY��^�"
It is all messed up. I'm guessing that as $iv is created another time with MCRYPT_RAND option it's gonna be different, thus decrypt differently too. But I can't find an example of how this ought to be written for this case.
How should I be encrypting/decrypting so that I can successfully decrypt after the redirect back?