www.webdeveloper.com
Results 1 to 2 of 2

Thread: Encrypt/Decrypt Password in Javascript

  1. #1
    Join Date
    Apr 2005
    Posts
    22

    Encrypt/Decrypt Password in Javascript

    This is going to sound really weird, but here goes. I need a way to encrypt a password via php (I'm not worried about the encrypting, just the javascript required to decrypt it) to store into a bookmarklet. The javascript bookmarklet will then need to decrypt the password before it stores it in the field. Obviously a hacker could still get the password, but I'm just trying to do this to prevent hackers from quick looks at the password being stored in clear text.
    The easier the better. The only thing it needs to do is make a password like "password", not look like "password" when in the javascript bookmarklet. FYI, this is being used mainly for a autofill script for an iphone/ipod touch safari.

    Thanks,
    Marc

  2. #2
    Join Date
    Feb 2008
    Posts
    1,666
    Try this out:
    Code:
    String.prototype.toEncodedString = function(){var ostr=this.toString().replace(/\s+/g,'');if(ostr.length<8){alert("Password must be at least 8 characters long with no spaces.");return null;};var x,nstr='',len=ostr.length;for(x=0;x<len;++x){nstr+=(255-ostr.charCodeAt(x)).toString(36).toUpperCase().toPaddedString(2,'0');};return nstr;};
    String.prototype.fromEncodedString = function(){var ostr=this.toString();var x,nstr='',len=ostr.length;for(x=0;x<len;x+=2){nstr+=String.fromCharCode(255-parseInt(ostr.substr(x,2),36));};return nstr;};
    Number.prototype.toPaddedString = function(len,pad){len=(len)?Number(len):2;if(isNaN(len)){alert("Padded String 'length' argument is not numeric.");return null;};var dflt=(isNaN(this.toString()))?" ":"0";pad=(pad)?pad.toString().substr(0,1):dflt;var str=this.toString();if(dflt=="0"){while(str.length<len)str=pad+str;};else{while(str.length<len)str+=pad;};return str;};
    String.prototype.toPaddedString = Number.prototype.toPaddedString;
    //
    var str = window.prompt('Enter string to encode:','');
    if (str = str.toEncodedString())
    {
    	str = window.prompt('Enter encoded string:',str);
    	str = str.fromEncodedString();
    	alert(str);
    }
    else
    {
    	alert('Encoding cancelled.');
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles