Java 给定的最终块未正确填充异常

Java 给定的最终块未正确填充异常,java,encryption,Java,Encryption,我有服务器客户端套接字将字节数组从客户端发送到服务器,我正在尝试使用密码输入流、密码输出流,但出现以下异常: javax.crypto.BadPaddingException:给定的最终块不正确 填充的。线程“main”java.io.IOException中的异常: javax.crypto.BadPaddingException:给定的最终块不正确 填补 javax.crypto.cipherPutStream.getMoreData(cipherPutStream.java:121)位于

我有服务器客户端套接字将字节数组从客户端发送到服务器,我正在尝试使用密码输入流、密码输出流,但出现以下异常:

javax.crypto.BadPaddingException:给定的最终块不正确 填充的。线程“main”java.io.IOException中的异常: javax.crypto.BadPaddingException:给定的最终块不正确 填补 javax.crypto.cipherPutStream.getMoreData(cipherPutStream.java:121)位于 getMoreData(CipherInputStream.java:121) 在javax.crypto.cipheriputstream.read(cipheriputstream.java:239) 在javax.crypto.cipheriputstream.read(cipheriputstream.java:215) 在SecretSocketServer.main(SecretSocketServer.java:46)上,由以下原因引起: javax.crypto.BadPaddingException:给定的最终块不正确 填补 com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)位于 com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)位于 com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314) 位于javax.crypto.Cipher.doFinal(Cipher.java:2048) getMoreData(CipherInputStream.java:118)

我尝试了与“DES”不同的算法,但仍然得到相同的异常 这是我的代码:

公共类SecretSocket{
密码输入,密码输出;
插座;
钥匙;
public SecretSocket(套接字套接字,密钥)抛出NoSuchAlgorithmException、NoSuchPaddingException、InvalidKeyException{
this.key=key;
this.socket=socket;
初始化密码();
}
private void initializeCipher()抛出NoSuchAlgorithmException、NoSuchPaddingException、InvalidKeyException{
outCipher=Cipher.getInstance(“DES”);
init(Cipher.ENCRYPT_模式,密钥);
inCipher=Cipher.getInstance(“DES”);
inCipher.init(Cipher.DECRYPT_模式,密钥);
}
公共InputStream getInputStream()引发IOException{
InputStream=socket.getInputStream();
CipherInputStream cis=新的CipherInputStream(is,inCipher);
返回CI;
}
public OutputStream getOutputStream()引发IOException{
OutputStream os=socket.getOutputStream();
CipherOutputStream cos=新的CipherOutputStream(os,outCipher);
返回cos;
}
}
公共类密钥生成{
public static void writeKey()抛出NoSuchAlgorithmException、FileNotFoundException、IOException{
KeyGenerator kg=KeyGenerator.getInstance(“DES”);
Key=kg.generateKey();
File File=新文件(“key1.txt”);
FileOutputStream fos=新的FileOutputStream(文件);
ObjectOutputStream oos=新的ObjectOutputStream(fos);
oos.writeObject(键);
}
公共类SecretSocketServer{
公共静态void main(字符串[]args)抛出IOException、FileNotFoundException、ClassNotFoundException、NoSuchAlgorithmException、NoSuchPaddingException、InvalidKeyException{
int端口=12345;
服务器套接字服务器套接字;
插座客户端插座;
Serversocket=新的Serversocket(端口);
System.out.println(“等待客户端连接”);
clientSocket=Serversocket.accept();
System.out.println(“新客户端连接”);
Key=KeyGen.getSecretKey();
System.out.println(“键为:“+Key”);
secretsockets=新SecretSocket(clientSocket,key);
InputStream in=s.getInputStream();
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
字节[]b=新字节[1024];
int numberOfBytedRead;
而((numberOfBytedRead=in.read(b))>=0){
写入(b,0,numberOfBytedRead);
}
System.out.println(新字符串(baos.toByteArray());
Serversocket.close();
}
}
公共静态最终字符串KEY\u FILE=“key1.txt”;
公共静态密钥getSecretKey()抛出FileNotFoundException、IOException、ClassNotFoundException{
FileInputStream fis=新的FileInputStream(密钥文件);
ObjectInputStream ois=新ObjectInputStream(fis);
Key=(Key)ois.readObject();
返回键;
}
公共类SecretSocketClient{
公共静态void main(字符串[]args)抛出IOException、NoSuchAlgorithmException、ClassNotFoundException、NoSuchPaddingException、InvalidKeyException{
int端口=12345;
Socket soc=新套接字(“本地主机”,端口);
System.out.println(“连接到服务器”);
KeyGen.writeKey();
Key=KeyGen.getSecretKey();
System.out.println(“生成的键:+Key”);
secretsockets=新SecretSocket(soc,密钥);
//InputStream in=s.getInputStream();
OutputStream out=s.getOutputStream();
out.write(“HELLOWORLD.getBytes());
out.flush();
out.close();
soc.close();
System.out.println(“消息已发送”);
}
}

有许多因素会导致“错误填充”异常。基本上,任何导致最后一个块的结尾与预期填充不匹配的因素都会引发错误。可能的原因包括:不正确的填充设置、不正确的键、损坏的cyphertext等

要尝试并诊断问题,请将解密端设置为
NoPadding
。这将接受任何内容,并允许您检查输出:

  • 完全垃圾:您可能有一个关键错误或错误的模式设置

  • 第一块垃圾:您可能有密钥错误或IV错误

  • 最后一个块垃圾:可能是cyphertext文件的损坏端

  • 正确的解密,结尾有一些奇怪的字节:奇怪的字节是填充

如果它确实只是填充,那么将解密函数设置为期望这种填充。否则,请检查密钥/IV/cyphertext对于两个enc是否都是相同的