Я хочу отправить с клиента на сервер шифрование данных, чтобы расшифровать на стороне сервера.Я шифрую на стороне клиента "123456", чтобы расшифровать его на стороне сервера с помощью симметричного ключа.
Мой код следующий.
Код клиента:
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();
}
Код сервера:
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();
}
}
}
Зашифрованная часть на стороне клиента в порядке, но когда сервер собирается расшифровать данные с клиента, появляется эта ошибка:
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)
Что я делаю не так?