I am having problems with an RSA application that I have to do in Java.
I need to read a line from a file, encrypt it and then save the encrypted line in a new file.
My RSA key is 1024 bits long.
The code part where the problem occurs is as follows:
readBytes = in.read(bytesToBeEncoded, 0, bytesToBeEncoded.length); while(readBytes != -1){ encodedContent = ciph.update(bytesToBeEncoded, 0, readBytes); out.write(encodedContent); bytesToBeEncoded= new byte[(KEY_SIZE/8)-11]; readBytes = in.read(bytesToBeEncoded, 0, bytesToBeEncoded.length); } encodedContent = ciph.doFinal(); out.write(encodedContent);
If the variables are defined as follows:
byte[] bytesToBeEncoded = new byte[(KEY_SIZE/8)-11]; FileInputStream in = new FileInputStream(new File(file1)); FileOutputStream out = new FileOutputStream(new File(file2)); int readBytes;
The fact is that when I encrypt a string less than 117 bytes in size, it works fine (is encrypted and then decrypts well), but when the size is larger, the application throws this exception:
javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
Abandoned:
encodedContent = ciph.doFinal();
I do not know where the problem is and what I should do.
Can anyone help me? Thanks.
Sorry for my English.
Shuli source share