【发布时间】: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