"Steve" <st*********@hotmail.com> wrote in message
news:e3**************************@posting.google.c om...
Hi,
I am trying to do a very simple "encryption" of a text string in java
script. For instance, if the user enters : steve, I want to just
convert each character to its ASCII value and add 5 to each character,
then convert back to a string giving: "xyj{j" for this example.
is there a simple way to do this? any suggestions on functions I can
use would be greatful!
The most well-known of those are probably the "ROT-13" algorithm, where
you have the 26 letters in a "Ring" buffer. For each letter in your string,
you add 13, and use the letter in that position instead.
Now the magic: when you add 13 the second time, you will be back to your
original letter.
Below is a Java implementation of a variation of rot13, which I've called
Rot39. (Excactly the same as rot13, but uses a larger "ring" of characters).
It should be relatively simple to implement this in JavaScript...
public class Scramble
{
private final static int UPPER_LIMIT = 125;
private final static int LOWER_LIMIT = 48;
private final static int CHARMAP = 39;
public Scramble()
{
}
/**
* rot39 is a variation of the ROT13 algorithm,
* that also scrambles numbers and, most important in this
* case; xml-tags ("<", ">" & "/")
* @param - data, String to (de)scrambled
* @return - The string in "data" in (de)scrambled form.
*/
public String rot39(String data)
{
try
{
byte[] buffer = data.getBytes("ISO-8859-1");
for(int iData = 0; iData < buffer.length; iData++)
{
int iCode = buffer[iData];
if((iCode >= LOWER_LIMIT) && (iCode <= UPPER_LIMIT ))
{
iCode+= CHARMAP;
if(iCode > UPPER_LIMIT)
{
iCode = iCode - UPPER_LIMIT + LOWER_LIMIT - 1;
}
buffer[iData] = (byte)iCode;
}
}
return new String(buffer, "ISO-8859-1");
}
catch( java.io.UnsupportedEncodingException e)
{
System.out.println("Unicode/ISO FuckUp!");
System.exit(-1);
return "";
}
}
}
--
Dag
58°26'15.9" N 008°46'45.5" E