Search code examples
phpencryptionmcrypt

Using mCrypt with same IV for several pages?


I am using following code to Encrypt my url data like ?a=1&b=2&c=3 and I send this encrypted data to another file like this

http://www.example.com/parse.php?data={encrypted_string}

but on this page, i does not get decoded result, Just getting blank screen, Here are both functions used for encryption and decryption

function encrypt($decrypted, $password, $salt) {

 $key = hash('SHA256', $salt . $password, true);
mode.  (Note: ECB mode is inadequate as IV is not used.)
 srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);

 if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;

 $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));

 return $iv_base64 . $encrypted;
 }

Second Function for decryption is this.

function decrypt($encrypted, $password, $salt) {

 $key = hash('SHA256', $salt . $password, true);

 $iv = base64_decode(substr($encrypted, 0, 22) . '==');


 $encrypted = substr($encrypted, 22);


 $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4");


 $hash = substr($decrypted, -32);


 $decrypted = substr($decrypted, 0, -32);

 if (md5($decrypted) != $hash) return false;

 return $decrypted;
 }

Here how I am using decrypt

$de_data = decrypt($_GET['data'],$ad_passcode, $salt);
echo '==>'.$de_data;

It works on same page, Like if I do Encryption and Decryption on the page it successfully works, But when i send Encrypted Text from One page to another, On another page Decryption does not works. gives blank page. return false;

Thanks for your help


Solution

  • I have solved this issue as below

    I do encryption as normal shown in my question, But when i call Decryption functions, I use like this

    $de_data = decrypt($_GET['data'],$ad_passcode='', $salt='');
    

    this gives me exact result, that i was looking on other pages