如何使用BouncyCastle轻量级API使用PBE AES加密/解密文件?

时间:2011-11-10 11:51:13

标签: java encryption cryptography aes bouncycastle

我正在尝试使用AES使用PBE加密/解密文件。我正在使用Bouncy Casle库(轻量级API),因为我需要忽略对密钥长度的限制。我找到了函数并更改了一些代码。

public void decryptLW(InputStream in, OutputStream out, String password, byte[] salt, final int iterationCount) throws Exception {

    PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(new SHA256Digest());
    char[] passwordChars = password.toCharArray();
    final byte[] pkcs12PasswordBytes = PBEParametersGenerator.PKCS12PasswordToBytes(passwordChars);
    pGen.init(pkcs12PasswordBytes, salt, iterationCount);
    CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine());
    ParametersWithIV aesCBCParams = (ParametersWithIV) pGen.generateDerivedParameters(256, 128);
    aesCBC.init(false, aesCBCParams);
    PaddedBufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding());

    try {

        // Read in the decrypted bytes and write the cleartext to out
        int numRead = 0;
        while ((numRead = in.read(buf)) >= 0) {

            byte[] plainTemp = new byte[aesCipher.getOutputSize(buf.length)];
            int offset = aesCipher.processBytes(buf, 0, buf.length, plainTemp, 0);
            int last = aesCipher.doFinal(plainTemp, offset);
            final byte[] plain = new byte[offset + last];
            System.arraycopy(plainTemp, 0, plain, 0, plain.length);

            out.write(plain, 0, numRead);
        }
        out.close();
        in.close();
    } catch (java.io.IOException e) {
    }

}

我有一个错误:

  

org.bouncycastle.crypto.InvalidCipherTextException:pad block corrupted
    在org.bouncycastle.crypto.paddings.PKCS7Padding.padCount(未知来源)
    在org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(未知来源)

我该怎么做才能删除此错误?我必须在此功能中更改以获得加密文件的能力。

2 个答案:

答案 0 :(得分:1)

最后,我发现问题,我没有初始化aesCipher。当我添加方法aesCipher.init(true, aesCBCParams); it started working.

我也更改了一些代码:

int numRead = 0;
        while ((numRead = fin.read(buf)) >= 0) {
            if (numRead == 1024) {
                byte[] plainTemp = new byte[aesCipher.getUpdateOutputSize(numRead)];
                int offset = aesCipher.processBytes(buf, 0, numRead, plainTemp, 0);

                final byte[] plain = new byte[offset];
                System.arraycopy(plainTemp, 0, plain, 0, plain.length);
                fout.write(plain, 0, plain.length);
            } else {
                byte[] plainTemp = new byte[aesCipher.getOutputSize(numRead)];
                int offset = aesCipher.processBytes(buf, 0, numRead, plainTemp, 0);
                int last = aesCipher.doFinal(plainTemp, offset);
                final byte[] plain = new byte[offset + last];
                System.arraycopy(plainTemp, 0, plain, 0, plain.length);
                fout.write(plain, 0, plain.length);
            }
        }

答案 1 :(得分:0)

您的填充有问题。这可能意味着传入的密文使用不同的填充而不是PKCS7加密。这可能意味着传入的密文以不同的模式(不是CBC)加密。这可能意味着您有错误的密钥,因此最后一个块会随机解密。如果你的消息只有一个块长,那么它可能意味着你有一个错误的IV,所以填充再次损坏。

您需要检查两端的密钥,模式,填充和IV是否相同。这意味着逐字节地检查密钥和IV。