gpt4 book ai didi

java - SecretKeyFactory.getInstance ("PBKDF2WithHmacSHA512") 抛出 NoSuchAlgorithmException

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:26:23 27 4
gpt4 key购买 nike

经过一些研究和一些工作后,我终于能够对密码进行哈希加盐,现在我想到了一个问题,我使用了 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(来吧,它是 open!)并向其构造函数添加一个参数。您可以避免创建自己的 Provider 的麻烦,只需按如下方式初始化和使用您的工厂:

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

关于java - SecretKeyFactory.getInstance ("PBKDF2WithHmacSHA512") 抛出 NoSuchAlgorithmException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20807647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com