Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Constructor of ObjectInputStream freezes

843790Apr 28 2010 — edited May 19 2010
Hi. I have problem with my aplication. You must run it with argument 0(for server) and 1(for client).
    import java.io.*;  
    import java.net.*;  
    import java.security.*;  
    import java.util.Arrays;  
    import java.util.Scanner;  
    import java.util.concurrent.TimeUnit;  
    import javax.crypto.*;  
      
    public class Szyfr implements Serializable  
   {  
       public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException,   
                   NoSuchPaddingException, IOException, ClassNotFoundException, InterruptedException, IllegalBlockSizeException  
       {  
           new Szyfr().dzialaj(args[0]);  
       }  
       public void dzialaj(String op) throws NoSuchAlgorithmException, IOException, NoSuchPaddingException,   
                     InvalidKeyException, ClassNotFoundException, InterruptedException, IllegalBlockSizeException  
       {  
           if(op.equals("0"))  
           {  
               System.out.println("server");  
               KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");  
               KeyPair kp = kpg.generateKeyPair();  
               Cipher  cipher = Cipher.getInstance("RSA");  
               cipher.init(Cipher.UNWRAP_MODE, kp.getPrivate());  
               ServerSocket ss = new ServerSocket(7777);  
               Socket s = ss.accept();  
               ObjectOutputStream ous = new ObjectOutputStream(s.getOutputStream());  
               ObjectInputStream ois = new ObjectInputStream(s.getInputStream());  
               ous.writeObject(kp.getPublic());  
               Klucz kl = (Klucz)ois.readObject();  
               Key priv = cipher.unwrap(kl.getKlucz(), "AES", Cipher.SECRET_KEY);  
               cipher = Cipher.getInstance("AES");  
               cipher.init(Cipher.ENCRYPT_MODE, priv);  
               Cipher cipher2 = Cipher.getInstance("AES");  
               cipher2.init(Cipher.DECRYPT_MODE, priv);  
               ois = new ObjectInputStream(new CipherInputStream(s.getInputStream(), cipher2));  
               System.out.println("server ois stworzony");  
               ous = new ObjectOutputStream(new CipherOutputStream(s.getOutputStream(), cipher));  
               System.out.println("server ous stworzony");  
               while(true)  
               {  
                   ous.writeObject(new String("tomek"));  
                   TimeUnit.MILLISECONDS.sleep(1000);  
               }  
           }  
           else  
           {  
               System.out.println("klient");  
               Socket s = new Socket("localhost", 7777);  
               s.setReuseAddress(true);  
               ObjectInputStream ois = new ObjectInputStream(s.getInputStream());  
               ObjectOutputStream ous = new ObjectOutputStream(s.getOutputStream());  
               PublicKey pub = (PublicKey)ois.readObject();  
               Cipher cipher = Cipher.getInstance("RSA");  
               cipher.init(Cipher.WRAP_MODE, pub);  
               SecretKey priv = KeyGenerator.getInstance("AES").generateKey();   
               byte[] wrappedKey = cipher.wrap(priv);  
               ous.writeObject(new Klucz(wrappedKey));  
               cipher = Cipher.getInstance("AES");  
               cipher.init(Cipher.DECRYPT_MODE, priv);  
               Cipher cipher2 = Cipher.getInstance("AES");  
               cipher2.init(Cipher.ENCRYPT_MODE, priv);  
               ous = new ObjectOutputStream(new CipherOutputStream(s.getOutputStream(), cipher2));  
               System.out.println("kllient ous stworzony");  
               ois = new ObjectInputStream(new CipherInputStream(s.getInputStream(), cipher));  
               System.out.println("klient ois stworzony"); 
               while(true)  
               {  
                 System.out.println((String)ois.readObject());  
               }  
           }  
       }  
       public class Klucz implements Serializable  
       {  
           public Klucz(byte[] klucz)  
           {  
             
               this.klucz = klucz;  
           }  
           public byte[] getKlucz()  
           {  
               return klucz;  
           }  
           private byte[] klucz;  
       }  
         
   }      
I don't know why but it freezes on the ObjectInputStream constructors with Cipher streams. When I don't use Cipher stream it works. When i have only ous = new ObjectOutputStream(new CipherOutputStream(s.getOutputStream(), cipher2)); in the server and ois = new ObjectInputStream(new CipherInputStream(s.getInputStream(), cipher2)); in the client it works too. What i'm doing wrong?
Thanks for your help and sorry for my english.
greetings for you all.

Edited by: calm1985 on Apr 27, 2010 10:00 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 16 2010
Added on Apr 28 2010
27 comments
901 views