'scramble' string

S

Steve

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!

Thanks,
Steve
 
D

Dag Sunde

Steve said:
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 "";
}
}
}
 
G

Gernot Frisch

Steve said:
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!

Thanks,
Steve

Make 2 arrays. One with all allowed characters and another with the
characters in a different order.
e.g.
"ABCDEabcde" vs "AbCdEaBcDe"
Now this is your dictionary. When scambling seek for character in A
and use the character at this position in B. Unscrable by swapping
lists.

BTW: Doing this in JavaScript is a bit ridicoulosly, since the source
code is available to everyone...

HTH,
Gernot
 
L

Lasse Reichstein Nielsen

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 two functions you need are
charCodeAt (on string objects)
and
fromCharCode (on String)

Example
---
function encodeString(string) {
var chars = [];
for(var i=0;i<string.length;i++) {
chars = String.fromCharCode(string.charCodeAt(i)+5);
}
return chars.join("");
}
---
(Collecting a lot of small strings in an array and joining them
once is more efficient than concatenating strings each round of
the loop).

/L
 
S

Shawn Milo

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!

Thanks,
Steve

Depending upon what you're trying to do, you can always use md5 encrytion.
It's one-way only, so if you're planning on converting the string back
later, then this won't work.

Shawn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top