【问题标题】:AES decryption error in javajava中的AES解密错误
【发布时间】:2011-09-02 20:01:16
【问题描述】:

我正在研究 AES 算法,我得到了这个异常:

javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at org.enterprisepower.io.MyEncryptTools.decrypt(MyEncryptTools.java:41)
at org.enterprisepower.io.IOUtils.copyStream(IOUtils.java:132)
at org.enterprisepower.net.portforward.Processor$Copier.run(Processor.java:99)
at java.lang.Thread.run(Unknown Source)

异常发生在解密部分。我在 myClient 程序中加密了一条消息并将 cipherMessage 发送到 myServer 程序。服务器收到 cipherMessage 后会引发上述异常,但在客户端我可以解密完全相同的 cipherMessage。(我检查这与在两侧打印字节...)

这些是我的代码:

//It's decrypt method for both client and server
public  byte[] decrypt(byte[] encryptedData, int length) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] enc = new byte[length];
    for (int i = 0; i < length; i++) {
        enc[i] = encryptedData[i];
    }
    byte[] decordedValue= Base64.decodeBase64(enc);//org.apache.commons.codec.binary.*;
    byte[] decValue = c.doFinal(decordedValue);
    return decValue;
}


//Client side
public static void copyStream(InputStream in, OutputStream out,
        boolean closeOnFinish, boolean encrypt, String password) throws Exception {
    MyEncryptTools mit;
    try {
        mit = new MyEncryptTools(password);
        byte[] buf = new byte[BUF_SIZE];
        byte[] enbuf ;
        int count;
        try {
            if (encrypt) {
                while (((count = in.read(buf)) != -1) && alive) {
                    enbuf = mit.encrypt(buf,count);
                    out.write(enbuf, 0, enbuf.length);
                }
            } else {
                buf = new byte[172];
                while (((count = in.read(buf)) != -1) && alive) {
                    enbuf = mit.decrypt(buf, count);
                    out.write(enbuf, 0, enbuf.length);
                }
            }
        } finally {
            if (closeOnFinish)
                close(in);
            if (closeOnFinish)
                close(out);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } 
}



//Server side
public static void copyStream(InputStream in, OutputStream out,
        boolean closeOnFinish, boolean encrypt, String user, String password) throws Exception {
    MyEncryptTools mit;
    try {
        mit = new MyEncryptTools(password);

        byte[] buf = new byte[BUF_SIZE];
        byte[] enbuf ;
        int count;
        try {
            if (encrypt) {
                System.out.println("Encrypt;");
                while (((count = in.read(buf)) != -1)) {
                    enbuf = mit.encrypt(buf,count);
                    out.write(enbuf, 0, enbuf.length);
                }
            } else {
                buf = new byte[172];
                while (((count = in.read(buf)) != -1)) {
                    enbuf = mit.decrypt(buf, count);
                    out.write(enbuf, 0, enbuf.length);
                }
            }
        } finally {
            if (closeOnFinish)
                close(in);
            if (closeOnFinish)
                close(out);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } 
}

我应该提一下,我使用 117 字节的数组进行加密,使用 172 字节的数组进行解密。

【问题讨论】:

    标签: java aes


    【解决方案1】:

    使用美妙的 CipherInputStream 并忘记缓冲区长度。

    【讨论】:

    • 它看起来很棒......我该如何使用它?你有示例代码吗?
    • @M.S.只需执行 'CipherInputStream cis = new CipherInputStream(, );'然后从 cis 读取到输出流。在此处查看更多示例:'google.com/…:^java$&type=cs'。
    • 感谢您的回答。我这样做了,但效果不佳。经过几行后,它停止了。我使用 AES 算法和 16 个字符作为密钥的 Cipher 类。您对此有任何想法吗?
    • 我想添加一些东西:编辑我的程序后,它会解密这些行: GET / HTTP/1.1 主机:localhost:3333 连接:keep-alive Cache-Control:max-age=0 User-Agent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.218 Safari/535.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q 但它停止了此时每次都不要解密这个字符:=0.7,*;q=0.3 我不知道问题出在哪里...
    • 我发现问题是文件结尾,cipherInputStream 看不懂。
    【解决方案2】:

    您的错误消息说:“给定最终块未正确填充”。要修复错误,请为加密或解密指定 PKCS7 填充。或者切换到不需要填充的 CTR 模式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多