Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getInstance(“PBKDF2WithHmacSHA512”)抛出NoSuchAlgorithmException_Java_Algorithm_Security_Sha - Fatal编程技术网

Java getInstance(“PBKDF2WithHmacSHA512”)抛出NoSuchAlgorithmException

Java getInstance(“PBKDF2WithHmacSHA512”)抛出NoSuchAlgorithmException,java,algorithm,security,sha,Java,Algorithm,Security,Sha,经过一点研究和一些工作,我终于能够散列盐密码现在有一个问题,这是在我的脑海中我已经使用了SHA1方法,我想尝试使用SHA512,因为我被告知它更好(更安全)下面是我的代码,有点到处都是,但我认为这是可以理解的: public class Safety { //calling some parameters for possible later changes public static final String algorithm = "PBKDF2WithHmacSHA1";

经过一点研究和一些工作,我终于能够散列盐密码现在有一个问题,这是在我的脑海中我已经使用了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实现只提供了一个包含“HmacSHA1”摘要的代码。就我所测试的而言,OracleJDK在这个意义上并没有什么不同

您需要做的是派生
PBKDF2HmacSHA1Factory
(来吧,它是打开的!)并向其构造函数添加一个参数。您可以避免创建自己的
提供程序
,只需按如下方式初始化并使用工厂即可:

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

如果你让别人看你的代码,一定要把它格式化得好看。代码的清晰性是问题的一半。谢谢你,我知道这一点,但我的破解方式限制了我让它看起来漂亮的能力。不是真的。您粘贴的代码有未对齐的间距和压缩的语句等。至少修复了这一问题,以便更好地回答潜在的问题=)修复了它。代码格式的主要问题似乎是制表符——代码中不应该有制表符,编辑显示和输出可能使用不同的制表符,因此缩进看起来可能不同。另外,如果你使用一个像样的IDE(比如说NetBeans),如果没有像样的格式,编写代码实际上是相当困难的。TY@Dukeling你是我的英雄:)我需要将密钥[]保存在数据库中吗?是的。不管名称如何,结果“key”实际上是PBKDF的输出。攻击者无法(但通过暴力)从“密钥”中获取原始密码。当有人登录时,我应该事先询问的另一件事是检索密钥和salt并再次对其进行哈希运算?更确切地说,密钥的用途是什么?或者它实际上是散列密码,我太厚而无法理解?我认为更改很简单:您需要一个带有“HmacSHA512”的PBKDF工厂,class
PBKDF2HmacSHA1Factory
是带有“HmacSHA1”的PBKDF工厂:复制源代码,重命名它,用“HmacSHA512”替换“HmacSHA1”,祝您新年快乐。Java8现在有了PBKDF2HmacSHA512-