2012-03-02 77 views
2

我今天一直在修补BB RSA Crypto并成功地加密了一个字符串(我认为,无法解密测试)。我的问题在于解密。我已经浏览论坛并尝试了很多代码组合,但似乎没有任何工作。所有调用解密cipertext挂起/阻止应用程序。黑莓RSA解密总是挂起

那么,我只在模拟器上尝试过,它会阻塞超过10分钟。我假设有些事情是错的。

下面我显示了我的代码来加密和解密一个字符串。任何有关我的解密程序有什么问题的见解都将不胜感激。谢谢。

  cryptoSystem = new RSACryptoSystem(1024); 
     byte[] expo = Base64InputStream.decode(exponent, 0, exponent.length()); 
     byte[] modul = Base64InputStream.decode(modulus, 0, modulus.length()); 


     byte[] pArr = Base64InputStream.decode(p, 0, p.length()); 
     byte[] qArr = Base64InputStream.decode(q, 0, q.length()); 
     byte[] dpArr = Base64InputStream.decode(dp, 0, dp.length()); 
     byte[] dqArr = Base64InputStream.decode(dq, 0, dq.length()); 
     byte[] inverseQArr = Base64InputStream.decode(inverseQ, 0, inverseQ.length()); 
     byte[] dArr = Base64InputStream.decode(d, 0, d.length()); 


     // Public Key Setup 
     RSAPublicKey publicKey = new RSAPublicKey(cryptoSystem, expo, modul); 
     RSAEncryptorEngine eEngine = new RSAEncryptorEngine(publicKey); 
     fEngine = new PKCS1FormatterEngine(eEngine); 

     // Private Key Setup 
     RSAPrivateKey privateKey = new RSAPrivateKey(cryptoSystem, expo, pArr, qArr, dpArr, dqArr, inverseQArr); 
     dEngine = new RSADecryptorEngine(privateKey); 
     ufEngine = new PKCS1UnformatterEngine(dEngine); 

     // ################################ ENCRYPTION ################################ 
     BlockEncryptor cryptoStream = new BlockEncryptor(fEngine, out); 
     cryptoStream.write(data, 0, data.length); 
     cryptoStream.close(); 
     out.close(); 
     // ################################ END ENCRYPTION ################################ 

     // Convert encrypted bytes to text; 
     int finalLength = out.size(); 
     byte[] cipherText = new byte[finalLength]; 
     System.arraycopy(out.getByteArray(), 0, cipherText, 0, finalLength); 
     cipherText = out.toByteArray(); 

     // ################################ DECRYPTION ################################ 
     ByteArrayInputStream inputStream = new ByteArrayInputStream(cipherText); 
     byte[] plainText = new byte[finalLength]; 
     BlockDecryptor decryptor = new BlockDecryptor(new PKCS1UnformatterEngine(new RSADecryptorEngine(privateKey)), inputStream); 
     decryptor.read(plainText, 0, finalLength); // THIS HANGS APP 
     //IOUtilities.streamToBytes(decryptor); // AND ALSO THIS 

     String strPlaintText = new String(plainText); 
     // ################################ END DECRYPTION ################################ 
+2

hi conor。我建议你使用bouncycastle,因为BB库有很多意想不到的行为:只需要在项目中为j2me导入bouncycastle。 – rosco 2012-03-02 19:49:25

+0

我开始认为我自己。谢谢。 – conor 2012-03-02 20:14:05

回答

2

通常情况下,您不会直接使用RSA算法作为分组密码来加密文本。通常的方法是创建一个随机对称/秘密密钥(例如AES),然后用它来加密纯文本。然后使用公钥和RSA加密来加密AES密钥。您将加密的纯文本和加密的对称AES密钥发送给接收方。接收机首先解密AES密钥,然后解密密文。

RSA很慢,速度很慢,特别是在解密过程中。由于公开指数通常是一个短数字,并且有一些比特设置(0x010001是常见的,费马的第四个数字),所以加密仍然非常快。解密不是,并且你将有一个相当大的开销,因为对于每个加密块,密文至少比纯文本短11个字节。

不要使用RSA进行块加密,而是使用带有随机IV的AES CBC &而不是PKCS5Padding。

+0

听起来像一些伟大的建议。这是一个大学项目,所以如果我有时间我会执行它。干杯! – conor 2012-03-02 21:51:06

+0

欢迎:) – 2012-03-02 23:40:36

+0

一个接受将是非常好的,科尔... – 2012-03-08 20:13:32