我使用了来自online.UDP客户端的示例AES-GCM示例java代码,必须使用AES-GCM模式加密数据并发送到服务器.UDP服务器必须接收和解密它。当我这样做时,我有两个问题
1.我向服务器发送“hi”消息(明文为2字节,加密后为18字节)。之后,服务器收到了我们关注的消息
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???)
为什么两者的大小不一样???
2.然后,当尝试解密30字节数据(?)时,在以下行中得到异常
byte[] plainText = cipher.doFinal(cipherText);
示例客户端:
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();
}
}
示例服务器:
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!
}
}
}
答案 0 :(得分:0)
我不知道为什么字节内容大小不同。但是,当我使用以下代码
时它解决了 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
byte[] rec=receivePacket.getData();
byte[] cipherText=new byte[receivePacket.getLength()];
System.arraycopy(rec, 0, cipherText, 0, receivePacket.getLength());
而不是
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
byte[] rec=receivePacket.getData();
String receivedData = new String(rec,0,receivePacket.getLength());
并解决了javax.crypto.AEADBadTagException:标签不匹配异常