java中的AES解密错误

时间:2011-09-02 09:36:05

标签: java aes

我正在研究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的数组进行解密。

2 个答案:

答案 0 :(得分:2)

使用精彩的CipherInputStream并忘记缓冲区长度。

答案 1 :(得分:1)

您的错误消息显示:“鉴于最终阻止未正确填充”。要修复错误,请指定PKCS7填充以进行加密或解密。或者切换到CTR模式,不需要填充。