完美解決Linux操作系統(tǒng)下aes解密失敗的問題
更新時間:2013年08月28日 09:55:51 作者:
以下是針對在Linux操作系統(tǒng)下關(guān)于AES解密失敗的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下
現(xiàn)象描述:
windows上加解密正常,linux上加密正常,解密時發(fā)生如下異常:
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at chb.test.crypto.AESUtils.crypt(AESUtils.java:386)
at chb.test.crypto.AESUtils.AesDecrypt(AESUtils.java:254)
at chb.test.crypto.AESUtils.main(AESUtils.java:40)
解決方法:
經(jīng)過檢查之后,定位在生成KEY的方法上,如下:
public static SecretKey getKey (String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance( "AES" );
_generator.init(128, new SecureRandom(strKey.getBytes()));
return _generator.generateKey();
} catch (Exception e) {
throw new RuntimeException( " 初始化密鑰出現(xiàn)異常 " );
}
}
修改到如下方式,問題解決:
public static SecretKey getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance( "AES" );
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(strKey.getBytes());
_generator.init(128,secureRandom);
return _generator.generateKey();
} catch (Exception e) {
throw new RuntimeException( " 初始化密鑰出現(xiàn)異常 " );
}
}
原因分析
SecureRandom 實(shí)現(xiàn)完全隨操作系統(tǒng)本身的內(nèi)部狀態(tài),除非調(diào)用方在調(diào)用 getInstance 方法之后又調(diào)用了 setSeed 方法;該實(shí)現(xiàn)在 windows 上每次生成的 key 都相同,但是在 solaris 或部分 linux 系統(tǒng)上則不同。
windows上加解密正常,linux上加密正常,解密時發(fā)生如下異常:
復(fù)制代碼 代碼如下:
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at chb.test.crypto.AESUtils.crypt(AESUtils.java:386)
at chb.test.crypto.AESUtils.AesDecrypt(AESUtils.java:254)
at chb.test.crypto.AESUtils.main(AESUtils.java:40)
解決方法:
經(jīng)過檢查之后,定位在生成KEY的方法上,如下:
復(fù)制代碼 代碼如下:
public static SecretKey getKey (String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance( "AES" );
_generator.init(128, new SecureRandom(strKey.getBytes()));
return _generator.generateKey();
} catch (Exception e) {
throw new RuntimeException( " 初始化密鑰出現(xiàn)異常 " );
}
}
修改到如下方式,問題解決:
復(fù)制代碼 代碼如下:
public static SecretKey getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance( "AES" );
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(strKey.getBytes());
_generator.init(128,secureRandom);
return _generator.generateKey();
} catch (Exception e) {
throw new RuntimeException( " 初始化密鑰出現(xiàn)異常 " );
}
}
原因分析
SecureRandom 實(shí)現(xiàn)完全隨操作系統(tǒng)本身的內(nèi)部狀態(tài),除非調(diào)用方在調(diào)用 getInstance 方法之后又調(diào)用了 setSeed 方法;該實(shí)現(xiàn)在 windows 上每次生成的 key 都相同,但是在 solaris 或部分 linux 系統(tǒng)上則不同。
相關(guān)文章
Linux系統(tǒng)下如何查看及修改文件讀寫權(quán)限
linux下查看文件權(quán)限的命令2008-05-05Ubuntu系統(tǒng)中部署Vagrant和VirtualBox的圖文教程
這篇文章主要介紹了Ubuntu系統(tǒng)中部署Vagrant和VirtualBox的圖文教程,需要的朋友可以參考下2018-04-04SYN Cookie在Linux內(nèi)核中的實(shí)現(xiàn)
SYN Cookie在Linux內(nèi)核中的實(shí)現(xiàn)...2006-10-10ubuntu取消pppoe啟動時自動拔號的設(shè)置方法
昨晚用pppoe撥號,今早回公司發(fā)現(xiàn)原先的dhcp設(shè)置不生效了,所以取消pppoe啟動時自動拔號2008-09-09Linux C中庫函數(shù)與系統(tǒng)調(diào)用的區(qū)別詳細(xì)解析
以下是對Linux下C中庫函數(shù)和系統(tǒng)調(diào)用的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08