2015-05-22 43 views
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) 

当我尝试运行ClientServer之间的应用程序。

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]); 
} 
} 
+0

您是否尝试打印'final_plaintext'并查看所得结果? – RealSkeptic

+0

我将final_plaintext改为String,现在我在线程“main”中得到Exception Exception:javax.crypto.BadPaddingException:pad block corrupted – user3562222

+0

你究竟如何改变它?最好的,如果你编辑你的问题,并添加标题为“编辑:改变这个......”或类似的新代码,所以我们可以看到新的代码。 – RealSkeptic

回答

1

你不能只是把密文的字符。如果您这样做,您将丢失数据。要将密文转换为字符串,您应该使用编解码器 - 例如base 64.

+0

谢谢!解决了! – user3562222