[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

javascript encryption

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
zattz
Asked:
zattz
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
HonorGodCommented:
<!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
 
rama_krishna580Commented:
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
 
Pravin AsarCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Pravin AsarCommented:
Did you try my solution..

_PA
0
 
zattzAuthor Commented:
Sorry forgot about this
0
 
HonorGodCommented:
Thanks for the 'A', good luck, and Happy New Year.
0
 
zattzAuthor Commented:
And to you :>
0
 
zattzAuthor Commented:
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
 
HonorGodCommented:
Well, I try to be polite, and find that it is a good habit to get into.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now