簡體   English   中英

如何使用BouncyCastle Lightweight API使用PBE AES加密/解密文件?

[英]How to encrypt/decrypt files with PBE AES using BouncyCastle Lightweight API?

我正在嘗試使用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:填充塊已損壞
在org.bouncycastle.crypto.paddings.PKCS7Padding.padCount(未知來源)
在org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(未知來源)

我該怎么辦才能消除此錯誤? 而且我必須對此功能進行更改才能獲得加密文件的功能。

最后,我發現了問題,我還沒有初始化aesCipher。 當我添加方法aesCipher.init(true, aesCBCParams); it started working. 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);
            }
        }

您的填充有問題。 這可能意味着傳入的密文是使用不同的填充而不是PKCS7加密的。 這可能意味着傳入的密文以不同的模式(不是CBC)進行了加密。 這可能意味着您使用了錯誤的密鑰,因此最后一個塊被隨機解密。 如果您的消息只有一個塊長,則可能意味着您的IV錯誤,因此填充已損壞。

您需要檢查兩端的鍵,模式,填充和IV是否相同。 這意味着逐字節檢查密鑰和IV。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM