javax.crypto.BadPaddingException: pad block corrupted 异常
javax.crypto.BadPaddingException: pad block corrupted exception
我明白了
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
class:
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
class:
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]);
}
}
你不能只把密文当作字符。如果这样做,您将丢失数据。要将密文转换为字符串,您应该使用编解码器 - 例如 base 64。
我明白了
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
class:
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
class:
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]);
}
}
你不能只把密文当作字符。如果这样做,您将丢失数据。要将密文转换为字符串,您应该使用编解码器 - 例如 base 64。