| | |
Java Encryption: RSA Block Size?
![]() |
•
•
Join Date: Apr 2008
Posts: 49
Reputation:
Solved Threads: 3
How would one calculate the number of bytes of plaintext that could be encrypted as a single block given the key size? And how could you then calculate the number of bytes of cipher text that can be decrypted in a single block given the same key size because I am told that the two sizes are different.
I tried using but that only returns 0. Thanks for your help.
I tried using
java Syntax (Toggle Plain Text)
Cipher.getBlockSize()
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
Normally, you get the number of bits dictated by the key size (e.g. 512-bit key = 64 bytes) minus a few bytes (11, I think) of overhead. For the gory details, see the so-called PKCS #1 standard: http://tools.ietf.org/html/rfc3447#page-35
However, if you're worrying about this, you may be doing something wrong! RSA isn't really designed to be used as a regular block cipher. The idea is that you use RSA to encrypt the key of some other block cipher, e.g. AES. And usually, the number of bits in the RSA key will be much greater than the number of bits in the AES key or other you're encrypting-- e.g. you might have a 2048-bit RSA key and a 128 or 256-bit AES key.
You might be interested in some stuff I've written about RSA encryption in Java that includes an evaluation of different key lengths.
However, if you're worrying about this, you may be doing something wrong! RSA isn't really designed to be used as a regular block cipher. The idea is that you use RSA to encrypt the key of some other block cipher, e.g. AES. And usually, the number of bits in the RSA key will be much greater than the number of bits in the AES key or other you're encrypting-- e.g. you might have a 2048-bit RSA key and a 128 or 256-bit AES key.
You might be interested in some stuff I've written about RSA encryption in Java that includes an evaluation of different key lengths.
•
•
Join Date: Apr 2008
Posts: 49
Reputation:
Solved Threads: 3
0
#3 Oct 18th, 2009
It's for an assignment. It's demonstrating that AES, for example, is much better to use than RSA when it comes to encryption. I have to get it to work for both types. I'm not having anywhere near as much trouble with the AES portion. 
Overhead is 11 bytes apparently. So does that mean for encryption I use a block size of
If so, how would I calculate the length of the public key?
I have a 2048 bit key. From your original post, it appears that the blocksize for encrypting should be the same as the blocksize for decrypting, but my professor told us this isn't the case.

Overhead is 11 bytes apparently. So does that mean for encryption I use a block size of
java Syntax (Toggle Plain Text)
int blockSize = KeypairGenerator.getPublicKey().length() - 11; // I know that the .length() portion isn't valid.
If so, how would I calculate the length of the public key?
I have a 2048 bit key. From your original post, it appears that the blocksize for encrypting should be the same as the blocksize for decrypting, but my professor told us this isn't the case.
Last edited by leverin4; Oct 18th, 2009 at 1:31 pm.
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
0
#4 Oct 18th, 2009
•
•
•
•
I have a 2048 bit key. From your original post, it appears that the blocksize for encrypting should be the same as the blocksize for decrypting, but my professor told us this isn't the case.
One thing to remember is that with RSA and actually many other algorithms including AES usually, the "useful" data that you supply isn't literally the data that's encrypted. Usually, some extra data needs to be included, for example, indicating the actual length of the data in some way, data for any integrity checking... To the user, the number of input bytes doesn't necessarily equal the number of bytes following encryption.
•
•
Join Date: Apr 2008
Posts: 49
Reputation:
Solved Threads: 3
0
#5 Oct 18th, 2009
•
•
•
•
No, not exactly although you're on the right track. If you have a 2048-bit key, then the encrypted data will be an array of 2048/8 = 256 bytes. Then, the input data can be up to 256-11=245 bytes.
One thing to remember is that with RSA and actually many other algorithms including AES usually, the "useful" data that you supply isn't literally the data that's encrypted. Usually, some extra data needs to be included, for example, indicating the actual length of the data in some way, data for any integrity checking... To the user, the number of input bytes doesn't necessarily equal the number of bytes following encryption.
Then when I get down to the final block, if I'm not using any padding, then I'll have to manually fill in the extra bytes, and remember how many of those I manually filled in, so when I decrypt I remember to not print those out, yes?
But when I encrypt and decrypt, I'm always pulling 245 bytes of either plain text or ciphertext, but the amount I get back is what is different. I hope my rambling makes sense. Can you verify if I'm correct?
Last edited by leverin4; Oct 18th, 2009 at 2:31 pm.
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
0
#7 Oct 18th, 2009
•
•
•
•
Actually, it appears that when encrypting, I use a blocksize of (2048 / 8) - 11 , or 245, bytes, and that returns a block of ciphertext consisting of 256 bytes.
•
•
•
•
When decrypting, I need to pass in a 256-byte block of ciphertext and it should return a 245 byte block of plaintext, correct?
The actual number of bytes of original plaintext is "magically" encoded during encyption (that's partly why you loose 11 bytes-- to encode the length plus an integrity check).
Last edited by neilcoffey; Oct 18th, 2009 at 4:32 pm.
•
•
Join Date: Apr 2008
Posts: 49
Reputation:
Solved Threads: 3
0
#8 Oct 18th, 2009
So does this mean I don't have to manually pad anything? See code below for clarification.
Basically what you're telling me is that my else block in the code is pointless?
java Syntax (Toggle Plain Text)
DataInputStream pInRSA = new DataInputStream(new FileInputStream(inFile)); DataOutputStream eOutRSA = new DataOutputStream(new FileOutputStream("RSACipherText.txt")); encoderRSA = Cipher.getInstance("RSA/ECB/NoPadding"); encoderRSA.init(Cipher.ENCRYPT_MODE, keyRSA.getPublic()); blockSize = (2048 / 8) - 11; buffer = new byte[blockSize]; count = 0; while (pInRSA.available() > 0) { int i = 0; if (pInRSA.available() > blockSize) { while (i < blockSize) { buffer[i] = pInRSA.readByte(); count++; i++; } } else { while (pInRSA.available() > 0) { buffer[i] = pInRSA.readByte(); count++; i++; } while (i < blockSize) { buffer[i] = Byte.MAX_VALUE; i++; } } encodedMsg = encoderRSA.doFinal(buffer); eOutRSA.write(encodedMsg, 0, encodedMsg.length); } pInRSA.close(); eOutRSA.close();
Basically what you're telling me is that my else block in the code is pointless?
Last edited by leverin4; Oct 18th, 2009 at 4:41 pm.
•
•
Join Date: Apr 2008
Posts: 49
Reputation:
Solved Threads: 3
0
#9 Oct 18th, 2009
I could do something like
java Syntax (Toggle Plain Text)
DataInputStream pInRSA = new DataInputStream(new FileInputStream(inFile)); DataOutputStream eOutRSA = new DataOutputStream(new FileOutputStream("RSACipherText.txt")); encoderRSA = Cipher.getInstance("RSA/ECB/NoPadding"); encoderRSA.init(Cipher.ENCRYPT_MODE, keyRSA.getPublic()); blockSize = (2048 / 8) - 11; count = 0; while (pInRSA.available() > 0) { buffer = new byte[Math.min(blockSize, pInRSA.available())]; for (int i = 0; i < buffer.length; i++) { buffer[i] = pInRSA.readByte(); count++; } encodedMsg = encoderRSA.doFinal(buffer); eOutRSA.write(encodedMsg, 0, encodedMsg.length); } pInRSA.close(); eOutRSA.close();
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
0
#10 Oct 18th, 2009
You must use padding, but unless you're really sure of what you're doing, it's best to let the library do it. So just instantiate your Cipher as "RSA"-- don't specify Nopadding!
![]() |
Similar Threads
- Java Encryption and Decryption (Java)
- Java Encryption and Decryption (Java)
- help recovering a word using RSA (Python)
- Forms in Random access files (Visual Basic 4 / 5 / 6)
- CD Burning Error: Block size does not correspond to image length (Windows NT / 2000 / XP)
- Java Encryption error (Java)
- Java Encryption (Elections System) (Java)
- aspect oriented project (Java)
Other Threads in the Java Forum
- Previous Thread: ComboBox Problem
- Next Thread: Null Pointer Exception Help
| Thread Tools | Search this Thread |
.net 3d add ajax apple applet appportability automation balls bidirectional binary birt blackberry block browser button buyouts c++ class code collections component constructor crack design development domains eclipse ect educational email encryption error event firefox fractal froglogic ftp game google government grails gui html idea infosec integer java javadesktopapplications javafx javascript jetbrains julia linux listbox loop loops macosx method microsoft mysql netbeans newbie news open-source opensource oracle oriented password problem program programming project python qanda radio ria rodjohnson rsa scanner security set size software sort sorting ssl string sun survey swing swt threads tree virtualization web webmail whileloop windows wxpython






