RSA加密在Java中,解密在PHP [英] RSA encryption in Java, decryption in PHP

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

问题描述

我试图用加密的RSA公钥和Android应用的AES密钥,然后解密使用PHP与phpseclib服务器上的AES密钥。我已经测试,以确保通过加密一个静态文本事后解密,两个平台上的RSA加密/解密的工作检查,如果我仍然得到原文。根据试验,在RSA code单独工作的每个平台上,但似乎有平台之间的差异。

I am trying to encrypt an AES key with an RSA public key in and Android app and then decrypt the AES key on a server using PHP with phpseclib. I have tested to make sure that the RSA encryption/decryption work on both platforms by encrypting a static text and decrypting it afterwards to check if I still get the original text. According to the test, the RSA code works individually on each platform but there seems to be a difference between the platforms.

在Java中,我使用的充气城堡库,并具有以下code:

In Java, I am using the Bouncy Castle library and have the following code:

RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
new BigInteger("00c897f9e401819e223ffbecc6f715a8d84dce9022762e0e2d54fa434787fcaf230d28bd0c3b6b39b5211f74ffc4871c421362ccfc07ae98b88fa9728f1e26b8210ebbf4981e45867fe810938294d0095d341b646b86dcbd4c246676c203cb1584d01eef0635299714d94fa12933ecd35e6c412573156d9e6e549b7804eb6e165660507d8748bcc8c60da10099bacb94d3f7b50b1883ee108489e0dd97ed7d28e564edd4ee5d6b4225f5c23cdaaf495c3fa08c3b82e1674946e4fa1e79b2493204d6953c261105ba5d0f8dcf3fcd39a51fbc18a5f58ffff169b1bed7ceeded2ae0e8e8e2238e8b77b324d1a482593b1a642e688c860e90d5a3de8515caf384133b", 16),
new BigInteger("11", 16));
keyFactory = KeyFactory.getInstance("RSA", "BC");
//RSAPublicKeySpec rsaKeySpec = new RSAPublicKeySpec(rsaKey.MODULUS, new BigInteger("11", 16));
RSAPublicKey pubKey = (RSAPublicKey)keyFactory.generatePublic(pubKeySpec);  

//Set up the cipher to RSA encryption
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);

// make sure the Aes Key is less than a block size
// otherwise major errors will occur
if(AesKey.length * 8 > pubKey.getModulus().bitLength())
    return "Error: AesKey bigger than block size of RSA Key";

byte[] encryptedKey = cipher.doFinal(AesKey);

// return result Base64 encoded
return Base64.encodeToString(encryptedKey, Base64.DEFAULT);

然后在PHP中,我使用下面的code解密AES密钥:

Then in the PHP, I am using the following code to decrypt the AES key:

$AESkey = base64_decode($AES);

$rsa = new Crypt_RSA();
$private = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/PrivateData/private_key.pem');
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->loadKey($private);
$AESkey = $rsa->decrypt($AESkey);

在AES密钥是由服务器解密,我总是得到以下错误:解密错误在C:\\ XAMPP \\ PHP \\ PEAR \\ phpseclib \\地穴\\ RSA.php在线1911年综观$ C $下在RSA.php,我认为错误的东西已经在加密过程中做的不正确填充,但我似乎无法想出一个办法来解决它。

When the AES key is being decrypted by the server, I always get the following error: Decryption error in C:\xampp\php\PEAR\phpseclib\Crypt\RSA.php on line 1911. Looking at the code in RSA.php, I think the error has something to do with incorrect padding during encryption but I can't seem to figure out a way to fix it.

更新:我想通了,上面的加密/解密code实际上是正确的。我遇到的问题是,我没有从应用程序发送数据到PHP等等一些信息被丢失之前url可连接code的输出。

Update: I figured out that the above encryption/decryption code is actually correct. The problem I was having was that I did not url encode the output before sending the data from the app to the php so some of the info was being lost.

推荐答案

您使用的版本phpseclib的?根据不同的版本,下面可能会有帮助:

Which version of phpseclib are you using? Depending on the version the following may help:

http://www.frostjedi.com/phpbb/viewtopic。 PHP?p = 118414#p118414

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

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