I've been scratching my head on this one even though it shouldn't be so difficult... I'm trying to pass an email through a GET query string NOT POST I want to mask this email I don't really care about having a good encryption or anything as long as the email isn't shown in the url... Anyways I was trying to encrypt it and decrypt it using mcrypt_generic. The issue I'm having is when encrypted it converts the email into a variety of characters such as: "ñ ̄üŸPsúœœ á`agœ" I want to return normal alphanumeric character so I attempt to do a hex2bin on that and it works out great the issue is in php5 there is no bin2hex!!! and I've tried functions I found online to revert this change and it works as far as what you see but if you compare the strings in an if statement they don't match so of course when i decrypt it, it doesn't return the same value I passed initially. Anyways I was wondering if anyone could give me some suggestions on how I could pass an email through a GET without the email being readable an then reverting it back to normal when the parameter is read by my code.
These are my encryption methods and the converter from hex2bin function:
function encrypt($data)
{
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
if (mcrypt_generic_init($cipher, _QRYKEY, _IVKEY) != -1)
{
$encrypted_data = mcrypt_generic($cipher,$data );
mcrypt_generic_deinit($cipher);
}
return bin2hex($encrypted_data);
}
function decrypt($data)
{
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
if (mcrypt_generic_init($cipher, _QRYKEY, _IVKEY) != -1)
{
$decrypted_data = mdecrypt_generic($cipher,hex2bin($data));
mcrypt_generic_deinit($cipher);
}
return $decrypted_data;
}
function hex2bin($h)
{
if (!is_string($h)) return null;
$r='';
for($a=0; $a<strlen($h); $a+=2)
{
$r.=chr(hexdec($h{$a}.$h{($a+1)}));
}
return $r;
}
2 Answers 2
Use base64_encode()
to encode anything in text-safe format, and base64_decode()
to get the original data back
-
cant believe it was just that... i knew it hadto be alot simpler than everything i was doing but i couldnt put my finger on it... thanks a bunch!Orlando– Orlando2012年02月26日 23:26:54 +00:00Commented Feb 26, 2012 at 23:26
Just use urlencode()
to convert all the "ñ ̄üŸPsúœœ á
agœcharacters before sending them into the URL and and
urldecode()` them on the other side.
-
it's not, but that shouldn't be a concern in this exampleBen D– Ben D2012年02月26日 23:14:50 +00:00Commented Feb 26, 2012 at 23:14
-
I just checked it and seems like it is binary safe thus can be used as a solution. That's why I deleted my comment ;-)zerkms– zerkms2012年02月26日 23:22:18 +00:00Commented Feb 26, 2012 at 23:22
-
:) thanks for double-checking. I actually googled it and found a random reference suggesting it's not, but that's good to know!Ben D– Ben D2012年02月26日 23:32:04 +00:00Commented Feb 26, 2012 at 23:32
pack()
produces binary datapack
because that's where the documentation is best. But Base64 encoding as you suggested is a good solution too and probably easier.