簡體   English   中英

SecretKeyFactory.getInstance(“PBKDF2WithHmacSHA512”)拋出NoSuchAlgorithmException

[英]SecretKeyFactory.getInstance(“PBKDF2WithHmacSHA512”) throws NoSuchAlgorithmException

經過一些研究和一些工作,我終於能夠哈希密碼了,現在有一個問題在我的腦海里,我已經使用了SHA1方法,我想嘗試使用SHA512,因為我被告知它更好(更安全)所以以下是我的代碼它有點到處都是,但我認為它是可理解的:

public class Safety
{
   //calling some parameters for possible later changes
   public static final String algorithm = "PBKDF2WithHmacSHA1";
   public static final int saltbytesize = 24;
   public static final int hashbytesize = 24;
   public static final int iterations = 1000;
   public static final int iIndex = 0;
   public static final int sIndex = 1;
   public static final int pbkIndex = 2;

   public static Users passwordHash(Users user) throws NoSuchAlgorithmException,
                                                       InvalidKeySpecException
   {
      SecureRandom sR = new SecureRandom();

      byte[] pws = new byte[saltbytesize];

      sR.nextBytes(pws);
      byte[] pwh = pbkdf2(user.getPassword().toCharArray(), pws, iterations, hashbytesize);

      user.setPassword(toHex(pwh));

      byte[] sas = new byte[saltbytesize];

      sR.nextBytes(sas);

      byte[] sah = pbkdf2(user.getsA().toCharArray(), sas, iterations, hashbytesize);

      user.setsA(toHex(sah));

      user.setUserhash(pws);

      user.setSahash(sas);

      return user;
   }

   public static boolean hashpassword(String username, String password, Users user)
   throws NoSuchAlgorithmException,
          InvalidKeySpecException
   {
      byte[] pws = user.getUserhash();

      byte[] pwh = pbkdf2(password.toCharArray(), pws, iterations, hashbytesize);

      String searcher = toHex(pwh) + username;

      String searched = user.getPassword() + user.getUsername();

      if (searcher.equals(searched))
      {
         return true;
      }
      return false;
   }

   private static byte[] pbkdf2(char[] password, byte[] salt,
                                int iterations, int bytes)
      throws NoSuchAlgorithmException, InvalidKeySpecException
   {
      PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8);
      SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
      return skf.generateSecret(spec).getEncoded();
   }

   private static String toHex(byte[] array)
   {
      BigInteger bi = new BigInteger(1, array);

      String hex = bi.toString(16);

      int paddingLength = (array.length * 2) - hex.length();

      if (paddingLength > 0)
         return String.format("%0" + paddingLength + "d", 0) + hex;
      else
         return hex;
   }
}

那么這是我的代碼,但是,我還沒有能夠制作SHA512並且我已經嘗試過public static final String algorithm = "PBKDF2WithHmacSHA512"但這似乎不是算法的正確字符串,因為它會拋出不這樣的算法異常。

我也歡迎任何可以使代碼更好的更改。

如上所述! 相關的幾行代碼

public static final String algorithm =“PBKDF2WithHmacSHA512”<<<<<

開箱即用是不可能的

OpenJDK實現只提供了一個PBKDF2HmacSHA1Factory.java ,其中包含了“HmacSHA1”摘要。 就我測試而言,Oracle JDK在這個意義上並沒有什么不同。

你需要做的是派生PBKDF2HmacSHA1Factory (來吧,它是開放的 !)並在其構造函數中添加一個參數。 您可以避免創建自己的Provider的麻煩,只需按如下方式初始化和使用您的工廠:

PBKDF_SecretKeyFactory kf = new PBKDF_SecretKeyFactory("HmacSHA512");
KeySpec ks = new PBEKeySpec(password,salt,iterations,bitlen);
byte key[] = kf.engineGenerateSecret(ks).getEncoded();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM