java - 例外:Linux 中的“给定最终块未正确填充”,但它在 Windows 中有效
问题描述
这可能是Exception: "Given final block not proper padded" 在 Linux 中的重复,但它在 Windows 中有效,但并非完全有效。
我的应用程序在 Windows 中工作,但在 Linux 中失败
给定最终块未正确填充异常
配置:
JDK版本:1.8u162
视窗:版本 10
Linux:OpenSuSE 42.2 (x86_64)
我的代码如下:
public static Cipher createCipher(int mode, String passPhrase) throws CypherException {
// Salt value
byte[] salt = new byte[128]; // Should be atleast 8 bytes
SecureRandom secRandom = new SecureRandom(passPhrase.getBytes(StandardCharsets.UTF_8));
secRandom.nextBytes(salt); // Self-seeded randomizer for salt
// Iteration count
int iterationCount = 12288;
int derivedKeyLength = 256 ; // Should be atleast longer than 112 bits. Depends on Key size of algorithm.
Cipher cipher;
try {
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount, derivedKeyLength * 8);
SecretKey key = SecretKeyFactory.getInstance(APBEWithHmacSHA512AndAES_256).generateSecret(keySpec);
byte iv[] = new byte[16];
secRandom.nextBytes(iv); // Self-seeded randomizer to generate IV
IvParameterSpec ivSpec = new IvParameterSpec(iv) ; // IvParameterSpec initialized using its own randomizer
// Note: there is no typical transformation string. Algorithm, mode (CBC) and padding scheme (PKCS5Padding) is all taken care by ALGORITHM_NAME.
cipher = Cipher.getInstance(PBEWithHmacSHA512AndAES_256);
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount, ivSpec);
// Create the Cipher
cipher.init(mode, key, paramSpec);
} catch (NoSuchPaddingException nspe) {
throw new CypherException("No such Padding: " + nspe.getMessage());
} catch (NoSuchAlgorithmException nsae) {
throw new CypherException("Algorithm not supported: " + nsae.getMessage());
} catch (InvalidKeyException ike) {
throw new CypherException("Invalid key: " + ike.getMessage());
} catch (InvalidKeySpecException ikse) {
throw new CypherException("Invalid key specification: " + ikse.getMessage());
} catch (InvalidAlgorithmParameterException iape) {
throw new CypherException("Invalid algorithm parameter: " + iape.getMessage());
}
return cipher;
}
public static void decrypt(InputStream in, OutputStream out, String passPhrase) throws IOException, CypherException {
// Bytes read from in will be decrypted
in = new CipherInputStream(in, createCipher(Cipher.DECRYPT_MODE, passPhrase));
int numRead;
// Buffer used to transport the bytes from one stream to another
byte[] buf = new byte[1024];
// Read in the decrypted bytes and write the cleartext to out
try {
while ((numRead = in.read(buf)) >= 0) {
out.write(buf, 0, numRead);
}
} finally {
// close the streams
}
}
我得到
javax.crypto.BadPaddingException:给定最终块未正确填充
在以下行:
while ((numRead = in.read(buf)) >= 0) {
现在,正如Exception: "Given final block not proper padded" in Linux 中所建议的那样,但它在 Windows 中由 Maarten Bodewes 工作我已经使用 PBKDF2 作为 PBEWith*,实际上是 PBKDF2 + 加密方案(带有 PKCS5Padding 的 CBC 模式)。那么在我的情况下,为什么在 Windows 上完成的加密在 Linux 上不起作用?有人可以帮忙吗?
解决方案
推荐阅读
- java - Android:屏幕关闭 - 通知延迟/阻塞
- android - Android Bitmap 空对象引用
- ssl - Hyperledger Fabric Explorer config.json 握手错误
- python - 如何在保持特定顺序的熊猫中分组?
- linux - 无法在 Ubuntu 16.04 上使用 Go 连接到 NetSuite ODBC
- python - 使用 Python 在演示文稿中插入评论
- python - KFold 拆分方法为 DataFrame 返回的索引是 iloc 还是 loc?
- javascript - 如何使用谷歌驱动器嵌入 iframe 在 pdf 查看器中禁用弹出选项
- laravel - Laravel 更改网址名称
- google-cloud-platform - 将数据提取流式传输到 BigQuery