我想从客户端发送到服务器数据加密,以便在服务器端解密。我在客户端“123456”加密,以便通过对称密钥在服务器端解密它。
我的代码是以下代码。
客户代码:
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)
客户端的加密部分没问题但是当服务器要解密来自客户端的数据时会出现这个错误:
onMouseOver
我做错了什么?