473,395 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

'scramble' string

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
Jul 23 '05 #1
4 6191
"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
Jul 23 '05 #2

"Steve" <st*********@hotmail.com> schrieb im Newsbeitrag
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!

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

Jul 23 '05 #3
st*********@hotmail.com (Steve) writes:
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[i] = 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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4
st*********@hotmail.com (Steve) wrote in message news:<e3**************************@posting.google. com>...
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
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
7
by: Klaus Ambrass | last post by:
Hi all, I write applications for my company's intraweb, and recently we've had some eager users trying to get at some data they shouldn't. The way they did it was to look at the pages input tags...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
3
by: Jeremy S | last post by:
What are some options for scrambling information in the QueryString. Consider this URL for example: SomePage.aspx?userid=15 I don't care if there is something in the querystring - I'd just...
8
by: Scholar | last post by:
Hello friends, there is yet another problem for you... We have got a string say HOUR Now we need a program that can choose the specified number of words say n from this word in all possible ways...
3
by: onindita | last post by:
hello everyone! i m very here. i tried to make a program that will reverse the letters in each of a sequence of words while preserving the order of the words themselves. Sample Input Hello...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.