RSA ciphered streams problem

Discussion in 'Java' started by Jakub Stuglik, Dec 13, 2004.

  1. 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
     
    Jakub Stuglik, Dec 13, 2004
    #1
    1. Advertising

  2. Jakub Stuglik

    Guest

    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.
     
    , Dec 13, 2004
    #2
    1. Advertising

  3. 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
     
    Jakub Stuglik, Dec 13, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AdrianK
    Replies:
    0
    Views:
    1,541
    AdrianK
    Jul 9, 2003
  2. John Bergstrom
    Replies:
    0
    Views:
    1,234
    John Bergstrom
    Aug 3, 2003
  3. gg
    Replies:
    0
    Views:
    2,711
  4. Gandu
    Replies:
    0
    Views:
    462
    Gandu
    Dec 1, 2004
  5. Jakub Stuglik

    Ciphered streams problem

    Jakub Stuglik, Dec 12, 2004, in forum: Java
    Replies:
    0
    Views:
    335
    Jakub Stuglik
    Dec 12, 2004
Loading...

Share This Page