baidu_28888097
baidu_28888097
采纳率0%
2015-10-13 06:10

关于 JAVA中PBKDF2WithHmacSHA256加密。

public class SHA256 {

public static byte[] getEncryptedPassword(String password, byte[] salt,  int iterations,  int derivedKeyLength) throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength * 8);

    SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");

    return f.generateSecret(spec).getEncoded();
}

public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte [] slat={'9','T','u','x','w','O','z','X','c','m','e','y'};

    byte[]pwd=getEncryptedPassword("123456",slat, 12000, 256);


}

}

上面是源码,不知道为什么 我一运行 就报错。

Exception in thread "main" java.security.NoSuchAlgorithmException: PBKDF2WithHmacSHA256 SecretKeyFactory not available
at javax.crypto.SecretKeyFactory.(DashoA13*..)
at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..)
at TestLZQ.SHA256.getEncryptedPassword(SHA256.java:26)
at TestLZQ.SHA256.main(SHA256.java:34)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • lzp_lrp WorldMobile 6年前

    遇到此错误是因为SASL验证,取消掉此验证就可以了。给出了如下代码
    [html] view plaincopy
    configuration.setReconnectionAllowed(true);

    configuration.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

    configuration.setSendPresence(true);

    configuration.setSASLAuthenticationEnabled(false);

    configuration.setRosterLoadedAtLogin(false);

    经测试发现只需添加如入两句就可以了
    [html] view plaincopy
    config.setSASLAuthenticationEnabled(false);

    config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

    点赞 评论 复制链接分享
  • lzp_lrp WorldMobile 6年前

    参考一下这个试试
    报错: java.security.NoSuchAlgorithmException
    http://blog.csdn.net/chexitianxia/article/details/9718045

    点赞 评论 复制链接分享

相关推荐