Problem when encrypting and decrypting in client and server

1

I want to send a message (in this case simply "123456") encrypted from the client to the server so that the latter can decipher it. All this I do with a symmetric key that both have. The objective with this would be in a basic way to know that the client is who he claims to be. On the other hand, decrypting the message on the server side gives me an error. (Code below)

Customer code:

public class Client {

public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InterruptedException, InvalidAlgorithmParameterException
{
    int port=56321;
    byte[] ipAddr = new byte[] { 127,0,0,1 };
    InetAddress address = InetAddress.getByAddress(ipAddr);
    System.out.println(address);
    DatagramSocket socket = new DatagramSocket();
    byte[] data = new byte[1024];
    byte[] plainBytes = "123456".getBytes();
    byte[] keySymme = {
            0x74, 0x68, 0x69, 0x73, 0x49, 0x73, 0x41, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b, 0x65, 0x79
    };//"thisIsASecretKey";
    SecretKeySpec secretKey = new SecretKeySpec(keySymme, "AES");
   System.out.println("key"+secretKey);

    // Create Cipher instance and initialize it to encrytion mode
    Cipher cipher = Cipher.getInstance("AES");  // Transformation of the algorithm
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] EncryptedData = cipher.doFinal(plainBytes);

    DatagramPacket packet1=new DatagramPacket(EncryptedData, EncryptedData.length, address, port);//bytes    

    System.out.println("Sending...");
    socket.send(packet1);
    System.out.println("Sent...");
    socket.close();
}   

Server code:

public class Server {
public static void main(String[] args) throws IOException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException
{
    int port=56321;
    int port2;
    byte[] keySymme = {
            0x74, 0x68, 0x69, 0x73, 0x49, 0x73, 0x41, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b, 0x65, 0x79
    };//"thisIsASecretKey";
    SecretKeySpec secretKey = new SecretKeySpec(keySymme, "AES");
    DatagramSocket socket = new DatagramSocket(port);
    DatagramPacket packet = null;
    byte[] data = null;

    while(true){
        data = new byte[1024];
        packet = new DatagramPacket(data, data.length);
        socket.receive(packet);            
        port2=packet.getPort();
        InetAddress address = packet.getAddress();            
        String message=new String(packet.getData());
        System.out.println("Address "+address+" Port "+port2+" Message "+message);
        System.out.println("Listening...");

        byte[] EncryptedData=packet.getData();

        try
        {
            Cipher cipher = Cipher.getInstance("AES");
            // Reinitialize the Cipher to decryption mode
            cipher.init(Cipher.DECRYPT_MODE,secretKey, cipher.getParameters());
            byte[] plainBytesDecrypted = cipher.doFinal(EncryptedData);
            System.out.println("Decrypted data "+plainBytesDecrypted);

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }       

}

Error that appears when trying to decrypt (the message to encrypt in the client does not have any problem):

javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
    at javax.crypto.Cipher.doFinal(Cipher.java:2164)
    at Server.main(Server.java:48)
    
asked by Nfernandez 22.05.2018 в 17:41
source

1 answer

1

It's a very simple bug to fix: In the code of your server you have

byte[] plainBytesDecrypted = cipher.doFinal(EncryptedData);

That is, you are trying to decrypt 1024 bytes because it is the size of the buffer with which you initialized the datagram. The correct way to do it would be the following, specifying all the necessary information:

byte[] plainBytesDecrypted = cipher.doFinal(packet.getData(),
       packet.getOffset(), packet.getLength());

System.out.println("Decrypted data " + new String(plainBytesDecrypted));
    
answered by 22.05.2018 / 17:50
source