Click here to Skip to main content
14,454,945 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi..

I want to encrypt a string and later decrypt it in other page using javascript ..please can u send me any sample project for this
Posted
Updated 26-Oct-19 2:04am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Why does every one thing the way to program is to ask for 'sample projects' to copy ? What is the sense in this ? If you're doing it on the client, then the client has the source code AND can step through it in Chrome, at least. So, anyone can see the strings you're encrypting, and how they are encrypted and decrypted. It's a total waste of time.
   
Comments
Menon Santosh 13-Dec-12 4:10am
   
my +5
Rate this:
Please Sign up or sign in to vote.

Solution 5

To do such a thing you'd do something very simple, it's only 7 lines.
var chars = {'a':'b','b':'c','c':'a'};
var enc;
var str = "back at it again with the bois!";
window.onload = function() {
enc = str.replace(/[abc]/g, m => chars[m]);
alert(enc);
};

this will result in "cbak bt it bgbin with the cois!"
   
Comments
CHill60 15-Nov-17 9:48am
   
See the comments and solutions above - there is no point as the client will be able to see how the encryption works!
Richard Deeming 15-Nov-17 12:37pm
   
Even without being able to see and debug the source code, this simple variant of the Caesar-shift would be cracked within seconds.

The comments and solutions posted FIVE YEARS AGO already explain why this is a bad idea.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi,

You can try like this.

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.');
}


For reference view this link.
Encript and decript a string

Encription and decription

Thanks
   
Comments
Christian Graus 13-Dec-12 4:12am
   
And of what value is it ? Can I not set a breakpoint to step through this code, and see what's being encoded and decoded ? Can't I read the code and know exactly how to decode the string ?
[no name] 13-Dec-12 4:57am
   
Yes you can...
Put a debugger at any point in the code and get your control to that place for debugging.
fjdiewornncalwe 13-Dec-12 12:58pm
   
Christian's point is that if a client can step through the code in a debugger, and that code's purpose is encryption, the entire purpose of the encryption is rendered pointless.
[no name] 13-Dec-12 13:17pm
   
Yes I can understand the point. This is not acceptable. As there is security issues in javascript its not at all advisable to use the encription and decription using javascript. Serverside encription and decription are prefered.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100