NYCPHP Meetup

NYPHP.org

[nycphp-talk] MCrypt problems decrypting data

csnyder chsnyder at gmail.com
Sat Aug 18 09:12:37 EDT 2007


On 8/17/07, Sean <sean712 at gmail.com> wrote:
> I am having issues with encryption/decryption.
>
> I am attempting to encrypt an integer (61663)
>
> when i decrypt it I get the following appended to my decrypted value.
>
> 61663\357\277\275\357\277\275\357\277\275
>
> $td = mcrypt_module_open('blowfish', '', 'cbc, '');
>  $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
>     mcrypt_generic_init($td, $key, $iv);
>     $decrypted_data = mdecrypt_generic($td, base64_decode($input));
>
>
> BTW I base 64 encode the encypted integer(61663) and store it in a cookie
>
> Any Help would be greatly appreciated
>
> Thanks,
>
> Sean
>


I can see two potential problems:

You need to use the same initialization vector (IV) to decrypt as you
did to encrypt. Typically, after encryption, you prepend the iv to the
ciphertext value and store the whole thing (iv+encrypted). When you're
ready to decrypt, you chop the iv off the front and use it to decrypt
the ciphertext.

Second, how are you generating the key? Is it a multiple of 32 characters long?

-- 
Chris Snyder
http://chxo.com/



More information about the talk mailing list