Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Basic encoder

  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post

    Basic encoder

    How could a make a basic javascript encoder that can encode and decode text?

    (i just would want some tips on how to start)

  • #2
    New Coder
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts
    what's your purpose? how about escape and encodeURIComponent ?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    <script type = "text/javascript">
    
    function encodeToHex(str){
    var r = "";
    var len = str.length;
    var c = 0;
    var h;
    while(c < len){
    h = str.charCodeAt(c++).toString(16);
    while (h.length<2){h="0"+h}
    r+=h;
    }
    return r;
    }
    
    var enc = encodeToHex("Your message");
    document.write(enc);
    
    </script>

    Obviously such coding is insecure as it can easily be decoded.


    Quizmaster: Name three Prime Ministers of the Republic of Ireland since 1937?
    First Contestant: Neil Kinnock
    Second Contestant: Paddy Ashdown
    Third Contestant: Tony Benn

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    One purpose is just for fun but i want it for coding just plain text to code and i dont know what aencodeURIComponent.
    Last edited by sherlockturtle; 07-27-2011 at 03:06 PM.

  • #5
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by sherlockturtle View Post
    How could a make a basic javascript encoder that can encode and decode text?

    (i just would want some tips on how to start)
    Here's a quick and dirty encoder / decoder. If you run text through once, it gets encoded. Run the encoded through it and you get back the original.

    Code:
    var coder = function (text) {
        var str = '';
        var len = text.length;
        var charCode = 0;
        while (len--) {
            charCode = text.charCodeAt(len);
            charCode = (-charCode) + 255;
            str += String.fromCharCode(charCode);
        }
        return str; // return encoded result
    }
    What it does is replace:

    .......a,b,c,d,e..........u,v,w,x,y,z........

    with....

    ......z,y,x,w,v,u.........e,d,c,b,a........

    ...and it does it backwards starting from the end and ending at the beginning.

    Hope you can use it.

    -- Roger

    Edit: One downside of this code is that the characters it generates may not display properly on a web page, and if they do display, a copy and paste of the encoded text, run through the code again to "decrypt" it may fail. Study how this code works and then modify it to only operate on readable ASCII characters (i.e. letters and numbers only) while leaving the rest alone.
    Last edited by Krupski; 07-27-2011 at 03:55 PM.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #6
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    document.write(enc);
    Document.write?????
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #7
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Krupski View Post
    Here's a quick and dirty encoder / decoder. If you run text through once, it gets encoded. Run the encoded through it and you get back the original.

    Code:
    var coder = function (text) {
        var str = '';
        var len = text.length;
        var charCode = 0;
        while (len--) {
            charCode = text.charCodeAt(len);
            charCode = (-charCode) + 255;
            str += String.fromCharCode(charCode);
        }
        return str; // return encoded result
    }
    What it does is replace:

    .......a,b,c,d,e..........u,v,w,x,y,z........

    with....

    ......z,y,x,w,v,u.........e,d,c,b,a........

    ...and it does it backwards starting from the end and ending at the beginning.

    Hope you can use it.

    -- Roger

    Edit: One downside of this code is that the characters it generates may not display properly on a web page, and if they do display, a copy and paste of the encoded text, run through the code again to "decrypt" it may fail. Study how this code works and then modify it to only operate on readable ASCII characters (i.e. letters and numbers only) while leaving the rest alone.
    This helps but what im looking for is a simple start so i can edit it for my own code? and also were does the output go or the input ?
    Last edited by sherlockturtle; 07-27-2011 at 04:08 PM.

  • #8
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by sherlockturtle View Post
    This helps but what im looking for is a simple start so i can edit it for my own code? and also were does the output go or the input ?
    Oh... OK let me explain the code to you line by line:

    var coder = function (text) {
    Defines a function named "coder" which accepts one variable as input. The input goes into the variable "text".

    var str = '';
    Creates a variable to use later. It's initialized with an "empty string".

    var len = text.length;
    Create a variable named "len" which has the length of the input string (for example, if you passed "hello" to the function, "len" would be "5").

    var charCode = 0;
    Create a variable to use later. It's initialized with the number zero.

    while (len--) {
    Start running a loop which will keep running until "len" is zero. len-- means "len = len - 1".

    charCode = text.charCodeAt(len);
    Get the character code at position pointed to by "len". For example, if the character is the letter "A", the variable "charCode" is 65.

    charCode = (-charCode) + 255;
    This changes the character code to whatever code is at the opposite end of the ASCII chart. It implements Y=MX+B where M is -1 and B is 255.

    str += String.fromCharCode(charCode);
    Build up a string with the reversed "encoded" characters.

    }
    This is the end of the loop. Anything inside these brackets runs inside the loop.

    return str;
    All done, return the result to the caller.

    }
    End of the function.


    Now, to use it you would do something like this:

    var X = 'Hello there'; // make X into a string
    var Y = coder(X); // send the string to "coder" and put the return value into Y
    "print" Y (displays encoded gibberish) // print Y (it is encoded gibberish)
    var Z = coder(Y); // put the encoded text back into "coder", result --> Z
    "print" Z (displays 'Hello there'). // print Z (un-encoded = original)


    Make sense?
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Krupski View Post
    Document.write?????
    Yes, document.write(). I am sorry but I do not understand the point you are making.
    The outputted result is 596f7572206d657373616765, which is "Your message" encoded.
    Last edited by Philip M; 07-27-2011 at 05:09 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Philip M View Post
    Yes, document.write(). I am sorry but I do not understand the point you are making.
    The reason I said what I said is that document.write() has limited utility and it's use is generally discouraged (or so seems to be the trend when reading about web programming).

    And since the OP is obviously beginning the steep climb up the learning curve, I thought mentioning document.write() would not be of much help to him.

    I don't know what your personal opinion is, but typically web programmers just want document.write() to go away (same can be said for MSIE....)

    -- Roger
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #11
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Philip M View Post
    The outputted result is 596f7572206d657373616765, which is "Your message" encoded.
    Verified!

    Code:
    list
    10 A$="596f7572206d657373616765"
    20 FOR X = 1 TO LEN(A$) STEP 2
    30 PRINT CHR$(VAL("&H"+MID$(A$,X,2)));
    40 NEXT X
    Ok 
    run
    Your message
    Ok
    (yes I still use GWBasic!)
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    I think that everyone would agree that document.write() has limited utility - but this is an example where it is entirely suited to the purpose. I could have used "alert" but that would not have allowed the OP to copy and paste the result into another script to decode it again. How do you intend to "print" the output of your code?

    I do hope that you are not going to be yet another guy who would go into an empty room and still start an argument.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Philip M View Post
    I do hope that you are not going to be yet another guy who would go into an empty room and still start an argument.
    I'm here to get help when I need it and give help if I can. I'm not here to argue with anyone. Sorry if you thought I was...
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Krupski View Post
    Verified!

    Code:
    list
    10 A$="596f7572206d657373616765"
    20 FOR X = 1 TO LEN(A$) STEP 2
    30 PRINT CHR$(VAL("&H"+MID$(A$,X,2)));
    40 NEXT X
    Ok 
    run
    Your message
    Ok
    (yes I still use GWBasic!)
    Good heavens! You can decode it in Javascript very simply (and very similarly) with

    Code:
    <script type = "text/javascript">
    
    var str2 = '596f7572206d657373616765';
    var str3 = "";
    for(var i=0; i<str2.length; i=i+2){
    str3=str3+'%'+str2.substr(i,2);
    }
    document.write(unescape(str3));  // Your message
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #15
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    Ive started makine one of my own when im done in around a day ill show it to you guys.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •