Solved

javascript encryption

Posted on 2006-11-22
10
223 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: DanRollins
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…

707 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

15 Experts available now in Live!

Get 1:1 Help Now