0

I want to encrypt an email to decrypt later.

At that point I don't care about safety, I just want it working. The mail is stored in $umail

Researching stackoverflow I found the following code which I have adapted:

 $keyword = "Key";
 $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);
 $activation = base64_encode($iv.mcrypt_encrypt(MCRYPT_RIJNDAEL_128, hash('sha256', $keyword, true), $umail, MCRYPT_MODE_CBC, $iv));
 $activation = rawurlencode($activation);

$activation is the encrypted mail.

I get the encrypted mail ($activation) as a part of a URL.

$key = $_GET['key'];
$key = rawurldecode($key);
$keyword = "Key";
$requestMail = base64_decode($key);
$ivDec = substr($requestMail, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));

$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, hash('sha256', $keyword, true), substr($requestMail, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)), MCRYPT_MODE_CBC, $ivDec), "\0");

Sometimes it shows the email I enter. Other times, with the same email input, it shows strange symbols when the encrypted word has "+".

Any advice?

It's solved. This is the post I needed:

PHP - Plus sign with GET query

I noticed that when a + was generated, decrypt was broken. In that post is explained how + is a PHP reserved word, so it must be converted to %2B and after that, reconverted to +. To do this you use: rawurlencode, rawurldecode.

Code is updated.

Enjoy!

Scott Arciszewski
  • 33,610
  • 16
  • 89
  • 206
HugoRod
  • 33
  • 5
  • http://stackoverflow.com/questions/16600708/how-do-you-encrypt-and-decrypt-a-php-string – Sanjay Chaudhari Mar 11 '16 at 10:26
  • With the "+" I mean that if my mail y userMail, and the encrypted word has a "+", the dencryption has strange symbols – HugoRod Mar 11 '16 at 10:43
  • *"(...) strange symbols when the encrypted word has '+'"* Are you url encoding your messages before transmission? – jDo Mar 11 '16 at 12:21
  • I solved it @jDo, check the edited question – HugoRod Mar 11 '16 at 12:48
  • 1
    @Good. FYI the plus sign `+` is not a reserved keyword in PHP but in the [URI standard](https://tools.ietf.org/html/rfc3986#section-2.2) created by the IETF. In other words, it would fail in all programming languages without the proper encoding. – jDo Mar 11 '16 at 12:52
  • @jDo oh thanks for the information – HugoRod Mar 11 '16 at 13:36

0 Answers0