RSA加密/解密兼容Javascript和PHP
我想用 Javascript 加密,然后用 PHP 解密。 Javascript 和 PHP 有 RSA 实现,但它们不兼容。 我无法在 PHP 中正确解密用 Javascript 加密的内容。
有谁知道可以同时使用 Javascript 和 PHP 的库/代码吗?
谢谢。
I'd like to encrypt in Javascript and then decrypt in PHP. There are RSA implementations for Javascript and PHP but they are not compatible. I cannot decrypt in PHP correctly what I had encrypted with Javascript.
Does anyone know a library/code that will work both with Javascript and PHP?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试以下简单示例。
它使用开源 javascript 库 https://github.com/ziyan/javascript-rsa
HTML/JAVASCRIPT:
PHP:
享受吧!
Try the following simple example.
It is using a open source javascript library https://github.com/ziyan/javascript-rsa
HTML/JAVASCRIPT:
PHP:
Enjoy!
这是一个 JavaScript RSA 加密库: http://www.ohdave.com/rsa/
并且我认为您可以使用此类来解密生成的加密字符串 - http:// www.phpclasses.org/browse/package/4121.html
让我知道您是否能一起完成这项工作,因为我自己正在研究这个主题(我实际上发现这篇文章自己寻找这个答案:P) 。
编辑:看,我也发现了这个 - http://www.sematopia.com/?p =275 - 似乎也与前两个有关......
Here is an JavaScript RSA encryption library: http://www.ohdave.com/rsa/
And I think you could use something like this class to decrypt the generated encrypted string - http://www.phpclasses.org/browse/package/4121.html
Let me know if you manage get this work together, as I am myself looking into this subject (I actually found this post looking for this answer myself :P ) .
Edit: Look, I've also found this - http://www.sematopia.com/?p=275 - seems related to the previous two as well...
如果您将服务器设置为使用 SSL,那么您可以使用 https 通过 ajax 进行加密传输。 这可能是在 javascript 和 php 之间加密数据的最佳方法。 如果你想自己做,你很有可能会在某个地方搞砸,而且系统也不会安全。
Google 了解如何为您的服务器设置 https。
If you set up your server to use SSL then you can have encrypted transmission via ajax using https. That is probably the best way to encrypt data between javascript and php. If you want to make it yourself there is a big chance you will screw up somewhere and the system wont be secure.
Google on how to set up https for your server.
我假设你有充分的理由这样做,而不是自己做 https,所以我想说,如果你坚持这些标准,你应该能够使用支持这些标准的任何技术轻松解密:即它应该有效,
例如,如果如果您以 PKCS#7 格式加密数据,请确保您的 php 库知道输入数据是 PKCS#7。
还要确保您的加密密钥在服务器和客户端之间没有被扰乱。
您是否尝试使用 javascript 库解密您的数据?
希望这可以帮助...
I assume you have a valid reason to do that other than to do https yourself, so I'd say that if you stick to the standards you should be able to easily decrypt with whatever technology supports these standards : ie it should work
For example if you encrypt your data in PKCS#7 format, be sure that your php library knows that the input data is PKCS#7.
Also be sure that your encryption key is not scrambled between server and client.
Did you try to decrypt your data with your javascript library ?
Hope this can help...
也许您可以通过放置您用于 js 和 php 的代码来提供帮助。
另外,也许你可以更具体地说明为什么需要使用 js 和 php。 也许你可以在使用 js 的地方只使用 php 和 AJAX(查询相同的 php 函数)。
Maybe you can help by putting the code you're using for both js and php.
Also, maybe you could be more specific on why you need to use js and php. Maybe you could use only php, and AJAX (to query the same php function) where you were using js.
我不是一个自吹自擂的人,但我在 github.com 上有一个项目可以执行此功能。
服务器上生成私钥,公钥和 pkcs#7 证书也从私钥派生。 公钥被发送到客户端,此时与指定表单关联的每个表单元素在提交到服务器之前都会被加密。
它 100% OpenSSL 兼容,因为它使用 PHP OpenSSL 扩展来生成、加密和解密数据。
https://github.com/jas-/jQuery.pidCrypt/
该项目不是与 PGP 一样安全,因为在将表单数据发送到服务器之前,JavaScript 不会对电子邮件进行签名和加密,但要加密和/或签名的表单数据在发送到服务器之前会使用 RSA 公钥加密进行加密。
同样,该项目在身份验证和电子邮件签名方面并不完整,但对于使用公钥的普通形式加密,它效果很好。
I am not one to toot my own horn but I have a project at github.com that will perform this functionality.
A private key is generated on the server, a public key and pkcs#7 certificate is also derived from the private key. The public key is sent to the client at which time each form element assocated with the specified form is encrypted prior to being submitted to the server.
It is 100% OpenSSL compatibile as it uses the PHP OpenSSL extension to generate, encrypt and decrypt data.
https://github.com/jas-/jQuery.pidCrypt/
This project is not as secure as PGP because the JavaScript will not sign and encrypt emails until the form data is sent to the server, but the form data that is to be encrypted and or signed is encrypted using RSA public key encryption prior to being sent to the server.
Again the project is not complete in terms of the authentication and email signing but for ordinary form encryption using a public key it works very well.
我找到了这个 jsencrypt 库(http://travistidwell.com/jsencrypt),经过 2 天的尝试,我得到了我的解决方案。
我遇到的唯一问题是当我发送长短信时。 这是因为根据定义,RSA 支持有限长度的字符串。
https: //security.stackexchange.com/questions/33434/rsa-maximum-bytes-to-encrypt-comparison-to-aes-in-terms-of-security/33445#33445
IE
如果我使用 1024 的 private_key_bits,我就无法
再发送任何内容。
如果我使用 512 的 private_key_bits,我就无法
再发送任何内容。
在长字符串 JavaScript 控制台报告:“消息对于 RSA 来说太长”
然后,如果你想加密长字符串,你必须在 javascript 加密之前压缩和拆分它们,并在解密之后在 php 上加入和解压缩,我认为 zlib 是 split/ 的一个很好的解决方案加入,因为它支持 javascript 和 php。
我的工作代码如下:
目录树必须如下所示:
以及公共区域之外的 php 可写的目录
I find this jsencrypt library (http://travistidwell.com/jsencrypt), after 2 days trying I got my solution.
The only problem I got is when I send a long text. That's because RSA, by definition, supports strings of limited lenght.
https://security.stackexchange.com/questions/33434/rsa-maximum-bytes-to-encrypt-comparison-to-aes-in-terms-of-security/33445#33445
i.e.
If I use private_key_bits of 1024 I can send
nothing longer.
If I use private_key_bits of 512 I can send
nothing longer.
On long strings JavaScript console reports: "Message too long for RSA"
Then if you want to encrypt long strings you must compress and split them before javascript encryption and after decryption join and uncompress on php, I think zlib is a good solution for split/join because it is supported on javascript and php.
My working code is as follows:
Directory tree must looks like:
and a directory writable by php outside of public zone named