我正在尝试使用以下代码解密字节数组。为了简洁起见,我省略了异常处理和其他实践:
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
byte[] key = getKey(); \\Assume it is implemented.
byte[] iv = getIv(); \\Assume it is implemented;
SecretKeySpec sc = new SecretKeySpec(key, "AES");
cipher.init(Cipher.DECRYPT_MODE, sc, new IvParameterSpec(iv));
byte[] encrypted = getBytesFromFile(); \*Assume it is implemented. Simply reads bytes from a binary file into a byte array and returns them as are.*\
byte[] clear = new byte[cipher.getOutputSize(encrypted.length)];
int processed = cipher.doFinal(encrypted, 0, encrypted.length, clear, 0);注意: PKCS7Padding在Java中本机不受支持,但我通过添加securtiy实现了它的工作。为了便于论证,PKCS5Padding也有同样的问题.
import org.bouncycastle.jce.provider.BouncyCastleProvider;问题:
doFinal抛出一个BadPaddingException: pad块损坏的。但是,如果我用update替换doFinal,即:
int processed = cipher.update(encrypted, 0, encrypted.length, clear, 0); 它工作得很完美。结果和预期的一样。
有些人能帮助我理解什么是不同之处,以及我如何使doFinal工作吗?如果需要更多的信息,请告诉我。
发布于 2016-02-29 03:08:27
您没有显示加密,最好的选择是,填充确实是不正确的。若要在不使用PKCS7Padding的情况下检查此解密,您将能够看到填充,并确定其是否正确。
错误出现在doFinal中,因为这是检查填充的地方,如果正确的话,就移除填充。
这样做,并放置解密数据的he转储(十六进制,因为填充将是在0x01-0x10范围内的字节。
https://stackoverflow.com/questions/35689983
复制相似问题