We use mcrypt with php to encrypt a key passed through a url, ie :
www.x.com/x.php?key=xxxxxxxxWe are moving to a new server and decryption fails on the new server. It turns out that the key is stripped of '+' characters and replaced with spaces and when they are restored the decryption succeeds. The strange thing is that '+''s are stripped from the key on the old server as well, as I can clearly see after writing it to a file, but decryption there succeeds.
I have no idea if '+' is the only problem character and I need to come up with a comprehensive solution to the problem. The fact that it works on the old server suggests that the cause is some difference in the software between the two servers. The old server has php 4.3.10 and mcrypt version listed as >=2.4.x. The new server has php 5.1.2 and mcrypt 2.5.7. Below is the code used to encrypt and decrypt. The key variable is accessed as $GLOBALS['key'] but I've tried it with $_GET['key'] and $_REQUEST['key'] with the same results.
encryption code======================
==========
==========
=
$iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256,MCRYP
T_MODE_ECB
), MCRYPT_RAND);
$dirkey = "xxxxx";
$timekey = "xxxxx";
$url="somedirname";
$today=time();
$crypttext = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $dirkey, $url,MCRYPT_MODE_ECB, $iv);
$crypttime = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $timekey, $today,MCRYPT_MODE_ECB, $iv);
$minurl="?key=".base64_enc
ode($crypt
text).":::
***".base6
4_encode($
crypttime)
;
==========================
==========
==========
decryption code======================
==========
==
//Function for decrypting the encrypted values
function decryption($value,$keyval)
{
//Create an initialization vector (IV) from a random source
$iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256,MCRYP
T_M
ODE_ECB), MCRYPT_RAND);
$decrypted_val=mcrypt_decr
ypt(MCRYPT
_RIJNDAEL_
256, $keyval, base64_decod
e($value),MCRYPT_MODE_ECB,
$iv);
return $decrypted_val;
}
Start Free Trial