2 way encryption...

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

Post Reply
User avatar
ironhamster88
Forum Newbie
Posts: 3
Joined: Fri Oct 03, 2008 8:51 am
Location: Essex, UK

2 way encryption...

Post 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 :-)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: 2 way encryption...

Post by onion2k »

MySQL has AES_ENCRYPT() and AES_DECRYPT functions built in. I use them.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: 2 way encryption...

Post 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)
Post Reply