关于 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)

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

遇到此错误是因为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);