我正在使用以下代码片段来获取指定算法的工厂实例。但它正在抛出一个java.security.NoSuchAlgorithmException
. 我在 jre1.6 的 java 项目中使用它。
它需要任何外部库(jar)吗?当我在我的 Android 应用程序中尝试使用相同的代码时,它运行良好。
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();
}
以上是编辑过的代码,并引发以下异常:
java.security.NoSuchAlgorithmException: SHA1PRNG SecretKeyFactory not available
at javax.crypto.SecretKeyFactory.<init>(DashoA13*..)
at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..)