有没有办法在 jQuery 中加密?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3486465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to encrypt in jQuery?
提问by Asaf
How do I encrypt something in jQuery?
I want to have the option to encrypt via
SHA1 or MD5.
如何在 jQuery 中加密某些东西?
我希望可以选择通过
SHA1 或 MD5进行加密。
How do I do that?
我怎么做?
采纳答案by Haim Evgi
have list of plugins in this link :
在此链接中有插件列表:
http://www.jquery4u.com/security/10-jquery-security/
http://www.jquery4u.com/security/10-jquery-security/
example to md5 :
以 md5 为例:
回答by RandyMohan
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 '';
}
}
回答by Nick Craver
This isn't a direct answer to the question, but considerations you should take into account with the overall approach:
这不是对问题的直接回答,而是您应该在整体方法中考虑的注意事项:
Though you coulddo somethingin jQuery, you should use SSLif at all possible, this is a much more secure way of passing information back and forth to the server if that's your goal.
尽管您可以在 jQuery 中做一些事情,但您应该尽可能使用 SSL,如果这是您的目标,那么这是一种更安全的将信息来回传递到服务器的方式。
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.
通过 JavaScript 加密内容但仍以纯文本形式发送它确实无法阻止中间人攻击,而有些人认为这种情况很少见,因为您必须控制连接中的某个点......有多少人使用第三方 WiFi、咖啡馆等?任何有公共热点的地方对于中间人来说都是容易的游戏,只是要记住一些事情。
回答by johans
You can use this proven library: http://pajhome.org.uk/crypt/md5
您可以使用这个经过验证的库:http: //pajhome.org.uk/crypt/md5
It's not a jQuery plugin - just call the functions as needed.
它不是 jQuery 插件 - 只需根据需要调用函数。