JAVA的DES加密解密在windows上測試一切正常,在linux上異常


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());

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM