2

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;
}
Julius Vainora
48.4k9 gold badges95 silver badges107 bronze badges
asked Feb 26, 2012 at 23:06
6
  • Where is your code where you are actually encrypting your email and retrieving the get string? Commented Feb 26, 2012 at 23:08
  • Have you tried base64? Is not encryption but will do the work. Commented Feb 26, 2012 at 23:09
  • If you want alternatives to hex2bin and bin2hex, you can try playing with "pack" and "unpack" php.net/manual/en/function.pack.php Commented Feb 26, 2012 at 23:11
  • 1
    @Optimist: pack() produces binary data Commented Feb 26, 2012 at 23:12
  • @zerkme sure, that's why I said pack and unpack :-) I linked to pack because that's where the documentation is best. But Base64 encoding as you suggested is a good solution too and probably easier. Commented Feb 26, 2012 at 23:22

2 Answers 2

4

Use base64_encode() to encode anything in text-safe format, and base64_decode() to get the original data back

answered Feb 26, 2012 at 23:08
1
  • 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! Commented Feb 26, 2012 at 23:26
2

Just use urlencode() to convert all the "ñ ̄üŸPsúœœ áagœcharacters before sending them into the URL and andurldecode()` them on the other side.

http://php.net/manual/en/function.urlencode.php

answered Feb 26, 2012 at 23:08
3
  • it's not, but that shouldn't be a concern in this example Commented 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 ;-) Commented 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! Commented Feb 26, 2012 at 23:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.