windows上加解密正常,linux上加密正常,解密時發生 如下異常,異常信息如下:
[ERROR] 2018-10-15 09:30:35,998 method:com.iscas.ippc.common.utils.RandomPwd.decrypt(RandomPwd.java:120)
decrypt Exception is Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
at javax.crypto.Cipher.doFinal(Cipher.java:2164)
Des修改方式如下:
private static SecretKeySpec getSecretKey() {
//返回生成指定算法密鑰生成器的 KeyGenerator 對象
KeyGenerator kg = null;
try {
kg = KeyGenerator.getInstance(KEY_ALGORITHM);
//DES 要求密鑰長度為 256
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(KEY_STRING.getBytes());
kg.init(56,random);
//生成一個密鑰
SecretKey secretKey = kg.generateKey();
return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);// 轉換為DES專用密鑰
} catch (NoSuchAlgorithmException e) {
logger.error("getSecretKey Exception is "+e.getMessage(),e);
}
return null;
}
主要是SecureRandom 紅色部分的問題,
修改前:
kg.init(56,new SecureRandom(key.getBytes()));
修改成下面的代碼即可:
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(KEY_STRING.getBytes());