Javascript 和 PHP 加密/解密
基本上,我有一个带有登录信息的ajax表单,有什么方法可以在ajax发送密码之前对其进行加密,然后在php中解密吗?
或者我应该以其他方式看待它?
非常感谢:)
Basically, I have an ajax form that carries login information, is there any way I can encrypt the password before it sends in ajax then decrypt it in php?
Or any other ways I should look at it?
Many thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有理由在 JavaScript 或 PHP 中进行任何加密,因为适当的解决方案是使用 SSL (HTTPS)。现在您甚至可以获得所有浏览器都信任的证书免费 因此没有理由不使用 SSL。
如果您由于某种原因无法使用 SSL,您可以获取 RSA 的 JavaScript 实现,这样您就可以在本地对其进行加密,但只有您的服务器能够再次对其进行解密。
There is no reason to do any encryption in JavaScript or PHP as the appropriate solution is to use SSL (HTTPS). Nowadays you can even get certificates which are trusted in all browsers for free so there's no reason for not using SSL.
If you cannot use SSL for some reason, you could get a JavaScript implementation of RSA so you can encrypt it locally but only your server is able to decrypt it again.
你可以使用 RC4,因为我知道它有 PHP 和 Javascript 的实现。但是,使用任何类型的加密,您都必须保留密钥客户端(以便它可以对其进行加密),这意味着有权访问您的页面的任何人都可以获取密钥并对其进行解密(从而破坏了这一点)。
您可能最好在客户端对其进行散列(然后在 PHP 中匹配散列,如果您不需要知道密码),或者使用公钥-私钥加密(如 RSA),以便客户端可以加密,但不解密它。
对于哈希,请查看 hash() 和 Javascript 的 sha1。
对于 RSA,请查看此博客文章 http://www.sematoopia.com/2008/10/rsa-encrypting-in-javascript-and-decrypting-in-php/
You could use RC4, since I know theres an implementation of it in PHP and Javascript. However, with any sort of encryption, you'd have to leave the key client side (so it can encrypt it), which means that anyone who has access to your page can get the key and decrypt it (thus defeating the point).
You might be better off either hashing it client-side (and then matching the hashes in PHP, if you don't need to know the password), or using Public-Private key encryption (like RSA), so that clients can encrypt, but not decrypt it.
For hashing, look at hash() and sha1 for Javascript.
And for RSA, check out this blog post http://www.sematopia.com/2008/10/rsa-encrypting-in-javascript-and-decrypting-in-php/
使用 SSL 证书 并通过 HTTPS 从 AJAX 表单发送登录信息。
Use an SSL certificate and send the login over HTTPS from your AJAX form.
你不能以安全的方式。你应该使用 https
You can't in a secure manner. you should use https
你可以在JS和PHP中执行
md5(password)
,然后比较加密后的密码。由于用户名未加密,您可以使用它从PHP中的DB中获取密码,然后对其进行加密。
最好的方法是:
$_SESSION['crypt_key']< /code>,并将其作为 ajax 表单上的隐藏输入发送;
md5(crypt_key + password)
在JS中加密;md5($_SESSION['crypt_key'] . $password)
在 PHP 中加密并比较它们。这样,每个请求都会传输一个不可预测的加密密码。You can do
md5(password)
in both JS and PHP, and then compare the encrypted passwords.As username is not encrypted, you can use it to take the password from DB in PHP, and then encrypt it.
Best way to do that is:
$_SESSION['crypt_key']
, and send it as a hidden input on the ajax form;md5(crypt_key + password)
before sending it;md5($_SESSION['crypt_key'] . $password)
and compare them. This way, every request will transfer an unpredictable crypted password.