Page 1 of 1

hashing using mcrypt_encrypt and mcrypt_decrypt [SOLVED]

Posted: Sat Jan 07, 2006 7:07 pm
by trukfixer
Hiyas. Well, because of a mysql upgrade, we no longer have mysql with openssl support , and now I am tasked with building a set of functions with equivalent functionality to des_encrypt and des_decrypt (these are mysql functions that only work with openssl support compiled in)

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;
}
RESULTS:

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:

Posted: Sat Jan 07, 2006 7:22 pm
by feyd
Your decrypt calls mcrypt_encrypt()?

Posted: Sat Jan 07, 2006 7:38 pm
by trukfixer
:oops: Heh .. typo :) just goes to show my frustration with what I was doing .. :)

still, even with that correction..

Code: Select all

$key =  'I23B56a';
$info = array('123.45','myemail@somewhere.com','12275','USA','extrastring','2006-01-07');
$pinfo = gzcompress($info);
$crypted = des_encrypt($pinfo,$key);
echo "<pre>";
var_dump($info);
var_dump($crypted);
$clear =  des_decrypt($crypted,$key);
var_dump($clear);
var_dump(gzuncompress($clear));
var_dump($cinfo);


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_decrypt(MCRYPT_DES, $key, $string, MCRYPT_MODE_ECB, $iv);
    return $cleartext;
}
OUTPUT:

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: