java异常:Linux中的“给定的最后一个块未正确填充”,但它在Windows中工作
这可以是Exception: "Given final block not properly padded" in Linux, but it works in Windows的副本,但不能完全复制
我的应用程序在windows中工作,但在Linux中失败
Given final block not properly padded exception
配置:
JDK版本:1.8u162
Windows:版本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: Given final block not properly padded
在下一行:
while ((numRead = in.read(buf)) >= 0) {
现在,正如Maarten Bodewes在{a1}中所建议的那样,我已经在使用PBKDF2作为PBEWith*,实际上是PBKDF2+加密方案(带PKCS5Padding的CBC模式)。那么,在我的例子中,为什么在Windows上完成的加密在Linux上不起作用呢?有人能帮忙吗
共 (0) 个答案