AES 文件解密“给定的最终块未正确填充”

AES File decrypting “given final block not properly padded”

我想使用 AES 加密然后解密文件。我已经阅读了很多关于错误 "Given final block not properly padded" 的主题。但是我没有找到适合我的解决方案。

抱歉指定我的代码的语言,我不知道编写语言java

这是我的代码:

变量

// IV, secret, salt in the same time
private byte[] salt = { 'h', 'u', 'n', 'g', 'd', 'h', '9', '4' };
public byte[] iv;
public SecretKey secret;

创建密钥

public void createSecretKey(String password){
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
    SecretKey tmp = factory.generateSecret(spec);
    secret = new SecretKeySpec(tmp.getEncoded(), "AES");
}

加密方法

public void encrypt(String inputFile){
    FileInputStream fis = new FileInputStream(inputFile);
    // Save file: inputFile.enc
    FileOutputStream fos = new FileOutputStream(inputFile + ".enc");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);

    AlgorithmParameters params = cipher.getParameters();
    // Gen Initialization Vector
    iv = (byte[]) ((IvParameterSpec) params
            .getParameterSpec(IvParameterSpec.class)).getIV();
    // read from file (plaint text)  -----> save with .enc
    int readByte;
    byte[] buffer = new byte[1024];
    while ((readByte = fis.read(buffer)) != -1) {
        fos.write(cipher.doFinal(buffer), 0, readByte);
    }
    fis.close();
    fos.flush();
    fos.close();
}

解密方法

public void decrypt(String inputFile){
    FileInputStream fis = new FileInputStream(inputFile);
    // Save file: filename.dec
    FileOutputStream fos = new FileOutputStream(inputFile.substring(0,
            inputFile.length() - 4) + ".dec");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
    // Read from file encrypted  ---> .dec 
    int readByte;
    byte[] buffer = new byte[1024];
    while ((readByte = fis.read(buffer)) != -1) {
        fos.write(cipher.doFinal(buffer), 0, readByte);
    }
    fos.flush();
    fos.close();
    fis.close();
}

更新

解决方案:buffer的编辑大小是16的倍数。使用CipherInput/Output来读/写文件。

感谢Artjom B.

AES 是一种块密码,因此仅适用于 16 字节的块。 CBC 等操作模式使您能够将多个块链接在一起。诸如 PKCS#5 填充之类的填充使您能够通过将明文填充到块大小的下一个倍数来加密任意长度的明文。

问题是您每 1024 个字节单独加密一次。由于1024划分了块大小,padding在加密前加了一个full块。因此,密文块的长度为 1040 字节。然后在解密过程中,您只读取 1024 缺少填充。 Java 尝试解密它,然后尝试删除填充。如果填充格式错误(因为它不存在),则抛出异常。

轻松修复

只需将用于解密的缓冲区增加到 1040 字节即可。

正确修复

不要在单独的块中加密,而是使用 Cipher#update(byte[], int, int) instead of Cipher.doFinal to update the ciphertext for every buffer you read or use a CipherInputStream.


其他安全注意事项:

您缺少随机 IV。没有它,攻击者可能仅通过观察密文就可以看到您使用相同的密钥加密了相同的明文。

您缺少密文身份验证。没有它,您将无法可靠地检测到密文中的(恶意)更改,并且可能会使您的系统受到攻击,例如填充 oracle 攻击。使用像 GCM 这样的身份验证模式或 运行 您通过 HMAC 创建的密文来创建身份验证标记并将其写入末尾。然后就可以验证标签during/before解密了。

您错误地假设加密数据的长度等于普通数据的长度,但加密的 AES 数据始终是 AES 块大小(16 字节)的倍数,并且可以有一个额外的完整填充块。

处理流加密的最有效方法是使用 JCE 的 CipherOutputStream 和 CipherInputStream (http://docs.oracle.com/javase/7/docs/api/javax/crypto/CipherInputStream.html)。这些 类 为您完成所有工作。

此外,请确保始终将新生成的 IV 保存在您的加密方法中,以便能够将其用于解密。