Page 1 of 1

2 way encryption...

Posted: Tue Oct 07, 2008 6:58 am
by ironhamster88
Hi Guys,
I've got a website thats holding pretty sensative information in a mysql db. The problem being that the data will need to be decrypted and read, by a human, at some point in the future, so the data needs to be able to be decrypted quickly and on-the-fly back into it's original context.

I've been looking at mcrypt and read some interesting stuff, I just wondered if anybody knew a way to get 128 (or even 256) bit 2way encryption with PHP.

The following gave me interesting results...

Code: Select all

 
$string = 'sensative data';
$key = 'encryption key';
$cipher_alg = MCRYPT_RIJNDAEL_128;
 
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg, MCRYPT_MODE_ECB), MCRYPT_RAND);
$encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_CBC, $iv);
$decrypted_string = mcrypt_decrypt($cipher_alg, $key, $encrypted_string, MCRYPT_MODE_CBC, $iv);
 
echo 'Decrypted string: ' . $decrypted_string;
 
The result was: Decrypted string: sensative data??
WITH the two symbols on the end, so obviously, we can't be doing with that.

If anybody can help me it would be much appriciated.

Cheers for your help :-)

Re: 2 way encryption...

Posted: Tue Oct 07, 2008 7:09 am
by onion2k
MySQL has AES_ENCRYPT() and AES_DECRYPT functions built in. I use them.

Re: 2 way encryption...

Posted: Tue Oct 07, 2008 3:05 pm
by Mordred
The data is padded with zeroes, it's up to you to provide a mechanism for cutting the padding (normally, record the length of the data, and substr)