Quiero mandar un mensaje (en este caso simplemente "123456") cifrado desde el cliente al servidor para que este último lo pueda descifrar. Todo ello lo hago con una clave simétrica que poseen los dos. El objetivo con esto sería de una manera básica saber que el cliente es quien dice ser. En cambio al descifrar el mensaje en el lado del servidor me da un error. (Código abajo)
Código de cliente:
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 que aparece al intentar descifrar (el mensaje para cifrar en el cliente no tiene ningún problema):
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)