0
votes

I have a large file that I am trying to encrypt with RSA. Yes, I know that hybrid encryption is typically used in this case, but I would like to use standard public key encryption to see the difference in efficiency.

In order to encrypt such a large file, I understand that I must encrypt block by block. After receiving, an error that stated my byte array was too large to encrypt with RSA-2048, I reduced the byte array to 245 bytes. A byte array of 246 bytes works fine, but it also only works for 246 bytes which is my issue. I need to encrypt the whole file with RSA-2048.

After researching RSA-2048 cryptography to get a better understanding, and block by block encryption, I am still confused about implementing block by block encryption with RSA-2048. This is my first time implementing cryptography in any language. Below is my code pertaining to encrypting the large file with an RSA public key.

Any suggestions as to how I can encrypt the whole large file block by block with RSA are appreciated.

1
No, not "I know that hybrid encryption is typically used in this case" but never be knowledgable developers. Symmetric and asymmetric encryption each have their place based on their characteristics, use them that way just as one would not use 1000 mini-cars to deliver the concrete for a skyscraper. There there is the question why are you even using asymmetric encryption (RSA), do you have a need for both a public and private key pair? Finally, I sure hope this will never be used in production code. - zaph

1 Answers

1
votes

First of all, regardless of what you might say, you should never use RSA as a block cipher. If you did this in production code and someone found out, you would most likely get fired.

But...

  • You should read upto ~246 bytes from the FileInputStream.
  • Encrypt the result of this using RSA.
  • Write the encrypted block to the FileOutputStream.

Your code doesn't do this because:

  • You return the first block of ciphertext immediately:

    inputfile= cipher.doFinal(block); return inputfile;

  • You don't do anything with the result.