Hi there,
I am struggling to encrypt more than 117 bytes of data with a 1024 bit RSA key.
If I try to encrypt (write to my OutputStreamWriter) 118 Bytes ("A"s) or more, the file I am writing to is just empty and I get an exception whe trying to read it.
I know the encryptable bytes (blocksize) are related to the key length: blocksize = keylength / 8 -11
I also know RSA encryption of large files is discouraged, because it is like 100x slower than for instance AES and was originally only intended to exchange symmetric keys.
Still I need to be able to asymmetrically encrypt at least 5kb of Data.
I am out of ideas here and have no understanding of why the block size is limited (i know from hours of "googling" that it is limited as described above though).
So, I would be
very glad If somebody could point out a way how I could encrypt more than 117 bytes (without using a longer key of course).
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.Reader;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.security.PublicKey;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import javax.crypto.*;
public class strchrbty {
public static void main(String[] args) {
//generate Keys
PublicKey publicKey = null;
PrivateKey privateKey = null;
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair kp = kpg.generateKeyPair();
publicKey = kp.getPublic();
privateKey = kp.getPrivate();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
//------ENCODE------
try {
//initialize cipher
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
OutputStream cos = new CipherOutputStream(new FileOutputStream("c:\\test.txt"), cipher);
Writer out = new OutputStreamWriter(cos);
// 1024 bit (key length) / 8 bytes - 11 bytes padding = 117 Bytes Data.
// for 118 Bytes (or more) Data: c:\\test.txt is empty annd no Data is read.
for(int i = 0; i<117; i++) {
out.write("A");
}
out.close();
cos.close();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//------DECODE------
try {
StringBuffer buf = new StringBuffer();
Cipher cipher2 = Cipher.getInstance("RSA");
cipher2.init(Cipher.DECRYPT_MODE, privateKey);
//read char-wise
InputStream cis = new CipherInputStream(new FileInputStream("c:\\test.txt"), cipher2);
Reader in = new InputStreamReader(cis);
for(int c = in.read(); c != -1; c = in.read()) {
buf.append((char)c);
}
//output
System.out.println(buf.toString());
in.close();
cis.close();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}
Regards.
Edited by: junghansmega on Sep 10, 2008 3:41 PM
Sorry about the bad autoformating.... It only occurrs in here, in my eclipse it looks fine =(