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

    Exclamation [SOLVED] UDP chat encryption

    I did these programs:
    Java Code:
    package aes;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    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.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.NullCipher;
    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) {
            try {
                
                String hostname;
                if (args.length==0)
                {
                    hostname = "127.0.0.1";
                }
                else
                { 
                    hostname = args[0];
                }
                
                InetAddress ia = InetAddress.getByName(hostname);
    
                
                SendT send = new SendT(ia, 4000);
                send.start();
                
                ReciveT recive = new ReciveT(send.getSocket());
                recive.start();
                
            } catch (UnknownHostException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
    }
    class SendT extends Thread
    {
        private InetAddress server;
        private DatagramSocket client;
        private int port;
        private byte readText[];
        private byte cipherText[] = new byte[256];
        
        
        
        public SendT(InetAddress address,int port)
        {
            try {
                this.server = address;
                this.port = port;
                this.client = new DatagramSocket(5000);
                this.client.connect(address, port);
                
            } catch (SocketException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        public DatagramSocket getSocket()
        {
            return this.client;
        }
        @Override
        public void run()
        {   
            try {
                    
                    
                    BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
                    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);
                   // sifrovani.init(Cipher.ENCRYPT_MODE, key);
                    cipher.init(NullCipher.ENCRYPT_MODE, key);
                    
                while (true) {
                    
                    
    
                    String readLine = read.readLine();
                    
                    readText = readLine.getBytes();
                    
                    
                    byte cipheredText[] = cipher.doFinal(readText);
                    
                    if (readLine.equals("/q")) {
                        break;
                    }
                    DatagramPacket sendData = new DatagramPacket(cipheredText, cipheredText.length, server, port);
                    client.send(sendData);
                }
            }  catch (IllegalBlockSizeException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (BadPaddingException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvalidKeyException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }
    }
    
    class ReciveT extends Thread
    {
        DatagramSocket client;
        private byte recive [] = new byte[256];
        private byte decryptText[];
       private byte som[] = new byte[256];
        private byte decText[] ;
        
        public ReciveT (DatagramSocket pomA)
        {
            this.client=pomA;
        }
       
        @Override
        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);
                    //desifrovani.init(Cipher.DECRYPT_MODE, key);  
                    decryption.init(NullCipher.DECRYPT_MODE, key);            
                while (true) {
                    
                    
                    DatagramPacket recived = new DatagramPacket(recive, recive.length);
                    client.receive(recived);
                    
                    som = recived.getData();
                    
                    String cText = new String(som,0,recived.getLength());
                                    
                    decText = cText.getBytes();
                    
                    System.out.println("Length:"+recived.getLength());
                    
                    byte deko[] = decryption.doFinal(decText);
                    String statement = new String(deko,"UTF-8");
                    System.out.println(statement);
                    
                    
                    
                   
                }
            }  catch (IllegalBlockSizeException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (BadPaddingException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvalidKeyException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            }  catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }
    }
    and second code:
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    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.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.NullCipher;
    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) {
            try {
                
                String hostname;
                if (args.length==0)
                {
                    hostname = "127.0.0.1";
                }
                else
                { 
                    hostname = args[0];
                }
                
                InetAddress ia = InetAddress.getByName(hostname);
    
                
                SendT send = new SendT(ia, 5000);
                send.start();
                
                ReciveT recive = new ReciveT(send.getSocket());
                recive.start();
                
            } catch (UnknownHostException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
    }
    class SendT extends Thread
    {
        private InetAddress server;
        private DatagramSocket client;
        private int port;
        private byte readText[];
        private byte cipherText[] = new byte[256];
        
        
        
        public SendT(InetAddress address,int port)
        {
            try {
                this.server = address;
                this.port = port;
                this.client = new DatagramSocket(4000);
                this.client.connect(address, port);
                
            } catch (SocketException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        public DatagramSocket getSocket()
        {
            return this.client;
        }
        @Override
        public void run()
        {   
            try {
                    
                    
                    BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
                    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);
                   // sifrovani.init(Cipher.ENCRYPT_MODE, key);
                    cipher.init(NullCipher.ENCRYPT_MODE, key);
                    
                while (true) {
                    
                    
    
                    String readLine = read.readLine();
                    
                    readText = readLine.getBytes();
                    
                    
                    byte cipheredText[] = cipher.doFinal(readText);
                    
                    if (readLine.equals("/q")) {
                        break;
                    }
                    DatagramPacket sendData = new DatagramPacket(cipheredText, cipheredText.length, server, port);
                    client.send(sendData);
                }
            }  catch (IllegalBlockSizeException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (BadPaddingException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvalidKeyException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }
    }
    
    class ReciveT extends Thread
    {
        DatagramSocket client;
        private byte recive [] = new byte[256];
        private byte decryptText[];
       private byte som[] = new byte[256];
        private byte decText[] ;
        
        public ReciveT (DatagramSocket pomA)
        {
            this.client=pomA;
        }
       
        @Override
        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);
                    //desifrovani.init(Cipher.DECRYPT_MODE, key);  
                    decryption.init(NullCipher.DECRYPT_MODE, key);            
                while (true) {
                    
                    
                    DatagramPacket recived = new DatagramPacket(recive, recive.length);
                    client.receive(recived);
                    
                    som = recived.getData();
                    
                    String cText = new String(som,0,recived.getLength());
                                    
                    decText = cText.getBytes();
                    
                    System.out.println("Length:"+recived.getLength());
                    
                    byte deko[] = decryption.doFinal(decText);
                    String statement = new String(deko,"UTF-8");
                    System.out.println(statement);
                    
                    
                    
                   
                }
            }  catch (IllegalBlockSizeException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (BadPaddingException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvalidKeyException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            }  catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchPaddingException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }
    }
    And problem isencryption. Somethime i can send 10 datagrams and no problem but somethime send 1 datagram and error:
    SEVERE: null
    javax.crypto.BadPaddingException: pad block corrupted
    at org.bouncycastle.jce.provider.JCEBlockCipher.engin eDoFinal(Unknown Source)
    at javax.crypto.Cipher.doFinal(DashoA13*..)
    at aes.ReciveT.run(Main.java:182)
    Where is the problem. Maybe you will need this jar: bcprov-jdk16-143.jar on page: bouncycastle.org/latest_releases.html]bouncycastle.org
    Thanks for your advices.

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Why are you making a string out of the bytes before you've decrypted them? Only do that after you've decrypted.

    If you still have trouble, I'd print out the bytes that you're actually getting (and compare with what you sent) to see if that shows anything up.

    By the way, beware of ECB mode-- you may want to read through some stuff I have on block modes (and cryptography in general) on my web site. Unless Bouncy Castle does something odd that I'm unaware of, I think your initialisation vector is useless in this case (and in real life, you obviously shouldn't hard-code it).
    Last edited by neilcoffey; 05-01-2009 at 06:11 AM.

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    P.S. Although I don't think it's causing your problem here, you should bear in mind that UDP doesn't guarantee that packets will be delivered in the order you sent them (or even delivered at all!). In a real-world application, you'd need to build something in to cope with out-of-order and missing packets.

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

    Default

    neilcoffey: Thanks a lot. Problem was in making a string...

Similar Threads

  1. Encryption Project
    By JennaTailier in forum New To Java
    Replies: 6
    Last Post: 05-03-2009, 12:38 AM
  2. Very Simple Encryption
    By AndrewM16921 in forum New To Java
    Replies: 5
    Last Post: 03-24-2009, 01:00 PM
  3. Encryption/Decryption
    By Echilon in forum New To Java
    Replies: 2
    Last Post: 03-24-2009, 11:58 AM
  4. encryption
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 02-06-2008, 10:27 AM
  5. Encryption/Decryption Through AOP
    By SirRawlins in forum Advanced Java
    Replies: 0
    Last Post: 12-19-2007, 03:22 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
  •