views:

500

answers:

2

I have the following Encryption Classs in php

define(ENCRYPTION_KEY,"abcdegef");
define(INITIALIZATION_VECTOR,mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_ECB), MCRYPT_RAND));

function EncryptString($input)
{
    $encrypted_string = mcrypt_encrypt(MCRYPT_DES, ENCRYPTION_KEY, $input, MCRYPT_MODE_CBC, INITIALIZATION_VECTOR);
    return base64_encode($encrypted_string);
}

function DecryptString($encryptedInput)
{
    $decrypted_string = mcrypt_decrypt(MCRYPT_DES, ENCRYPTION_KEY, base64_decode($encryptInput), MCRYPT_MODE_CBC, INITIALIZATION_VECTOR);
    return $decrypted_string;
}


And have url on anchor tag with querystring which i am encrypting

<a href="SomePage.php?action=<?php include_once ('EncryptionLibrary.php');
echo EncryptString("IamData"); ?>

When I am trying to decrypt it on SomePage.php using following code .. I am getting decrypted value incorrect

if (isset($_GET["action"]))
{
        echo trim(DecryptString($_GET["action"]));
}
+2  A: 

The value of INITIALIZATION_VECTOR is different each time. For modes that use an IV you need the same one for encryption and decryption.

Ewan Todd
then how to do this ?
Nav Ali
Options. (a) Use a mode (like EBC) that doesn't use an IV. (b) Send the IV to the page (it doesn't need to be secret). (c) Create the IV once, by hand. Hardcode it [*], perhaps using serialize and unserialize. [*] I can't believe I'm advising you to hardcode data. Don't tell anyone I said that.
Ewan Todd
A: 

I tried to use it but my app throws "Call to undefined function mcrypt_create_iv() ..." exception

abera zeinu