org.bouncycastle.crypto.DataLengthException problem.
I'm getting this error :
org.bouncycastle.crypto.DataLengthException: attempt to process message to long for cipher
at org.bouncycastle.crypto.BufferedAsymmetricBlockCipher.processBytes(BufferedAsymmetricBlockCipher.java:127)
at org.bouncycastle.jce.provider.JCERSACipher.engineUpdate(JCERSACipher.java:183)
at javax.crypto.Cipher.update(DashoA6275)
at javax.crypto.CipherInputStream.a(DashoA6275)
at javax.crypto.CipherInputStream.read(DashoA6275)
at javax.crypto.CipherInputStream.read(DashoA6275)
at cryptFile.main(cryptFile.java:47)
Exception in thread "main"
When I run the following code:
import java.io.*;
import java.security.*;
import java.security.spec.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.*;
public class cryptFile
{
public static void main(String[] args) throws NoSuchPaddingException,InvalidKeyException,FileNotFoundException,IOException,NoSuchProviderException,InvalidKeySpecException,NoSuchAlgorithmException
{
String homeDir = "g:/java/code";
String fileToEncrypt = homeDir + "/dec.txt";
String encryptedFile = homeDir + "/dec.encr";
String pubKey = homeDir + "/key/pubKey.key";
String algorithm = "RSA";
String provider = "BC";
String transformation = "RSA/CBC/PKCS5Padding";
//int encrypt= "ENCRYPT_MODE";
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(pubKey));
SecureRandom random = new SecureRandom();
byte[] buffer = new byte[bis.available()];
bis.read(buffer);
bis.close();
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
KeyFactory keyFac = KeyFactory.getInstance(algorithm,provider);
PublicKey publicKey = keyFac.generatePublic(keySpec);
bis = new BufferedInputStream(
new FileInputStream(fileToEncrypt));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(encryptedFile));
Cipher cipher = Cipher.getInstance(algorithm,provider);
cipher.init(1,publicKey,random);
CipherInputStream cis = new CipherInputStream(bis,cipher);
CipherOutputStream cos = new CipherOutputStream(bos,cipher);
buffer = new byte[1024];
while(cis.read(buffer) != 0)
{
cos.write(buffer);
cos.flush();
}
cos.close();
cis.close();
}
}
Can someone help tell me why ?

