views:

122

answers:

5

I'm creating an application that requires passing email addresses around in querystrings and linking to these pages in public documents.

I'd like to prevent my site from becoming spambot heaven, so I'm looking for a simple algorithm (preferably in JavaScript) to encrypt/obfuscate the address so it can be used publicly ina URL without making the email address an easy target.

ex

www.mysite.com/[email protected]
 to
www.mysite.com/page.php?e=aed3Gfd469201

Preferably the result would be a short-ish string that could easily be used in a URL. Any suggestions for what algorithm I could use?

A: 

How about simply hashing the email, say with sha256?

Zimm3r
And how do you get the email back again?
Borealid
I thought of this, but then I hit Borealid's realization.
MarathonStudios
+7  A: 

RSA-encrypt the data using the public key corresponding to a private key held only by your site.

Base64 and urlencode the result.

Borealid
This is the only 'secure' way of doing it. You could do simpler things like ROT13, etc which would be easy and fool most types of spam bots, but you know... easy to overcome if someone writes a custom spam bot.
thomasrutter
RSA seems a bit heavy duty for my needs. I don't need to completely "secure" the email, I just need to make sure that it's protected from spambots and (preferably) doesn't look like an ugly rot-13'ed email like [email protected].
MarathonStudios
Then skip the encryption and just base64 the string.
Paul Sasik
+1  A: 

Some options coming to my mind :)

Nikita Rybak
+2  A: 

you can make a simple function, which would xor each char value with some integer, and make a hex encoded string. (email addresses do not contain non-ascii characters, so it won't complicate with multibyte chars). e.g.:

obfusc = function(s, c) {
  c = c || 0x7f;
  r = "";
  for (i in s) {
    valh = (s.charCodeAt(i) ^ c).toString(16);
    if (valh.length == 1) valh = "0" + valh;
    r += valh;
  };
  return r;
}

deobfusc = function(s, c) {
  c = c || 0x7f;
  r = "";
  for (var i=0; i<(s.length/2); i++) {
    r += String.fromCharCode(parseInt(s.substr(i*2, 2), 16) ^ c)
  };
  return r;
}

addr = "[email protected]";
x = obfusc(addr);
alert(addr + " -> " + x + " -> " + deobfusc(x))

// [email protected] -> 15101a3f1a071e120f131a511c1012 -> [email protected]
mykhal
A: 

The simplest method is simply to store the email addresses within a database table, and pass around a key / ID field that you look up at the point where you want to actually send an email.

caf