RSA ciphered streams problem

J

Jakub Stuglik

Hi. I've got a problem with the RSA ciphered streams. Here is a piece of
code that generates an exception :

KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
System.out.print("Generating the RSA key pair...");
KeyPair kp = gen.generateKeyPair();
System.out.println("done.");
Cipher ci = Cipher.getInstance("RSA");
ci.init(Cipher.ENCRYPT_MODE,kp.getPublic());
Cipher cid = Cipher.getInstance("RSA");
cid.init(Cipher.DECRYPT_MODE,kp.getPrivate());
CipherOutputStream cout = new CipherOutputStream(new
FileOutputStream("rsa.dat"),ci);
ObjectOutputStream oout = new ObjectOutputStream(cout);
oout.writeObject(kp.getPrivate().toString());
oout.flush();
oout.close();
CipherInputStream cin = new CipherInputStream(new
FileInputStream("rsa.dat"),cid);
ObjectInputStream oin = new ObjectInputStream(cin); // Exception here
System.out.println(oin.readObject());
oin.close();

The exception is EOFException and it's not very weird because the file
"rsa.dat" is empty after writing to it!! It happens only if object is "big"
( when I try to write some short String, for example, everything's ok ).
When I tried to write an unciphered output ( without using
CipheredOutputStream ) it went well of course.
My question is : can anybody tell me why it happens? What should I do to
write "big" objects to ciphered output stream using ObjectOutputStream
properly?
Thanks for any requests.

Kuba
 
K

klynn47

Have you tried printing the output to the screen to make sure that
something is being written? The only think I could think might be the
problem is that there wasn't enough time to write the whole object. You
might try two methods that use thread synchronization. That way, if the
write operation and read operation are synchronized, then the read
can't happen until the write is finished.
 
J

Jakub Stuglik

You're right - I tried to put it on the screen and "big" objects aren't
written to the stream - there's nothing on the screen after writing
operation ( I also commented code lines responsible for reading the object
to make sure there's enough time to write the whole object but it wouldn't
help ). Do you maybe have any idea how to deal with such a problem?
Thanks for any suggestions.

Kuba
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top