'scramble' string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    '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
  • Dag Sunde

    #2
    Re: 'scramble' string

    "Steve" <steve551979@ho tmail.com> wrote in message
    news:e3805b94.0 405102049.4f4fd 158@posting.goo gle.com...[color=blue]
    > 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!
    >[/color]

    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.Unsuppo rtedEncodingExc eption e)
    {
    System.out.prin tln("Unicode/ISO FuckUp!");
    System.exit(-1);
    return "";
    }
    }
    }

    --
    Dag
    58°26'15.9" N 008°46'45.5" E


    Comment

    • Gernot Frisch

      #3
      Re: 'scramble' string


      "Steve" <steve551979@ho tmail.com> schrieb im Newsbeitrag
      news:e3805b94.0 405102049.4f4fd 158@posting.goo gle.com...[color=blue]
      > Hi,
      >
      > I am trying to do a very simple "encryption " of a text string in[/color]
      java[color=blue]
      > script. For instance, if the user enters : steve, I want to just
      > convert each character to its ASCII value and add 5 to each[/color]
      character,[color=blue]
      > 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[/color]
      can[color=blue]
      > use would be greatful!
      >
      > Thanks,
      > Steve[/color]

      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



      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: 'scramble' string

        steve551979@hot mail.com (Steve) writes:
        [color=blue]
        > 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![/color]

        The two functions you need are
        charCodeAt (on string objects)
        and
        fromCharCode (on String)

        Example
        ---
        function encodeString(st ring) {
        var chars = [];
        for(var i=0;i<string.le ngth;i++) {
        chars[i] = String.fromChar Code(string.cha rCodeAt(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 - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Shawn Milo

          #5
          Re: 'scramble' string

          steve551979@hot mail.com (Steve) wrote in message news:<e3805b94. 0405102049.4f4f d158@posting.go ogle.com>...[color=blue]
          > 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[/color]

          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

          Comment

          Working...