Could someone tell me how I can read a text file, a buffer at a time? I want to read certain bytes at a time.
I wanted to encrypt a text file using RSA ( just for knowledge sake ), using 1024 key size. But the size of the message to encrypt is limited. I can't go on increasing the key size as the process become super slow.
I was wondering if I could read certain bytes at a time, encrypt them and write the encrypted data to a file.
also should I use the doFinal() or the updata()? Can someone tell me a better way to do this, if there is another way?
InputStream has read() methods that take a byte. They will read up to the array length in bytes, and return the number of bytes read. You can use those two (the array and the number of bytes read) in the update() method. doFinal() should be called when you've called update() with all read data already, to get the remainder.
You're calling doFinal() each time so you are not encrypting the entire file; you are encrypting each chunk as if it were a separate entity. This will lead to problems if the cipher adds padding to make the final result of a certain length (like a multitude of 8 bytes). The update uses intermediate results, and does not add padding. You will only get that padding with doFinal(). In short: