【问题标题】:Blackberry RSA Decryption always hangs黑莓 RSA 解密总是挂起
【发布时间】:2012-03-21 06:48:48
【问题描述】:

我今天一直在修补 BB RSA Crypto 并成功加密了一个字符串(我认为,无法解密进行测试)。我的问题在于解密。我浏览了论坛并尝试了很多代码组合,但似乎没有任何效果。所有解密密文的调用都会挂起/阻止应用程序。

嗯,我只在模拟器上试过,它阻塞了 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 ################################

【问题讨论】:

  • 嗨康纳。我建议你使用 bouncycastle,因为 BB 库有很多意想不到的行为:只需在你的项目中为 j2me 导入 bouncycastle。
  • 我自己也开始这么想了。谢谢。

标签: blackberry encryption cryptography rsa


【解决方案1】:

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

RSA 很慢,非常慢,尤其是在解密期间。由于公共指数通常是一个设置了几个位的短数字(0x010001 是常见的,费马的第四个数字),所以加密仍然很快。解密不是,而且您将有相当大的开销,因为对于每个加密块,密文将至少比纯文本短 11 个字节。

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

【讨论】:

  • 听起来不错的建议。这是一个大学项目,所以如果我有时间,我会实施它。干杯!
  • 好吧,我一直在等待我的问题的正确答案。我赞成你提供了一些好的建议。但是,我认为这不是我问题的答案。我想我有一个解决方案,但我只需要测试它。稍后我会回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
相关资源
最近更新 更多