0

i have the below code for encryption , but the problem is its not decrypting the data as reqired

$salt ='whatever_you_want';

$en= simple_encrypt('data');
echo simple_decrypt($en);
function simple_encrypt($text)
{
    global $salt;
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $text, MCRYPT_MODE_CBC, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND))));
}

function simple_decrypt($text)
{
    global $salt;
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($text), MCRYPT_MODE_CBC, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND)));
}

the problem is it encrypts and each time it generates a new encrypted text as i want it to happen but the decryption is not happening

im getting something like this

%B…*¥   Þ‚á+ËU:L|(øŽ«úÐ9ÇvÉêÿ¿Ïg

any insight would be appreciated guys :)

mega-crazy
  • 838
  • 2
  • 17
  • 36

1 Answers1

0

When decrypting, you need to use the same IV and key as when encrypting.

Your encrypt function needs to return the IV as well as the encrypted data. That IV should then be sent to the decrypt function along with the data and the key.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337