I used sample AES-GCM sample java code from online.UDP client have to encrypt data using AES-GCM mode and send to sever.UDP server have to receive and decrypt it. I have two problem when i do that
1.I sent "hi" message(plaintext was 2 bytes and after encrypted it was 18 bytes) to server. After ,server received message we do following
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
byte[] rec=receivePacket.getData();
String receivedData = new String(rec,0,receivePacket.getLength());
System.out.println(receivePacket.getLength());//18 bytes
System.out.println(receivedData.length);// 30 bytes(how???)
why both are not same size???
2.then,when try to decrypt 30 byte data(?), Got exception in following line
byte[] plainText = cipher.doFinal(cipherText);
sample client:
class GCMClient
{
// AES-GCM parameters
public static final int AES_KEY_SIZE = 128; // in bits
public static final int GCM_NONCE_LENGTH = 12; // in bytes
public static final int GCM_TAG_LENGTH = 16; // in bytes
public static void main(String args[]) throws Exception{
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("192.168.1.8");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
byte[] input = "hi".getBytes(); //2 bytes
byte[] keyBytes ="qwertyuiopasdfgh".getBytes();
SecretKey key = new SecretKeySpec(keyBytes, 0, keyBytes.length, "AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");
byte[] nonce = new byte[GCM_NONCE_LENGTH];
nonce = "poiuytrewqlk".getBytes();;;
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, nonce);
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
byte[] aad = "Whatever I like".getBytes();;
cipher.updateAAD(aad);
byte[] cipherText = cipher.doFinal(input);
System.out.println(cipherText.length+ "data sent!!!!!!! "); //18 bytes after encryption
DatagramPacket sendPacket = new DatagramPacket(cipherText, cipherText.length, IPAddress, 9999);
clientSocket.send(sendPacket);
clientSocket.close();
}
}
sample server:
class GCMServer
{
// AES-GCM parameters
public static final int AES_KEY_SIZE = 128; // in bits
public static final int GCM_NONCE_LENGTH = 12; // in bytes
public static final int GCM_TAG_LENGTH = 16; // in bytes
public static void main(String args[]) throws Exception{
try{
DatagramSocket serverSocket = new DatagramSocket(9999,InetAddress.getByName("192.168.1.8"));
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true){
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
receivePacket.setData(new byte[4096]);
serverSocket.receive(receivePacket);
byte[] rec=receivePacket.getData();
String receivedData = new String(rec,0,receivePacket.getLength());
byte[] cipherText = receivedData.getBytes();
System.out.println("received packet size before convert to bytes "+receivePacket.getLength());//it displays 18
System.out.println("received packet size after convert to bytes "+cipherText.length);//it display 30 how???? it must be 18
byte[] keyBytes ="qwertyuiopasdfgh".getBytes();
SecretKey key = new SecretKeySpec(keyBytes, 0, keyBytes.length, "AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");
byte[] nonce = new byte[GCM_NONCE_LENGTH];
nonce = "poiuytrewqlk".getBytes();;;
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, nonce);
byte[] aad = "Whatever I like".getBytes();;
cipher.init(Cipher.DECRYPT_MODE, key, spec);
cipher.updateAAD(aad);
byte[] plainText = cipher.doFinal(cipherText);
System.out.println("After decryption "+new String(plainText));
}
}catch(Exception e){
System.out.println("Exception caught "+e);//got Exception caught javax.crypto.AEADBadTagException: Tag mismatch!
}
}
}
I dont know that why byte content size were different.But,it solved when i use following code
instead
and solved javax.crypto.AEADBadTagException: Tag mismatch exception