Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-28-2012, 05:59 PM   PM User | #1
Tacopopo
New to the CF scene

 
Join Date: Aug 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Tacopopo is an unknown quantity at this point
Smile Java Script Text Encryptor

I need to write a script for a text encryptor. So with an input box, and output with the encrypted text. I need to hse,forlooping and html. Thx. Will compare what I get with you giys.
Tacopopo is offline   Reply With Quote
Old 08-28-2012, 06:31 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Sounds like homework! As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.

It is your responsibility to die() if necessary….. - PHP Manual
__________________

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.
Philip M is offline   Reply With Quote
Old 08-28-2012, 07:00 PM   PM User | #3
Tacopopo
New to the CF scene

 
Join Date: Aug 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Tacopopo is an unknown quantity at this point
I wish I was in school. Im actually in the marines atm. Once im out this is what I want to,do. A,buddy of mine told me to do this to learn how to do lot more stuff.. My spellin is off bcuz my laptop is in my barracks. Ill post what I think needs to br done when I get back on base. Thx
Tacopopo is offline   Reply With Quote
Old 08-28-2012, 07:17 PM   PM User | #4
nomanic
Regular Coder

 
nomanic's Avatar
 
Join Date: Feb 2009
Location: United Kingdom
Posts: 252
Thanks: 9
Thanked 33 Times in 33 Posts
nomanic is an unknown quantity at this point
do you want to make something yourself? or get hold of some code that will do it? there are lots of functions on the web for encryption, if you want to make it yourself theres no end of methods
__________________
<DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
<DmncAtrny> And then hurl it through the window of a Sony officer
<DmncAtrny> and run like hell

Portfolio, Tutorials - http://www.nomanic.biz/
nomanic is offline   Reply With Quote
Old 08-28-2012, 07:31 PM   PM User | #5
Tacopopo
New to the CF scene

 
Join Date: Aug 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Tacopopo is an unknown quantity at this point
I really just need a format or tell me what the function is . Just need a small push
Tacopopo is offline   Reply With Quote
Old 08-28-2012, 08:07 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Tacopopo View Post
I really just need a format or tell me what the function is . Just need a small push
What sort of encryption? Straightforward Ceasar or what? Encryption can be very sophisticated, and there are dozens of different methods. And what will be the use to which the encrypted output will be applied? Remember that using Javascript only you (and no-one else) will be able to see the result! That does not seem to be very useful.
__________________

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.
Philip M is offline   Reply With Quote
Old 08-28-2012, 08:29 PM   PM User | #7
Tacopopo
New to the CF scene

 
Join Date: Aug 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Tacopopo is an unknown quantity at this point
Something simple.just a steppn stone
Tacopopo is offline   Reply With Quote
Old 08-28-2012, 10:01 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
One thing you need to understand: If you write your encryption using JavaScript, then ANYONE who can access your web page can look at the code and see what you have done and then easily decrypt your encrypted text. Effective encryption *MUST* be done server-side, not in the browser.

But if this is just an exercise in learning, and if your want (say) some very very simple encryption scheme just for fun, then yes, it's easy to do in JavaScript.

A substitution cypher is about the simplest possible one. That is, where you simply exchange each character for another. The kind of thing you see in puzzle magazines all the time. Is that adequate?

One step up from that is a variable substitution cypher, where you have a "secret" (won't be secret if the code is written in JavaScript, of course, but other than that...) keyword or key phrase. And you cycle through that keyword as you encrypt the message, so that (example only) "a" becomes "x" in the first position but "a" becomes "3" in the second position, and so on. See here:
http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher

So...(a) how strong a cypher do you want? (b) do you care about it being trivial to "break" a cypher when the JavaScript code is known?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-28-2012, 10:34 PM   PM User | #9
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,462
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
One thing you need to understand: If you write your encryption using JavaScript, then ANYONE who can access your web page can look at the code and see what you have done and then easily decrypt your encrypted text. Effective encryption *MUST* be done server-side, not in the browser.
That is not true. A true encryption algorithm is just as hard to decrypt in JavaScript as it is server side. The problem is that most people get obfuscation, hashing, and encryption confused.

Obfuscated code can be easily read. Hashing isalmost always pointless in JavaScript. Encryption may be useful but cannot be done in just a few dozen lines of code.

Take a look at the cipher section at http://code.google.com/p/crypto-js/ for several examples of completely secure JavaScript encryption scripts. Note that thisprovides a 118k zip file of the various code. When decompressed for use with a web page even the smallest encryption script is going to be several kb in size. The last time I checked the Rabbit Cipher was the smallest at just over 5k of code.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 08-28-2012 at 10:42 PM..
felgall is offline   Reply With Quote
Old 08-28-2012, 11:07 PM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yes, I oversimplified. But he *did* say he wanted "just a stepp[ing] stone."

Not at all familiar with Rabbit Cipher, but from my quick reading of it, it looks like it uses the same key for encryption and decryption. How is that secure in a JavaScript implementation? I would think you would at least need a public key/private key system if you wanted to do it in JS?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-29-2012, 09:05 AM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
This very simple script might help move you forward.

Code:
<html>
<head>
</head>
<body>

INPUT <input type = "text" id = "in" size = "50" onkeyup = "encrypt()">
<br>
OUTPUT <input type = "text" size = "50" id = "out" readonly>

<script type = "text/javascript">

var A = " abcdefghijklmnopqrstuvwxyz";
var B = " qwertyuiopasdfghjklzxcvbnm";  // the corresponding characters
var Alen = A.length;

function encrypt() {
var enctext = "";

var txtin = document.getElementById("in").value.toLowerCase();  // make all lower case
var Tlen = txtin.length;
for (var j = 0;  j < Tlen; j++) {
for (var i = 0;  i < Alen; i++) {
if (A.charAt(i) == txtin.charAt(j)) {
enctext += B.charAt(i);
}
}
}

document.getElementById("out").value =  enctext;
}


</script>

</body>
</html>
But once again, only YOU will be able to see the result!

You will find more complicated substitution ciphers at

http://www.java2s.com/Code/JavaScrip...JavaScript.htm
__________________

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.

Last edited by Philip M; 08-29-2012 at 09:15 AM..
Philip M is offline   Reply With Quote
Old 08-29-2012, 02:33 PM   PM User | #12
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
One thing you need to understand: If you write your encryption using JavaScript, then ANYONE who can access your web page can look at the code and see what you have done and then easily decrypt your encrypted text.
not even remotely true. encryption is about the math and physically protecting the secret key. javascript does math just like any other language.

so if ANYONE can do it, here's the full source, i'll be darned impressed if YOU can provide me a clear text copy of either the password or the message:

Code:
<html><title>Encoded Message</title><body style='margin: 0px; overflow:hidden; position:absolute; width:100%; height: 100%; white-space: pre;' ><textarea  id='t1' name='t1' rows='50' cols='210'  style='font-family:Tahoma, sans-serif; font-size:120%; position:absolute; left:0px; top: 0px; width:100%; height:100%; wrap: virtual'></textarea> <script> eval( unescape( "function%20jcipher%28p%2Cs%29%7Bvar%20i%3D0%2CP%3D0%2CK%3D0%2Cb%3D%22%22%2CMax%3D0%2Cd%3D%5B%5D%3Bif%28p.slice%280%2C3%29%3D%3D%22zz%2C%22%29%7Bvar%20slen%3Ds.length+1%3Bd%3Dp.split%28%22%2C%22%29%3Bp%3D%22%22%3Bvar%20junk%3Dd.shift%28%29%2CScc%3DString.fromCharCode%3BMax%3Dd.length%3Bvar%20tr%3D%5BMax%5D%3Bfor%28var%20i%3D0%3Bi%3CMax%3Bi++%29%7BP%3Dd%5Bi%5D%3BK%3Ds.charCodeAt%28i%25slen%29%3Btr%5Bi%5D%3DScc%28P%5EK%29%3B%7Dreturn%20tr.join%28%22%22%29%3B%7Dreturn%20false%3B%7D%0A" ) ); 
var enc='zz,11 ,13,13,2,8,69,26,10,114,15,12'
 if (typeof PW == 'undefined'){var PW = prompt('Enter The Password for this Document:')};
 if (PW.length){ document.getElementById('t1').value=jcipher(enc, PW); };
</script></body></html>
it's tongue in cheek demo data, guessable perhaps, but not crackable.

this was created using my old crypto project, viewable at http://danml.com/pub/crypto.htm ...

the cool thing about the cipher, based off of a KGB description, is that a succsessful decryption cannot be proven; you can't know that the message you "found" is the one actually sent.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 08-29-2012, 04:42 PM   PM User | #13
Richter
New Coder

 
Join Date: Jun 2012
Posts: 63
Thanks: 0
Thanked 11 Times in 11 Posts
Richter is an unknown quantity at this point
To rnd me,
Your encryption function seem good but is it fix length of password ?

Code:
var enc='zz,11 ,13,13,2,8,69,26,10,114,15,12';
var PW = "Testing encode password";
var Enc = jcipher(enc, PW); //_h~va+}*\x17ao
var Dec = jcipher(enc, Enc); //"Testing enc"
Richter is offline   Reply With Quote
Old 08-29-2012, 08:14 PM   PM User | #14
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
It's a good one, rndme. I think I could crack it, but it would take a lot of time.

I think perhaps I need to clarify what I've been trying to say: If the code *ALONE* can encrypt/decrypt something, then it's insecure. Because for that to happen, the keyword must be embedded somewhere in the code.

Even the Vigenere cipher is strong enough for all practical purposes if the user must supply the key (and if the key is long enough).
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-30-2012, 01:22 AM   PM User | #15
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by Richter View Post
To rnd me,
Your encryption function seem good but is it fix length of password ?

Code:
var enc='zz,11 ,13,13,2,8,69,26,10,114,15,12';
var PW = "Testing encode password";
var Enc = jcipher(enc, PW); //_h~va+}*\x17ao
var Dec = jcipher(enc, Enc); //"Testing enc"
no, the password repeats as needed. that's why long passwords are better.
i like to use guids because it's hard to see/detect even sucsessful decypts of the password, since it still looks like random data...

you are not using the code correctly in the snip above.
i wrote it before there were projects like crypto-js, so the argument order is different than the defacto standard of passing the encrytpted text as a key.

anyway, it works just fine once you know, and you were close. p is the phrase, and s is the secret password. it needs a slight cosmetic re-write i guess...


here is an example of using a longer phrase than password: (live demo)
Code:
function jcipher(p,s){var i=0,P=0,K=0,b="",Max=0,d=[];if(p.slice(0,3)=="zz,"){var slen=s.length + 1;d=p.split(",");p = "";var junk=d.shift(),Scc=String.fromCharCode; Max=d.length;var tr = [Max];for(var i=0;i<Max;i++) {P = d[i];K = s.charCodeAt(i % slen);tr[i]=Scc(P ^ K);}return tr.join("");}else{var slen=s.length+1;b="zz,";Max=p.length;var tr=[Max];for(i=0;i<Max; i++){P=p.charCodeAt(i);K=s.charCodeAt(i%slen);tr[i]=P ^ K;if(!(i % 40)){tr[i]+=" ";}}return b+tr.join(",");}return false;}

var data="this is my secret data. can you hear me now?";
var PW = "test123";

var enc = jcipher(data, PW); 
// enc==zz,0 ,13,26,7,17,91,64,32,25,28,83,7,84,81,65,101,0,69,23,21,69,83,29,32,23,4,29,84,72,93,70,32,28,0,18,6,17,95,86,32,26 ,10,4,75

jcipher(enc, PW); // == "this is my secret data. can you hear me now?"
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Tags
encrypt, encryption, for looping

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:06 AM.


Advertisement
Log in to turn off these ads.