0
我得到javax.crypto.BadPaddingException:墊塊損壞的例外
Exception in thread "main" javax.crypto.BadPaddingException: pad block corrupted at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$BufferedGenericBlockCipher.doFinal(Unknown Source) at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(Unknown Source) at javax.crypto.Cipher.doFinal(Cipher.java:2087) at Server.main(Server.java:67)
當我嘗試運行Client
和Server
之間的應用程序。
的Server
類:
public class Server {
private static SecretKeySpec AES_Key;
private static final String key = "1234567890ABCDEF";
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
AES_Key = new SecretKeySpec(key.getBytes(), "AES");
System.out.println(AES_Key);
Cipher AES_Cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4443);
} catch (IOException e) {
System.err.println("Could not listen on port: 4443.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String inputLine, outputLine;
byte[] input;
System.out.println("Server run ");
while ((input = in.readLine().getBytes()) != null) {
AES_Cipher.init(Cipher.DECRYPT_MODE, AES_Key);
System.out.println(input);
byte plaintext_decrypted[] = AES_Cipher.doFinal(input);
inputLine= toHexString(plaintext_decrypted);
System.out.println("Server receive : "+inputLine);
System.out.println("type message :");
outputLine = stdIn.readLine();
out.println(outputLine);
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
private static String toHexString(byte[] block) {
StringBuffer buf = new StringBuffer();
int len = block.length;
for (int i = 0; i < len; i++) {
byte2hex(block[i], buf);
if (i < len - 1) {
buf.append(":");
}
}
return buf.toString();
}
/*
* Converts a byte to hex digit and writes to the supplied buffer
*/
private static void byte2hex(byte b, StringBuffer buf) {
char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'};
int high = ((b & 0xf0) >> 4);
int low = (b & 0x0f);
buf.append(hexChars[high]);
buf.append(hexChars[low]);
}
}
的Client
類:
public class Client {
private static SecretKeySpec AES_Key;
private static final String key = "1234567890ABCDEF";
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Socket mySocket = null;
PrintWriter out = null;
BufferedReader in = null;
AES_Key = new SecretKeySpec(key.getBytes(), "AES");
System.out.println(AES_Key);
Cipher AES_Cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
try {
mySocket = new Socket("localhost", 4443);
out = new PrintWriter(mySocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
System.out.println("Client run ");
while (true) {
System.out.println("type message :");
AES_Cipher.init(Cipher.ENCRYPT_MODE, AES_Key);
fromUser = stdIn.readLine();
byte plaintext[] = fromUser.getBytes();
byte final_plaintext[] = AES_Cipher.doFinal(plaintext);
// fromUser=toHexString(final_plaintext);
String msg = new String(final_plaintext, "ASCII");
System.out.println(final_plaintext);
if (fromUser != null) {
out.println(msg);
}
else{ break; }
fromServer = in.readLine();
if(fromServer!=null){
System.out.println("Client receive :" + fromServer);
}
else{ break; }
}
out.close();
in.close();
stdIn.close();
mySocket.close();
}
private static String toHexString(byte[] block) {
StringBuffer buf = new StringBuffer();
int len = block.length;
for (int i = 0; i < len; i++) {
byte2hex(block[i], buf);
if (i < len - 1) {
buf.append(":");
}
}
return buf.toString();
}
/*
* Converts a byte to hex digit and writes to the supplied buffer
*/
private static void byte2hex(byte b, StringBuffer buf) {
char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'};
int high = ((b & 0xf0) >> 4);
int low = (b & 0x0f);
buf.append(hexChars[high]);
buf.append(hexChars[low]);
}
}
您是否嘗試打印'final_plaintext'並查看所得結果? – RealSkeptic
我將final_plaintext改爲String,現在我在線程「main」中得到Exception Exception:javax.crypto.BadPaddingException:pad block corrupted – user3562222
你究竟如何改變它?最好的,如果你編輯你的問題,並添加標題爲「編輯:改變這個......」或類似的新代碼,所以我們可以看到新的代碼。 – RealSkeptic