Results 1 to 1 of 1
  1. #1
    puriaah is offline Member
    Join Date
    Sep 2014
    Posts
    1
    Rep Power
    0

    Default Encryption in UDP Client/server

    each server has their own client that without Encryption , the work correctly.... , but just putting encryption in one way from server one to server two ,make it toface Exceprion that happne in line : bais=new ByteArrayInputStream(cipher.doFinal(Packet1.getDat a())); of Far_away_Server . can you tell me what is problem ?




    package server1;

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.PrintStream;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;

    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.KeyGenerator;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    //import server2.SerializablePacket;

    import serial.SerializablePacket;



    public class VPN_Server {


    public static final int serverPort=10002;
    public static final int MAXIMUM_SIZE=1024;
    ;
    public static final int client1Port=10001;
    public static final int client2port=10004;
    public static final int nextServerPort=10003;
    public static final int MAX_SIZE=1024;
    public static final int max=255;

    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    byte[] data1=new byte[max];
    KeyGenerator keyGene=KeyGenerator.getInstance("Blowfish");
    SecretKey secretkey =keyGene.generateKey();


    Cipher cipher=Cipher.getInstance("Blowfish");
    System.out.println("the key is "+secretkey.toString());
    DatagramSocket serverSocket = new DatagramSocket(serverPort);
    ByteArrayOutputStream bStream1 = new ByteArrayOutputStream();
    ObjectOutputStream oop1=new ObjectOutputStream(bStream1);
    oop1.writeObject(secretkey);

    System.out.println("VPNserver is ready to send the key to the Far_Away_Server");
    System.out.println();
    data1 =bStream1.toByteArray();

    DatagramPacket FarPacket=new DatagramPacket(data1,data1.length);

    InetAddress remote_addr =InetAddress.getLocalHost();
    FarPacket.setAddress(remote_addr);
    FarPacket.setPort(nextServerPort);

    serverSocket.send(FarPacket);



    int guest1Port; // Port to start serverSocket on
    InetAddress guest1Address;
    InetAddress destAddress;
    int desPort;
    String sData1;
    String sData2;
    System.out.println("server1 is seting up");
    System.out.println("*****************************" );

    while(true)
    {
    System.out.println("*****************************" );

    byte[] Data1 = new byte[MAXIMUM_SIZE];
    byte[] Data2 = new byte[MAXIMUM_SIZE];
    DatagramPacket Packet1 = new DatagramPacket(Data1, Data1.length);


    serverSocket.receive(Packet1);

    System.out.println("server1 receives a packet ");

    guest1Address=Packet1.getAddress();
    destAddress=guest1Address;
    guest1Port=Packet1.getPort();

    if(guest1Port == client1Port )
    {
    System.out.println("*****************************" );
    ByteArrayInputStream bais1=new ByteArrayInputStream(Packet1.getData());
    ObjectInputStream oos1 = new ObjectInputStream(bais1);

    sData1=(String)oos1.readObject();



    remote_addr =InetAddress.getLocalHost();
    SerializablePacket sp1=new SerializablePacket(remote_addr,client2port,MAXIMUM _SIZE);
    sp1.setContain(sData1);

    cipher.init(Cipher.ENCRYPT_MODE, secretkey);



    bStream1 = new ByteArrayOutputStream();
    oop1=new ObjectOutputStream(bStream1);
    oop1.writeObject(sp1);
    System.out.println("packet is going inside of another packet ");
    System.out.println();

    Data2 =cipher.doFinal(bStream1.toByteArray());

    DatagramPacket Packet2 = new DatagramPacket(Data2, Data2.length);

    Packet2.setAddress(InetAddress.getByName("127.0.0. 1"));
    Packet2.setPort(nextServerPort);

    serverSocket.send(Packet2);
    System.out.println("server1 sends a packet ");

    }




    if(guest1Port == nextServerPort )
    {
    System.out.println("*****************************" );
    byte[] Data3 = new byte[MAXIMUM_SIZE];
    DatagramPacket Packet3 = new DatagramPacket(Data3, Data3.length);

    SerializablePacket sp1=new SerializablePacket();
    ByteArrayInputStream bais1=new ByteArrayInputStream(Packet1.getData());
    ObjectInputStream oos1 = new ObjectInputStream(bais1);

    sp1=(SerializablePacket)oos1.readObject();
    String mess=sp1.getContain();
    desPort=sp1.getPort();

    bStream1 = new ByteArrayOutputStream();
    oop1=new ObjectOutputStream(bStream1);
    oop1.writeObject(mess);


    Data3 = bStream1.toByteArray();

    Packet3.setData(Data3);
    Packet3.setAddress(InetAddress.getByName("127.0.0. 1"));
    Packet3.setPort(desPort);
    serverSocket.send(Packet3);
    System.out.println("server1 sends a packet ");

    }


    }



    }

    }



    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    now Far _away_Server :


    package server2;

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.PrintStream;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;

    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    //import server1.SerializablePacket;

    import serial.SerializablePacket;



    public class Far_away_Server {


    public static final int serverPort=10003;
    public static final int MAXIMUM_SIZE=1024;
    public static final int clientPort1=10004;
    public static final int clientPort2=10001;
    public static final int nextServerPort=10002;
    public static final int MAX_SIZE=1024;
    public static int max=255;





    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {



    byte[] data1=new byte[max];
    SecretKey secretkey;
    DatagramSocket serverSocket = new DatagramSocket(serverPort);

    DatagramPacket serverPacket=new DatagramPacket(data1,data1.length);
    System.out.println("Far_Server is waiting for packet");
    serverSocket.receive(serverPacket);
    System.out.println("Far_Server received one packet from VPN_server");

    ByteArrayInputStream bais=new ByteArrayInputStream(serverPacket.getData());
    ObjectInputStream oos = new ObjectInputStream(bais);

    secretkey=(SecretKey)oos.readObject();

    Cipher cipher=Cipher.getInstance("Blowfish");
    System.out.println("the key is "+secretkey.toString());
    int guest1Port; // Port to start serverSocket on
    InetAddress guest1Address;
    InetAddress destAddress;
    String sData1;
    String sData2;
    int desPort;
    System.out.println("server2 is seting up ");

    while(true)
    {
    System.out.println("*****************************" );
    byte[] Data1 = new byte[MAXIMUM_SIZE];
    byte[] Data2 = new byte[MAXIMUM_SIZE];
    DatagramPacket Packet1 = new DatagramPacket(Data1, Data1.length);
    DatagramPacket Packet2 = new DatagramPacket(Data2, Data2.length);

    FireWall fw=new FireWall();
    serverSocket.receive(Packet1);
    System.out.println("server2 receive a packet ");

    guest1Address=Packet1.getAddress();
    destAddress=guest1Address;
    guest1Port=Packet1.getPort();

    if(fw.getFrom()<= guest1Port && guest1Port<= fw.getTo()){
    byte[] Data3 = new byte[MAXIMUM_SIZE];
    DatagramPacket Packet3 = new DatagramPacket(Data3, Data3.length);

    String mess="you are not suppose to have interaction with this user";
    ByteArrayOutputStream bStream1 = new ByteArrayOutputStream();
    ObjectOutputStream oop1=new ObjectOutputStream(bStream1);
    oop1.writeObject(mess);


    Data3 = bStream1.toByteArray();

    Packet3.setData(Data3);

    Packet3.setAddress(InetAddress.getByName("127.0.0. 1"));
    Packet3.setPort(guest1Port);


    serverSocket.send(Packet3);
    }
    else{

    if(guest1Port == nextServerPort )
    {
    byte[] Data3 = new byte[MAXIMUM_SIZE];
    DatagramPacket Packet3 = new DatagramPacket(Data3, Data3.length);
    System.out.println("*****************************" );
    SerializablePacket sp=new SerializablePacket();

    //cipher.init(Cipher.DECRYPT_MODE,secretkey);
    cipher.init(Cipher.DECRYPT_MODE, secretkey);

    bais=new ByteArrayInputStream(cipher.doFinal(Packet1.getDat a()));
    oos = new ObjectInputStream(bais);

    sp=(SerializablePacket)oos.readObject();

    String mess=sp.getContain();
    desPort=sp.getPort();

    ByteArrayOutputStream bStream1 = new ByteArrayOutputStream();
    ObjectOutputStream oop1=new ObjectOutputStream(bStream1);
    oop1.writeObject(mess);


    Data3 = bStream1.toByteArray();

    Packet3.setData(Data3);
    Packet3.setAddress(InetAddress.getByName("127.0.0. 1"));
    Packet3.setPort(desPort);


    serverSocket.send(Packet3);

    System.out.println("server2 sends a packet ");

    }

    if(guest1Port == clientPort1 )
    {
    byte[] Data4 = new byte[MAXIMUM_SIZE];
    DatagramPacket Packet4 = new DatagramPacket(Data4, Data4.length);
    System.out.println("*****************************" );

    ByteArrayInputStream bais1=new ByteArrayInputStream(Packet1.getData());
    ObjectInputStream oos1 = new ObjectInputStream(bais1);


    sData2=(String)oos1.readObject();
    InetAddress remote_addr =InetAddress.getLocalHost();
    SerializablePacket sp1=new SerializablePacket(remote_addr,clientPort2,MAXIMUM _SIZE);
    sp1.setContain(sData2);

    ByteArrayOutputStream bStream1 = new ByteArrayOutputStream();
    ObjectOutputStream oop1=new ObjectOutputStream(bStream1);
    oop1.writeObject(sp1);
    System.out.println("packet is going inside of another packet ");
    System.out.println();
    Data4 =bStream1.toByteArray();

    Packet4.setData(Data4);
    Packet4.setAddress(InetAddress.getByName("127.0.0. 1"));
    Packet4.setPort(nextServerPort);
    serverSocket.send(Packet4);
    System.out.println("server2 sends a packet ");
    bStream1.close();
    oop1.close();
    }



    }


    }
    }

    }
    ///////////////////////////////////////////////////////////////////////////////////////////////

    and th error is giving me :
    javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.CipherCore.doFinal(CipherC ore.java:966)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherC ore.java:824)
    at com.sun.crypto.provider.BlowfishCipher.engineDoFin al(BlowfishCipher.java:319)
    at javax.crypto.Cipher.doFinal(Cipher.java:2121)
    at server2.Far_away_Server.main(Far_away_Server.java: 120)
    Last edited by puriaah; 09-22-2014 at 08:02 AM.

Similar Threads

  1. Replies: 3
    Last Post: 06-03-2012, 09:48 AM
  2. Replies: 1
    Last Post: 06-03-2012, 03:07 AM
  3. Replies: 0
    Last Post: 11-27-2011, 02:41 PM
  4. Replies: 2
    Last Post: 09-24-2010, 11:31 AM
  5. Replies: 1
    Last Post: 10-05-2008, 03:12 PM

Tags for this Thread

Posting Permissions

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