Java Encryption: RSA Block Size?

Reply

Join Date: Apr 2008
Posts: 49
Reputation: leverin4 is an unknown quantity at this point 
Solved Threads: 3
leverin4 leverin4 is offline Offline
Light Poster
 
0
  #11
Oct 18th, 2009
Originally Posted by neilcoffey View Post
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!
Unfortunately "NoPadding" is another pesky requirement for this assignment. I wish I didn't have to.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 49
Reputation: leverin4 is an unknown quantity at this point 
Solved Threads: 3
leverin4 leverin4 is offline Offline
Light Poster
 
0
  #12
Oct 18th, 2009
I only compared the original text to the decrypted text at a glance, but it appeared to have worked. Below is my entire encryption/decryption process with RSA. Like I said, it appears to have worked great. Does it make sense to you?

  1. KeyPairGenerator generatorRSA = KeyPairGenerator.getInstance("RSA");
  2. generatorRSA.initialize(2048, new SecureRandom());
  3. KeyPair keyRSA = generatorRSA.generateKeyPair();
  4.  
  5. DataInputStream pInRSA = new DataInputStream(new FileInputStream(inFile));
  6. DataOutputStream eOutRSA = new DataOutputStream(new FileOutputStream("RSACipherText.txt"));
  7. encoderRSA = Cipher.getInstance("RSA/ECB/NoPadding");
  8. encoderRSA.init(Cipher.ENCRYPT_MODE, keyRSA.getPublic());
  9. blockSize = (2048 / 8) - 11;
  10.  
  11. while (pInRSA.available() > 0) {
  12. buffer = new byte[Math.min(blockSize, pInRSA.available())];
  13. for (int i = 0; i < buffer.length; i++) {
  14. buffer[i] = pInRSA.readByte();
  15. }
  16. encodedMsg = encoderRSA.doFinal(buffer);
  17. eOutRSA.write(encodedMsg, 0, encodedMsg.length);
  18. }
  19. pInRSA.close();
  20. eOutRSA.close();
  21.  
  22. DataInputStream eInRSA = new DataInputStream(new FileInputStream("RSACipherText.txt"));
  23. DataOutputStream pOutRSA = new DataOutputStream(new FileOutputStream("RSAPlainText.txt"));
  24. decoderRSA = Cipher.getInstance("RSA/ECB/NoPadding");
  25. decoderRSA.init(Cipher.DECRYPT_MODE, keyRSA.getPrivate());
  26. blockSize = encodedMsg.length;
  27. buffer = new byte[blockSize];
  28.  
  29. while (eInRSA.available() > 0) {
  30. for (int i = 0; i < blockSize; i++) {
  31. buffer[i] = eInRSA.readByte();
  32. }
  33. decodedMsg = decoderRSA.doFinal(buffer);
  34. pOutRSA.write(decodedMsg, 0, decodedMsg.length);
  35. }
  36. eInRSA.close();
  37. pOutRSA.close();
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training
 
0
  #13
Oct 18th, 2009
Originally Posted by leverin4 View Post
I only compared the original text to the decrypted text at a glance, but it appeared to have worked. Below is my entire encryption/decryption process with RSA. Like I said, it appears to have worked great. Does it make sense to you?
I think it probably fulfils the assignment. Note if you're not enabling any padding, you don't need to subtract 11 from the array size (the 11 was for the default padding).

Note you should never do this in real life. For example, try encrypting the following and seeing what the "encrypted" data looks like:
- a block of all zeroes
- a block of all zeroes except for the final byte in the array, which is set to 1

If you're encrypting multiple blocks with the same key, you should also never let the same plain text block result in the same encrypted block. This is what "block modes" are all about with genuine block ciphers. But note that in any case RSA is really not generally used this way: you generally just encrypt a single AES key (or some other key) at the start of the data, and then use that to encrypt the "real" data.
Last edited by neilcoffey; Oct 18th, 2009 at 7:25 pm.
Reply With Quote Quick reply to this message  
Reply

Tags
block, encryption, java, rsa, size

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC