Reputation: 9138
I have been tinkering with BB RSA Crypto today and managed to encrypt a string successfully (I think, can't decrypt to test). My problem lies in decryption. I've trawled the forums and tried lots of combinations of code but nothing seems to work. All calls to decrypt the cipertext hang/block the app.
Well, i've only tried on the simulator and it blocks for over 10 minutes. I'm assuming that somethings wrong.
Below I am showing my code to encrypt and decrypt a String. Any insight into what is wrong with my decryption procedure would be greatly appreciated. Thanks.
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 ################################
Upvotes: 3
Views: 454
Reputation: 94038
Normally you don't encrypt text directly using an RSA algorithm as block cipher. The usual way is to create a random symmetric/secret key (e.g. AES), then use that to encrypt the plain text. Then you encrypt the AES key using the public key and RSA encryption. You send both the encrypted plain text and the encrypted symmetric AES key to the receiver. The receiver first decrypts the AES key, then the cipher text.
RSA is slow, very slow, especially during decryption. As the public exponent is normally a short number with a few bits set (0x010001
is common, the fourth number of Fermat), so encryption is still pretty fast. Decryption is not, and you will have a rather large overhead since the cipher text will be at least 11 bytes shorter than the plain text, for each encrypted block.
Don't use RSA for block encrypt, use AES CBC with a random IV & PKCS5Padding instead.
Upvotes: 2