Nav Ali
Nav Ali

Reputation: 1230

Encrypting Querystring and Decrypting it in PHP

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"]));
}

Upvotes: 2

Views: 3140

Answers (1)

Ewan Todd
Ewan Todd

Reputation: 7312

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

Upvotes: 2

Related Questions