RSA Encryption/Decryption using Bouncy Castle
843810Jul 31 2002 — edited Aug 1 2002Hello,
I am trying to encrypt a String using my private key. I am able to encrypt and decrypt it fine, if the length of th String is 53 or less. However if the length of the string is greater than 53 I get the exception below. The provider I am using is BouncyCastle.
Any help would be greatly appreciated.
Thank you in advance,
Sunit Sheth.
I am getting the following error:
____ ERROR_START__
org.bouncycastle.crypto.DataLengthException: attempt to process message to long
for cipher
at org.bouncycastle.crypto.BufferedAsymmetricBlockCipher.processBytes(Bu
fferedAsymmetricBlockCipher.java:127)
at org.bouncycastle.jce.provider.JCERSACipher.engineUpdate(JCERSACipher.
java:183)
at javax.crypto.Cipher.update(DashoA6275)
at javax.crypto.CipherOutputStream.write(DashoA6275)
at CryptTester.encryptIt1(CryptTester.java:393)
at CryptTester.main(CryptTester.java:498)
____ ERROR_START__
My code for the encryption is as follows:
__START_CODE__
public String encryptIt1(String toencrypt)
{
try
{
// Encode the string into bytes using utf-8
byte[] plainText = toencrypt.getBytes("UTF8");
ByteArrayOutputStream output = new ByteArrayOutputStream (encryptCipher.getOutputSize(plainText.length));
CipherOutputStream encryptedStream = new CipherOutputStream(output, encryptCipher);
encryptedStream.write(plainText, 0, plainText.length);
encryptedStream.close();
return new sun.misc.BASE64Encoder().encode(output.toByteArray());
}catch (Exception e){ e.printStackTrace();}
return null;
}
__ END CODE __