Anyhow.. Ive been trying to make sense of the PHP manual, and simply cant seem to find a reliable example or guidance in making these work - I need to be able to hash and encrypt an array of values and store them to a mysql blob , and at a later date, decrypt them back to the original values..
My code so far :
Code: Select all
$key = 'I23B56a'; //the "salt"
$info = array('123.45','myemail@somewhere.com','12275','USA','extrastring','2006-01-07');//array to be hashed
$pinfo = gzcompress($info);//compress it
$crypted = des_encrypt($pinfo,$key);//encrypt it
echo "<pre>";
var_dump($info);//correct
var_dump($crypted);//works
$clear = des_decrypt($crypted,$key);//decrypt teh crypted value
var_dump($clear);//expecting a gzcompressed array
$cinfo = gzuncompress($clear);//gives data error due to the bad data given from decrypt
var_dump($cinfo);//gives false (of course)
function des_encrypt($string,$key)
{
$iv_size = mcrypt_get_iv_size('des', 'ecb');
$iv = mcrypt_create_iv($iv_size, 256);
$crypttext = mcrypt_encrypt(MCRYPT_DES, $key, $string, MCRYPT_MODE_ECB, $iv);
return $crypttext;
}
function des_decrypt($string,$key)
{
$iv_size = mcrypt_get_iv_size('des', 'ecb'); //returns integer 8
$iv = mcrypt_create_iv($iv_size, 256); //returns a binary value
$cleartext = mcrypt_encrypt(MCRYPT_DES, $key, $string, MCRYPT_MODE_ECB, $iv);
return $cleartext;
}Code: Select all
array(6) {
[0]=>
string(6) "123.45"
[1]=>
string(21) "myemail@somewhere.com"
[2]=>
string(5) "12275"
[3]=>
string(3) "USA"
[4]=>
string(11) "extrastring"
[5]=>
string(10) "2006-01-07"
}
string(16) "ánÕG: