I wrote those two following PHP functions to encrypt and decrypt strings easly and with a stronger encryption module than the other examples on the net (please, note that “strong” does not equals “secure”!);
Happy encryption/decryption!
Max
function encrypt($input_string, $key){
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$h_key = hash('sha256', $key, TRUE);
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $h_key, $input_string, MCRYPT_MODE_ECB, $iv));
}
function decrypt($encrypted_input_string, $key){
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$h_key = hash('sha256', $key, TRUE);
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $h_key, base64_decode($encrypted_input_string), MCRYPT_MODE_ECB, $iv));
}



6 comments
Mike
April 19, 2010 at 3:33 pm (UTC 2)
This does look extremely “strong” as I am having a tough time understanding most of the code there. Can you just clarify what the $input_string and $key variables are?
Thanks
Mike
mitzip
April 30, 2010 at 7:41 pm (UTC 2)
$input_string is the text you wish to encrypt
$key is your “password” or “passphrase”
maxvergelli
September 9, 2010 at 8:11 pm (UTC 2)
exactly
bagusjavas
September 21, 2010 at 8:29 am (UTC 2)
Thanks Max..
I Like it..
Let me use it
maxvergelli
September 21, 2010 at 3:29 pm (UTC 2)
Free to use it!
Joe
November 3, 2012 at 2:29 am (UTC 2)
Thank you maxvergelli, this is very useful..!
Thanks..