2012-03-02 76 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