Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: TCP chat attaching cipher is not running

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default TCP chat attaching cipher is not running

    Hi,
    I did TCP chat no problem and I attached cipher and It no run. What is wrong on my code.
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.Security;
    import java.security.spec.AlgorithmParameterSpec;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.crypto.Cipher;
    import javax.crypto.CipherInputStream;
    import javax.crypto.CipherOutputStream;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import org.bouncycastle.jce.provider.BouncyCastleProvider;
     
    /**
     *
     * @author Lolek
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            String adresaIP;
            if(args.length==0)
            {
                adresaIP="127.0.0.1";
            }
            else
            {
                adresaIP=args[0];
            }
            PrijemVlakno prijimat = new PrijemVlakno();
            OdeslatVlakno odeslat = new OdeslatVlakno(adresaIP);
     
            prijimat.server.start();
            odeslat.klient.start();
        }
     
    }
     
    class PrijemVlakno implements Runnable
    {
      Thread server;  
     
      public PrijemVlakno()
      {
     
          server = new Thread(this,"Server vlakno");
      }
      public void run()
      {
            try {
     
                Security.addProvider(new BouncyCastleProvider());
                SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes("UTF-8"), "AES");
                AlgorithmParameterSpec iv = new IvParameterSpec("4567891234567AAA".getBytes("UTF-8"));
     
                String nameAlgorithm = "AES/ECB/PKCS7Padding";                
                Cipher decryption = Cipher.getInstance(nameAlgorithm);
                decryption.init(Cipher.DECRYPT_MODE, key); 
     
                ServerSocket serverSocket = new ServerSocket(4000);
                Socket clientSocket = null;
                clientSocket = serverSocket.accept();
     
                while(true)
                {   
                    CipherInputStream cip = new CipherInputStream(clientSocket.getInputStream(),decryption);
                    BufferedReader cteni = new BufferedReader(new InputStreamReader(cip));
                    String radek = null;
                        while((radek = cteni.readLine())!=null)
                        {
                            System.out.println(radek);
                            System.out.flush();
                        }
                }
     
            } catch (InvalidKeyException ex) {
                Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex);
            }
     
     
      }
    }
     
    class OdeslatVlakno implements Runnable
    {   
        private String adresaIP;  
        Thread klient;
        private Socket socket;
        public OdeslatVlakno(String ip)
        {   
            this.adresaIP=ip;
            klient = new Thread(this,"Klient Vlakno");
        }
        public void run()
        {
            try {          
                try{
                 while (true) {
                     try {
                            socket = new Socket(this.adresaIP, 5000);
                            break;
                         }
                    catch (IOException e) {                       
                            Thread.sleep(1000);
                        }
                    }
                }
                catch (InterruptedException e) {
                    return;
                } 
     
     
     
                Security.addProvider(new BouncyCastleProvider());
                SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes("UTF-8"), "AES");
                AlgorithmParameterSpec iv = new IvParameterSpec("4567891234567AAA".getBytes("UTF-8"));
     
                String nameAlgorithm = "AES/ECB/PKCS7Padding";
                Cipher cipher = Cipher.getInstance(nameAlgorithm);
                cipher.init(Cipher.ENCRYPT_MODE, key);
     
                BufferedReader klavesnice = new BufferedReader(new InputStreamReader(System.in));
                OutputStream vystup = socket.getOutputStream();
                CipherOutputStream cip = new CipherOutputStream(vystup,cipher);
                BufferedWriter zapis = new BufferedWriter(new OutputStreamWriter(cip));
     
                String radek;
                while ((radek = klavesnice.readLine())!=null)
                {   
     
                    zapis.write(radek);
                    zapis.newLine();
                    zapis.flush();
     
                    System.out.println("Cteni radku:"+radek);
                }
            } 
            catch (InvalidKeyException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            }        catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            }               catch (IOException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: TCP chat cipher

    Hello Koren3,

    What error messages are you getting when running this code?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: TCP chat cipher

    No error, but another client doesnīt recive message.

  4. #4
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: TCP chat cipher

    I donīt know what is problem...

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: TCP chat cipher

    I know, where was a problem. I used block cipher. But I use stream cipher and it write this error: java.security.InvalidKeyException: no IV set when one expected. If i use stream cipher i don't need IV. I think. Where is the problem thank you.

    My code:
     Security.addProvider(new BouncyCastleProvider());
                SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes("UTF-8"), "Salsa20");
     
                String nameAlgorithm = "Salsa20";                
                Cipher decryption = Cipher.getInstance(nameAlgorithm);
                decryption.init(Cipher.DECRYPT_MODE, key);

Similar Threads

  1. Replies: 10
    Last Post: May 8th, 2009, 10:49 AM
  2. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM
  3. Replies: 0
    Last Post: May 3rd, 2009, 10:55 AM
  4. [SOLVED] Interesting error cipher while sending message
    By Koren3 in forum Java Networking
    Replies: 0
    Last Post: April 29th, 2009, 09:54 AM
  5. SSL Chat implementation
    By Koren3 in forum Java Networking
    Replies: 6
    Last Post: April 24th, 2009, 08:20 AM