生成器对象的提供者可以使用getProvider()类java.security.AlgorithmParameterGenerator中的方法获得。此方法不需要任何参数,它返回生成器对象的提供者。
演示此的程序如下所示-
import java.security.*;
import java.util.*;
public class Demo {
public static void main(String[] argv) {
try {
AlgorithmParameterGenerator apGenerator = AlgorithmParameterGenerator.getInstance("DiffieHellman");
apGenerator.init(1024);
Provider provider = apGenerator.getProvider();
System.out.println("The Provider is: " + provider);
} catch (NoSuchAlgorithmException e) {
System.out.println("Error!!! NoSuchAlgorithmException");
} catch (ProviderException e) {
System.out.println("Error!!! ProviderException");
}
}
}输出结果
The Provider is: SunJCE version 1.8
现在让我们了解上面的程序。
该方法getProvider()用于获取生成器对象的提供者。然后显示此提供程序。如果算法名称错误,则会引发异常NoSuchAlgorithmException。演示的代码片段如下-
try {
AlgorithmParameterGenerator apGenerator = AlgorithmParameterGenerator.getInstance("DiffieHellman");
apGenerator.init(1024);
Provider provider = apGenerator.getProvider();
System.out.println("The Provider is: " + provider);
} catch (NoSuchAlgorithmException e) {
System.out.println("Error!!! NoSuchAlgorithmException");
} catch (ProviderException e) {
System.out.println("Error!!! ProviderException");
}