I am using following code snippet for getting factory instance of specified algorithm. But it is throwing a java.security.NoSuchAlgorithmException. I am using this in my java project with jre1.6.
Is it require any external library(jar)? The same code when I tried in my Android app, it’s working fine.
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5AND256BITAES-CBC-OPENSSL");
} catch (Exception e) {
e.printStackTrace();
}
Provider[] providers = Security.getProviders();
if (null == providers) {
System.out.println("Providers are not available.");
return;
}
for (Provider provider : providers) {
System.out.println("Provider: " + provider.getName());
Set<Provider.Service> services = provider.getServices();
for (Provider.Service service : services) {
System.out.println("\tAlgorithm: " + service.getAlgorithm());
}
}
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance(providers[0].getServices().iterator().next().getAlgorithm());
if (null == factory) {
System.out.println("Getting instance of specified algorithm failed.");
} else {
System.out.println("Success.");
}
} catch (Exception e) {
e.printStackTrace();
}
Above is edited code and is throwing following exception:
java.security.NoSuchAlgorithmException: SHA1PRNG SecretKeyFactory not available
at javax.crypto.SecretKeyFactory.<init>(DashoA13*..)
at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..)
"PBEWITHMD5AND256BITAES-CBC-OPENSSL"is part of the Bouncy Castle provider, you may have to download that library to use it on Java SE. Don’t forget to download the unlimited strength jurisdiction policy files too.As for the second exception, if you just ask the algorithm of the first service of the first provider, you get get an algorithm that is only valid for a specific type. In this case it is usable only for
SecureRandom, not forSecretKeyFactoryas you would have found if you had replaced.getAlgorithm()with.getType().