【问题标题】:SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512") throws NoSuchAlgorithmExceptionSecretKeyFactory.getInstance("PBKDF2WithHmacSHA512") 抛出 NoSuchAlgorithmException
【发布时间】:2014-01-15 10:56:48
【问题描述】:

经过一些研究和一些工作,我终于能够对密码进行哈希处理,现在我想到一个问题,我使用了 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" 但这似乎不是算法的正确字符串,因为它会引发 no such algorithm 异常。

我也欢迎任何可以使代码更好的更改。

如上所述! 相关的几行代码

公共静态最终字符串算法 = "PBKDF2WithHmacSHA512"

【问题讨论】:

  • 如果您要求人们查看代码,请始终格式化代码以使其看起来更漂亮。代码的清晰性是问题的一半。
  • 谢谢你,我知道这一点,但我破解的方式限制了我让它看起来漂亮的能力。
  • 并非如此。您粘贴的代码具有未对齐的间距和压缩语句等
  • 已修复。您的代码格式的主要问题似乎是制表符 - 代码中应该从不有制表符,并且编辑显示和输出可能使用不同的制表位,因此缩进可能看起来不同。此外,如果你使用一个像样的 IDE(比如 NetBeans),如果没有像样的格式,编写代码实际上是相当困难的。
  • TY @Dukeling 你是我的英雄 :)

标签: java algorithm security sha


【解决方案1】:

这是不可能开箱即用的

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();

【讨论】:

  • 我需要将我的意思的密钥[]保存在数据库中吗?
  • 是的。尽管名称如此,生成的“密钥”实际上是 PBKDF 的输出。攻击者无法(但通过蛮力)从“密钥”中获取原始密码。
  • 当有人登录我所做的事情时,我应该事先问的另一件事是检索密钥和盐并再次对其进行哈希处理?更正确地说,密钥的用途是什么?或者它实际上是哈希密码,我太厚了,无法理解?
  • 我认为这些更改是微不足道的:您需要一个带有“HmacSHA512”的 PBKDF 工厂,PBKDF2HmacSHA1Factory 类是一个带有“HmacSHA1”的 PBKDF 工厂:复制源代码,重命名它,用“HmacSHA512”替换“HmacSHA1”,新年快乐
  • Java 8 现在提供 PBKDF2HmacSHA512 - docs.oracle.com/javase/8/docs/technotes/guides/security/…
猜你喜欢
  • 2014-04-19
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多