Solved

javascript encryption

Posted on 2006-11-22
10
226 Views
Last Modified: 2008-02-01
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
Comment
Question by:zattz
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 41

Accepted Solution

by:
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>
<head>
<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>  
</head>
<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

by:rama_krishna580
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...
http://javascript.internet.com/passwords/ascii-encryption.html#source

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)))
alert(str)
</script>

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

More info. Use microsoft script encoder, it is free :
http://www.microsoft.com/downloads/details.aspx?FamilyId=E7877F67-C447-4873-B1B0-21F0626A6329&displaylang=en

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

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

Here is a complete tutorial to try out.

<html>
<head>

<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>
</head>
<body>
<form name="fA">
<table cellpadding="5" width="700" cellspacing=0 border=0>
<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

by:Pravin Asar
ID: 18086618
Did you try my solution..

_PA
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:zattz
ID: 18226166
Sorry forgot about this
0
 
LVL 41

Expert Comment

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

Author Comment

by:zattz
ID: 18233516
And to you :>
0
 

Author Comment

by:zattz
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

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sums of coloumns in html/java 15 68
How to drill down using class name and find the text? 25 34
Poster Video HTML 5 Scale to fit video 3 38
JavaScript: Issue with onClick 5 36
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now