简体   繁体   English

javax.crypto.BadPaddingException:填充块损坏的异常

[英]javax.crypto.BadPaddingException: pad block corrupted exception

I get 我懂了

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)

when I'm trying to run an application between a Client and a Server . 当我尝试在ClientServer之间运行应用程序时。

The Server class: 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]);
    }
}

The Client class: 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]);
}
}

You cannot just treat ciphertext as characters. 您不能仅将密文视为字符。 You will lose data if you do so. 如果这样做,您将丢失数据。 To convert ciphertext to a String you should use an codec - for instance base 64. 要将密文转换为字符串,您应该使用编解码器-例如base 64。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javax.crypto.BadPaddingException:pad块损坏 - javax.crypto.BadPaddingException: pad block corrupted Android javax.crypto.BadPaddingException:填充块已损坏 - Android javax.crypto.BadPaddingException: pad block corrupted 文件解密失败:javax.crypto.BadPaddingException:填充块已损坏 - Decryption of file fails: javax.crypto.BadPaddingException: pad block corrupted javax.crypto.BadPaddingException:填充块有时损坏 - javax.crypto.BadPaddingException: pad block corrupted sometimes 错误:javax.crypto.BadPaddingException:解密时填充块损坏 - Error : javax.crypto.BadPaddingException: pad block corrupted while Decryption Java Blowfish CBC 解密 javax.crypto.BadPaddingException:填充块已损坏 - Java Blowfish CBC Decryption javax.crypto.BadPaddingException: pad block corrupted 函数解密引发javax.crypto.BadPaddingException:android中的类SimpleCrypto中的填充块已损坏 - function decrypt throws javax.crypto.BadPaddingException: pad block corrupted in class SimpleCrypto in android 解密图像时,给出javax.crypto.BadPaddingException:填充块损坏的Android - When Decrypting Image, gives javax.crypto.BadPaddingException: pad block corrupted Android AES128解密:javax.crypto.badpaddingexception pad块损坏 - AES128 Decryption :javax.crypto.badpaddingexception pad block corrupted javax.crypto.BadPaddingException:未知的块类型 - javax.crypto.BadPaddingException: unknown block type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM