Reputation: 8216
How do I encrypt something in jQuery?
I want to have the option to encrypt via
SHA1 or MD5.
How do I do that?
Upvotes: 5
Views: 53692
Reputation: 657
function Encrypt(str) {
if (!str) str = "";
str = (str == "undefined" || str == "null") ? "" : str;
try {
var key = 146;
var pos = 0;
ostr = '';
while (pos < str.length) {
ostr = ostr + String.fromCharCode(str.charCodeAt(pos) ^ key);
pos += 1;
}
return ostr;
} catch (ex) {
return '';
}
}
function Decrypt(str) {
if (!str) str = "";
str = (str == "undefined" || str == "null") ? "" : str;
try {
var key = 146;
var pos = 0;
ostr = '';
while (pos < str.length) {
ostr = ostr + String.fromCharCode(key ^ str.charCodeAt(pos));
pos += 1;
}
return ostr;
} catch (ex) {
return '';
}
}
Upvotes: 10
Reputation: 125604
have list of plugins in this link :
http://www.jquery4u.com/security/10-jquery-security/
example to md5 :
https://github.com/gabrieleromanato/jQuery-MD5
Upvotes: 3
Reputation: 630597
This isn't a direct answer to the question, but considerations you should take into account with the overall approach:
Though you could do something in jQuery, you should use SSL if at all possible, this is a much more secure way of passing information back and forth to the server if that's your goal.
Encrypting content via JavaScript but still sending it in plain-text really does nothing to thwart man-in-the-middle attacks, which while some people imagine to be rare because you must control some point in the connection...how many people use third-party WiFi, at the coffee house, etc? Anywhere with a public hotspot is easy game for a man-in-the middle, just something to keep in mind.
Upvotes: 3
Reputation: 16
You can use this proven library: http://pajhome.org.uk/crypt/md5
It's not a jQuery plugin - just call the functions as needed.
Upvotes: 0