Solved

# javascript encryption

Posted on 2006-11-22
223 Views
Hi,

I found the following function that decrypts something. I'm not sure how this works. Can somebody write a function that does the reverse. IE: one that encrypts

The decryption function is:

function hdecrypt(s){var o="",ar=new
Array(),os="",ic=0,p=0;
for(i=0;i<s.length;i++){c=s.charcodeat(i);
if (c<128)c=c^((p++%8)+1);os+=String.fromCharCode(c);
if(os.length>80){ar[ic++]=os;os=""}}o=ar.join("")+os;return o}
0
Question by:zattz
• 3
• 3
• 2
• +1

LVL 41

Accepted Solution

HonorGod earned 300 total points
ID: 17997995
<!doctype html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<title> Encrypt/Decrypt </title>
<script type='text/javascript'>
function hdecrypt( s ) {
var o = os = '', ar = [], p = 0;
for( i = 0; i < s.length; i++ ) {
c = s.charCodeAt( i );
if ( c < 128 ) c = c ^ ( ( p++ % 8 ) + 1 );
os += String.fromCharCode( c );
if ( os.length > 80 ) {
ar.push( os );
os = '';
}
}
o = ar.join( '' ) + os ;
return o
}

function fromTo( id1, id2 ) {
var txt = document.getElementById( id1 ).value;
document.getElementById( id2 ).value = hdecrypt( txt );
}
</script>
<body>
<form action=''>
Input:   <input id='in' type='text' size='80'></input><br>
Encrypt: <input id='en' type='text' size='80' readonly></input><br>
Decrypt: <input id='de' type='text' size='80' readonly></input><br>
<input type='button' value='encrypt' onclick='fromTo("in","en")'></input><br>
<input type='button' value='decrypt' onclick='fromTo("en","de")'></input><br>
</form>
</body>
</html>
0

LVL 23

Expert Comment

ID: 17997997
Hi,

I guess this is not going to be possible.  you can create a routine that encrypts and decrypts using Javascript, that bit is no problem.  Where are you going to store your encryption key though?  Javascript is not compiled and therefore anyone can see your code.  You could then view the code find the key and crack the password very very easily.

But try these below options...

XOR Encryption: Sample Code

<script>
str=''
str=str+String.fromCharCode('Q'.charCodeAt(0)^((0x05).toString(10)))
str=str+String.fromCharCode('P'.charCodeAt(0)^((0x38).toString(10)))
str=str+String.fromCharCode('j'.charCodeAt(0)^((0x03).toString(10)))
str=str+String.fromCharCode('h'.charCodeAt(0)^((0x1B).toString(10)))
str=str+String.fromCharCode('4'.charCodeAt(0)^((0x14).toString(10)))
str=str+String.fromCharCode('3'.charCodeAt(0)^((0x5A).toString(10)))
</script>

http://guymal.com/mycode/xor_js_encryption/

FOR Netscape/Mozilla there is no encryption only scrambeling :
http://www.shopable.co.uk/scrambler.html
http://www.brainjar.com/js/crunch/

R.K
0

LVL 28

Assisted Solution

Pravin Asar earned 200 total points
ID: 17998774
This is the one I use.

Here is a complete tutorial to try out.

<html>

<script language="javascript">

var encN=1;
// DECODES AND UNESCAPES ALL TEXT.
function decodeTxt(s){
var s1=unescape(s.substr(0,s.length-1));
var t='';
for(i=0;i<s1.length;i++)t+=String.fromCharCode(s1.charCodeAt(i)-s.substr(s.length-1,1));
return unescape(t);
}

// ENCODES, IN UNICODE FORMAT
function encodeTxt(s){
s=escape(s);
var ta=new Array();
for(i=0;i<s.length;i++)ta[i]=s.charCodeAt(i)+encN;
return ""+escape(eval("String.fromCharCode("+ta+")"))+encN;
}

// CONVERTS *ALL* CHARACTERS INTO ESCAPED VERSIONS.
function escapeTxt(os){
var ns='';
var t;
var chr='';
var cc='';
var tn='';
for(i=0;i<256;i++){
tn=i.toString(16);
if(tn.length<2)tn="0"+tn;
cc+=tn;
chr+=unescape('%'+tn);
}
cc=cc.toUpperCase();
os.replace(String.fromCharCode(13)+'',"%13");
for(q=0;q<os.length;q++){
t=os.substr(q,1);
for(i=0;i<chr.length;i++){
if(t==chr.substr(i,1)){
t=t.replace(chr.substr(i,1),"%"+cc.substr(i*2,2));
i=chr.length;
}
}
ns+=t;
}
return ns;
}

// SIMPLY UNESCAPES TEXT (ONLY INCLUDED TO MAKE A COMPLEMENTARY FUNCTION FOR escapeTxt()
function unescapeTxt(s){
return unescape(s);
}

// WRITES THE DECODED STRING (s) TO THE DOCUMENT WHEREVER THIS FUNCTION IS CALLED
function wF(s){
document.write(decodeTxt(s));
}
</script>
<body>
<form name="fA">
<tr align="center" valign="top">
<td class="tblc" width="350">
Normal Text/HTML/JavaScript &nbsp;
<input type="button" onclick="document.forms['fA'].f1.focus();document.forms['fA'].f1.select();" value="Select all">
<input type="button" onclick="document.forms['fA'].f1.value='';" value="Clear All">
<br><br>
<textarea id="f1" cols=30 rows=10 wrap="off">
<script language="javascript">
</script>
</textarea>
</td>
<td>
<br><br><br><br>
<input type="button" value="  -->  " onclick="document.fA.c1.value=escapeTxt(document.fA.f1.value)"><br><br>
<input type="button" value="  <--  " onclick="document.fA.f1.value=unescapeTxt(document.fA.c1.value)">
</td>
<td class="tblc" width="350">
Escaped Text/HTML/JavaScript
<br>
<input type="button" onclick="document.forms['fA'].c1.focus();document.forms['fA'].c1.select();" value="Select all">
<input type="button" onclick="document.forms['fA'].c1.value='';" value="Clear All">
<br>
<br>
<textarea id="c1" cols=30 rows=10>
</textarea>
</td>
</tr>
</table>
</form>
</body>
</html>
0

LVL 28

Expert Comment

ID: 18086618
Did you try my solution..

_PA
0

Author Comment

ID: 18226166
0

LVL 41

Expert Comment

ID: 18232453
Thanks for the 'A', good luck, and Happy New Year.
0

Author Comment

ID: 18233516
And to you :>
0

Author Comment

ID: 18233517
I was surprised you thanked me for the 'A'. Usually I only get people complaining when I give a C! The other day somebody gave me a one line answer for a 500 point question and expected an A!
0

LVL 41

Expert Comment

ID: 18234996
Well, I try to be polite, and find that it is a good habit to get into.
0

## Featured Post

This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…