Results 1 to 3 of 3
  1. #1
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default TCP stream cypher

    Hallo,
    I try do cypher in TCP. I did before UDP and I used block cypher AES and no problem. But now I need streamCypher and i don't know how can i do this things.
    My code:
    Java Code:
    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("11111111112222222222333333333344444444455555555566666666667777777777888888888899999999991111111111222222222233333333".getBytes("UTF-8"), "DES");
               
                   
                              
                Cipher decryption = Cipher.getInstance("SKIPJACK/ECB/PKCS7Padding","BC");
                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 (NoSuchProviderException ex) {
                Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex);
            }  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);
            }
          
        
      }
    }

  2. #2
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    I rebuild this program. Aplication work but. Message doesn´t send and after recive. Messsage sed to another same program but different ports... I'm desperate. It the last one program what I have to do. Please help me. Thank you.
    Java 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.InvalidAlgorithmParameterException;
    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;  
      CipherInputStream cis;
      
      public PrijemVlakno()
      {
    
          server = new Thread(this,"Server vlakno");
      }
      public void run()
      {
            try {
                
               byte[] iv = new byte[]{(byte)0x8E, 0x12, 0x39, (byte)0x9C,0x07,0x72,0x6F, 0x5A};
               AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
              
                
                Security.addProvider(new BouncyCastleProvider());
                SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes("UTF-8"), "Salsa20");
                
                Cipher decryption = Cipher.getInstance("Salsa20");
                decryption.init(Cipher.DECRYPT_MODE, key,paramSpec); 
                
                ServerSocket serverSocket = new ServerSocket(4000);
                Socket clientSocket = null;
                clientSocket = serverSocket.accept();
                            
                while(true)
                {   
                    cis = new CipherInputStream(clientSocket.getInputStream(),decryption);
                    BufferedReader cteni = new BufferedReader(new InputStreamReader(cis));
                    
                    String radek = null;
                        while((radek = cteni.readLine())!=null)
                        {
                            System.out.println(radek);
                            System.out.flush();
                        }
                }
                
            } catch (InvalidAlgorithmParameterException ex) {
                Logger.getLogger(PrijemVlakno.class.getName()).log(Level.SEVERE, null, ex);
            } 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;
                } 
                
                byte[] iv = new byte[]{(byte)0x8E, 0x12, 0x39, (byte)0x9C,0x07,0x72,0x6F, 0x5A};
                AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
                
                
                Security.addProvider(new BouncyCastleProvider());
                SecretKeySpec key = new SecretKeySpec("1234567891234567".getBytes("UTF-8"), "Salsa20");          
                    
                Cipher cipher = Cipher.getInstance("Salsa20");
                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 line;
                while ((line = klavesnice.readLine())!= null)
                {   
                    
                    zapis.write(line);
                    zapis.newLine();                
                    zapis.flush();
                    
                    System.out.println("Cteni radku:"+line);
                }
            } 
            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);
            }
        }
    }

  3. #3
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    Nobody know how can i do this thing? Maybe the best way. It will do normaln block cipher. Or somebody have idea?

Similar Threads

  1. Cypher 1.1.8.2
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-24-2008, 07:36 PM
  2. Cypher 1.1.8 beta
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-19-2008, 06:07 PM
  3. Cypher 1.1.5 beta
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-10-2008, 05:24 PM
  4. Cypher 1.1.0 beta
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-05-2008, 05:30 PM
  5. Cypher 1.0.8 beta
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-03-2008, 05:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •