On my current project I am saving personal info into a MySQL database
for later retrieval. I have discovered that I have trouble with a few
specific data entries, though the other ~20 work fine. The two I have
trouble with are a char(2) and a varchar(4) field, the smallest ones in the
table, and they return garbage when decrypted. Is there a minimum field
size for using mcrypt? Sample code:
// to encrypt, init once
$mykey = 'keytext';
$td = mcrypt_module_open(MCRYPT_TRIPLEDES,'', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), 1234567890);
$ks = mcrypt_enc_get_key_size ($td);
$key = substr(md5($mykey), 0, $ks);
mcrypt_generic_init($td, $key, $iv);
(...)
$CreditCardExpMonth = mcrypt_generic($td, $_POST['Credit_Card_Exp_Month']);
(...)
// then save to database as part of insert query
If I save/retrieve the fields to/from the database w/o encryption they
save & retrieve fine. This field is two digits ('02', '11', etc.). The
other is the year (4 digits).
// to decrypt
// I wrote a function to truncate the returned string at the
// first \0 since mcrypt's decrypt pads the result
echo mydecrypt($data['CCExpMonth']);
function mydecrypt($enc) {
global $td;
$str = mdecrypt_generic($td, $enc);
$pos = strpos($str, "\0");
if ($pos !== false) {
$str = substr($str, 0, $pos);
}
return $str;
}
Also as long as I'm posting, mcrypt_generic_deinit() seems to be
undefined on my system? Is that an mcrypt issue or an interaction between
PHP 4.1.2 and mcrypt 2.4.x?
- Steve Yates
- Do trees moving back and forth make the wind blow?
~ Taglines by Taglinator - www.srtware.com ~
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php