cswong 0 Newbie Poster

I need some help to make AES work with UDP. I keep getting this error.

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length
must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at AES.decrypt(AES.java:31)
at EchoServer.main(EchoServer.java:26)

It seems that upon receiving the UDP AES encrypted message from client, it has problem decrypting due to the byte[] which is being received from UDP transmission. I have also no idea whether am I doing it the right way.

What I mean is that, is there any way which I can do to ensure that the decryption input byte is always in multiples of 16? as the input data and size we receive from the UDP socket is always unpredictable?

Can Somebody help and recommend a good way to solve this problem?

//My Main Driver
import java.net.*;  
import java.util.*;  
import java.io.*;  
import java.security.*;  
class EchoServer   
{  
public static void main( String args[] ) throws Exception   
{  
    String ClientID = "client", ServerID = "Server1", Password = "123456789ABCDEF",Ticket ="", Key = "0123456789ABCDEF";  
    DatagramSocket socket = new DatagramSocket(7);  
    DatagramPacket packet = new DatagramPacket(new byte[1024],1024);  
    Scanner scan = new Scanner(System.in);  
    int ConnectionStatus = 0, port = 0, Nonce = 0;  
    InetAddress ip = null;  
    String message = "", data = "", data1 ="", rdata ="", rdata2 = "";  
    while ( true )   
    {  
        if(ConnectionStatus == 0)  
        {  
            socket.receive( packet );  
            System.out.println( "Recv: "+new Date()+" "+packet.getAddress()+":"+packet.getPort()+" "+new String(packet.getData(),0,packet.getLength()) );  
            socket.send( packet );  
            packet = new DatagramPacket(new byte[1024],1024);  
            socket.receive( packet );  
            rdata2 = new String(packet.getData(),0,packet.getLength());  
            rdata = AES.decrypt(rdata2.getBytes("UTF-8"),Key); // the Line that is giving the problem. AES Driver is in another code.  
            System.out.print("Received: "+ rdata);  
            Date TimeStamp = new Date();  
            Date tTime = new Date();  
            Ticket = tTime.toString() + "," + 100000;  
            data = ServerID +" "+ TimeStamp.toString() +" "+ Ticket;  
            byte[] buffer1 = AES.encrypt(data,Key);  
            ip = packet.getAddress();  
            port = packet.getPort();  
            packet = new DatagramPacket(buffer1,buffer1.length,ip,port);  
            socket.send(packet);  
            ConnectionStatus++;  
        }  
        else  
        {  
            packet = new DatagramPacket(new byte[512],512);  
            socket.receive( packet );  
            ip = packet.getAddress();  
            port = packet.getPort();  
            System.out.println( "Recv: "+new Date()+" "+packet.getAddress()+":"+packet.getPort()+" "+new String(packet.getData(),0,packet.getLength()) );  
            System.out.println("Message:");  
            message = scan.nextLine();  
            byte[] buffer = message.getBytes();  
            packet = new DatagramPacket(buffer,buffer.length,ip,port);  
            socket.send( packet );  
         }  
     }  
 }  
}



import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AES 
{ 
    static String IV = "AAAAAAAAAAAAAAAA";

    public static byte[] encrypt(String plainText, String encryptionKey) throws Exception 
    {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
        return cipher.doFinal(plainText.getBytes("UTF-8"));
    }

    public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception
    {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
        return new String(cipher.doFinal(cipherText),"UTF-8");
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.