3

I want to encrypt a URL variable so that the user can't see the information when it is passed. I've found several scripts online but none of them work. Most seem to lean toward using base-64. Could someone help me write a short script that would encode or encrypt and then reverse that in the next page? It doesn't have to be super secure, just enough to mask an email address to the average user.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
sehummel
  • 5,476
  • 24
  • 90
  • 137

2 Answers2

2

If you're not concerned about security, you can just use rot13:

function rot13($string, $mode) {
    $s = fopen("php://memory", "rwb");
    stream_filter_append($s, "string.rot13", STREAM_FILTER_WRITE);
    fwrite($s, $string);
    rewind($s);
    return stream_get_contents($s);
}

var_dump(rot13("my@email.com", STREAM_FILTER_WRITE));
var_dump(rot13("zl@rznvy.pbz", STREAM_FILTER_READ));

will give:

string(12) "zl@rznvy.pbz"
string(12) "my@email.com"
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • Thanks, Artefacto. How do I get it to return just the email without the string information or the quotes? – sehummel Aug 26 '10 at 23:50
  • @shu The return value of `rot13` doesn't have any string information or quotes. Those are added by `var_dump`. – Artefacto Aug 26 '10 at 23:51
1

You can use a symmetric encryption algorithm. You can use mcrypt_encrypt and mcrypt_decrypt functions in mcrypt library.

http://php.net/manual/en/function.mcrypt-encrypt.php http://www.php.net/manual/en/function.mcrypt-decrypt.php

Zafer
  • 2,180
  • 16
  • 28