RSA加密:用PHP创建密文并用Javascript解密 [英] RSA Encryption: Create a cipher text in PHP and decrypt it in Javascript

查看:75
本文介绍了RSA加密:用PHP创建密文并用Javascript解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在客户端/javascript 中解密在服务器端/PHP 中加密的密文.

I'm having trouble to decrypt a cipher text in client side/javascript which is encrypted in server side/PHP.

对于 PHP 中的加密,我使用了 phpseclib,这是我的示例代码块:

For encryption in PHP, I'm using the phpseclib and here is my sample code block:

<代码>定义( 'PUK', 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDwMKuLMVuo7vHDwFCbpgxx + RNF xSVC2GRWq9rW0fl4snnhRdrpprPRUO1RMV0wA/AO + fvzi1Sl0lcws3srpe28iLAj Wh5vFM/pFstwzjoHBv/6n4rQouIVy2NT18aFrxlQUCs4DHy5eOo21MjQXrgHwCeP HEc7OK + VpaQ/yKKX8wIDAQAB');包括 ('Crypt/RSA.php');$rsa = 新的 Crypt_RSA();$plaintext = '我的测试消息';$rsa ->加载密钥(PUK);$ciphertext = $rsa ->加密($纯文本);//echo $ciphertext;//这也不行!//echo strrev(base64_encode($ciphertext));//这也不起作用!参考:http://www.frostjedi.com/phpbb3/viewtopic.php?f=46&t=141187echo base64_encode($ciphertext);

对于客户端/Javascript 中的解密,我使用jsencrypt",这是代码块:

For decryption in client side/Javascript, I'm using "jsencrypt" and here is the code block:

var decrypt = new JSEncrypt();
decrypt.setPrivateKey('MIICXQIBAAKBgQDwMKuLMVuo7vHDwFCbpgxx+RNfxSVC2GRWq9rW0fl4snnhRdrp prPRUO1RMV0wA/ao+fvzi1Sl0lcws3srpe28iLAjWh5vFM/pFstwzjoHBv/6n4rQ ouIVy2NT18aFrxlQUCs4DHy5eOo21MjQXrgHwCePHEc7OK+VpaQ/yKKX8wIDAQAB AoGBAL2EuaZvwLIwL6VUVoYp5AH+FVJo3Ti8Q5e7rEX6kgyxTsf4dX4NIi9T2p1J BQ2A4xx7e1i0pIreyBtOUy6ik0y7e3MlmZidG91pz2KllQqwAMKrOZgPTBWBF7fr xIZERfOlZcIRrqp8ECbeHDyO6fUbfQm+o7vkxMypwjixBslJAkEA+mF8Sxvw+7D6 ntev+XsYj9Xp4wumqR2hK4WcXAAWbFmcd29tgTMKfcgw0Ru6FCGQdUvqu61PniS4 ie+u6zPORwJBAPWUos5KvEixkgSUY0PZOQavRwoXS1GEEvkjlFOyqWqUiKViT9iy UsXKxk3NAVMqIdF5RdAQ/ob9NxtxiuSxYvUCQQDUfFsBWwsebsmieCVNslvb5YyC NOcRaqXWy6MwqJpfBYW2Doh+NxTWPki/japTX1C7WtwwvhpteXhrB1AJJ4QNAkB1 RrsM6vHJgUsq9rYE07qA77lsHz2vuvPYmF4gLkTrie1LlYxt/pK6tCBJTSphzdAC mfh16XezfT8Q0wMyPWf1AkAxS//2T3J1g+dbG9dEKREcpwANxlFIEnOm9XsFd2vO I6Jr0ksaS4o0IeUBDWmMFOgCWVPdJkGrlqlVPQ6P9owA');
var uncrypted = decrypt.decrypt(ciphertext); //The ciphertext is obtained from the server by an AJAX call.

但是发现未加密"总是为空!

BUT the "uncrypted" is found always to be null!

请注意,如果我在 phpseclibjsencrypt 中使用提到的私钥/公钥对,那么它工作正常.问题是只在 PHP 加密和 Javascript 解密上创建.

Please note that if I use the mentioned Private/Public key pair either in phpseclib or jsencrypt then it is working fine. The problem is creating only on encryption in PHP and decryption in Javascript.

如果有人能在这方面帮助我,那将是非常值得赞赏的.

It will be really appreciable if anyone can help me on this regard.

推荐答案

如果你用 Javascript 加密并尝试用 PHP 解密会发生什么?这行得通吗?

What happens if you encrypt in Javascript and try to decrypt in PHP? Does that work?

如果没有,那么如果您可以发布由 Java 生成的密文,那会很有帮助.这将使我们有机会分解明文的编码并查看正在使用哪种填充方法(如果有).

If not then if you could post the ciphertext produced by Java that'd be helpful. That'd give us a chance to breakdown the encoding of the plaintext and see which padding method - if any - is being used.

也就是说,我的猜测是,您需要执行 $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1).

That said, in lieu of having that, my guess, off hand, would be that you need to do $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1).

这篇关于RSA加密:用PHP创建密文并用Javascript解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆