...

View Full Version : Basic encoder



sherlockturtle
07-27-2011, 05:39 AM
How could a make a basic javascript encoder that can encode and decode text?

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

henry42
07-27-2011, 07:59 AM
what's your purpose? how about escape and encodeURIComponent ?

Philip M
07-27-2011, 08:38 AM
<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

sherlockturtle
07-27-2011, 03:52 PM
One purpose is just for fun but i want it for coding just plain text to code and i dont know what aencodeURIComponent.

Krupski
07-27-2011, 04:46 PM
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.



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

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.

Krupski
07-27-2011, 04:49 PM
document.write(enc);



Document.write????? :eek:

sherlockturtle
07-27-2011, 05:05 PM
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.



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

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 ?

Krupski
07-27-2011, 05:27 PM
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?

Philip M
07-27-2011, 06:07 PM
Document.write????? :eek:

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.

Krupski
07-27-2011, 07:01 PM
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

Krupski
07-27-2011, 07:05 PM
The outputted result is 596f7572206d657373616765, which is "Your message" encoded.

Verified!



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!)

Philip M
07-27-2011, 07:09 PM
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. ;)

Krupski
07-27-2011, 07:12 PM
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...

Philip M
07-27-2011, 07:12 PM
Verified!



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


<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>

sherlockturtle
07-27-2011, 08:20 PM
Ive started makine one of my own when im done in around a day ill show it to you guys.

rnd me
07-27-2011, 11:06 PM
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
heck no!

how else could i stall the page rendering to load a off-domain script?

<script>
if(!document.querySelectorAll){
document.write("<"+"script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'><\/script>")
}//end if no css query
</script><script>
$=document.querySelectorAll?function(selector,root){root=root||document.documentElement; return root.querySelectorAll(selector); }:jQuery;
</script>

sherlockturtle
07-28-2011, 05:37 AM
Any of you know a basic starting ground i made my own but its not very good.

Philip M
07-28-2011, 08:41 AM
Any of you know a basic starting ground i made my own but its not very good.

What is wrong with the two approaches/scripts you have been given? There is no point in asking for assistance and then ignoring it.

Philip M
07-28-2011, 08:43 AM
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....) ;)



document.write is great for adding script tags that load before the page finishes, commenting out some contents under certain conditions, injecting off-site remote data into a specific place in the document without server proxies, or for creating popups with dynamic content.

And I have no issues with IE.

bullant
07-28-2011, 11:45 AM
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....) ;)


Not this one ;)

document.write can be used to write output while the page is still loading and/or writing to a page opened with javascript.

With MSIE I don't have any issues at all - but I now support only IE7+ and, especially for commercial work, I write valid XHTML and CSS2 code.

If your code is invalid, you are much more likely to have browser compatability issues.

Kor
07-28-2011, 12:05 PM
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
I doubt. I'd rather say that web programmers use document.write() only when it is necessary to. And I also doubt that the MS crew have anything against this method. Why should they have? :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum