• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read from a text file, a buffer at a time (specific bytes at a time)?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.

Like so:


also should I use the doFinal() or the updata()? Can someone tell me a better way to do this, if there is another way?

Thank you.

regards,
harke
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

harke baj wrote:should i put the doFinal() immediately after the update(), or after the loop finishes?


It's called doFinal; doesn't that say enough?

Also, when should I write the encoded data to a file.


I would do it in between. In pseudo code:
 
harke baj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have done it.

But not using the update(). I read the files in chunks and then used doFinal().



But I still could not get, how to use the update().
Could you direct me to some examples?

thanks you
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
harke baj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this did not work.
I go the following error:


my code:



The key size i used to generate the keys, is 1024.

Thanks,
harke
 
harke baj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i solved the problem. The problem was because i was using wrong byte size. I change the byte[] to 100 from 50. and it worked.\

Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic