<code id="qf3hh"></code>
  • <menuitem id="qf3hh"></menuitem>
  • <strike id="qf3hh"><label id="qf3hh"></label></strike>

  • ?
      開發(fā)技術 / Technology

      完美解決Linux操作系統(tǒng)下aes解密失敗的問題

      日期:2015年3月17日  作者:zhjw  來源:互聯(lián)網(wǎng)    點擊:958

      Given final block not properly padded

      以下是針對在Linux操作系統(tǒng)下關于AES解密失敗的問題進行了詳細的分析介紹,需要的朋友可以過來參考下:
      現(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 實現(xiàn)完全隨操作系統(tǒng)本身的內(nèi)部狀態(tài),除非調(diào)用方在調(diào)用 getInstance 方法之后又調(diào)用了 setSeed 方法;該實現(xiàn)在 windows 上每次生成的 key 都相同,但是在 solaris 或部分 linux 系統(tǒng)上則不同。

      国产一级婬片AAA毛,无码中文精品视视在线观看,欧美日韩a人成v在线动漫,五月丁香青草久久
      <code id="qf3hh"></code>
    • <menuitem id="qf3hh"></menuitem>
    • <strike id="qf3hh"><label id="qf3hh"></label></strike>