Java中的 AlgorithmParameterGenerator generateParameters() 方法及示例
Java.security.AlgorithmParameterGenerator类的generateParameters()方法用于生成参数。
句法:
public final AlgorithmParameters generateParameters()
返回值:此方法返回新的AlgorithmParameters对象。
以下是说明generateParameters()方法的示例:
注意:以下程序不会在在线 IDE 上运行。
示例 1:对于算法DSA
Java
// Java program to demonstrate
// generateParameters() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of
// AlgorithmParameterGenerator
// and getting instance
// using getInstance() method
AlgorithmParameterGenerator
sr
= AlgorithmParameterGenerator
.getInstance("DSA");
// initializing the AlgorithmParameterGenerator
// with 1024 using initialize() method
sr.init(1024);
// generating the Parameters
// using generateParameters() method
AlgorithmParameters param = sr.generateParameters();
// printing the keypair
System.out.println("AlgorithmParameters : " + param);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Java
// Java program to demonstrate
// generateParameters() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of
// AlgorithmParameterGenerator
// and getting instance
// using getInstance() method
AlgorithmParameterGenerator
sr
= AlgorithmParameterGenerator
.getInstance("DiffieHellman");
// initializing the AlgorithmParameterGenerator with 1024
// using initialize() method
sr.init(1024);
// generating the Parameters
// using generateParameters() method
AlgorithmParameters param = sr.generateParameters();
// printing the keypair
System.out.println("AlgorithmParameters : " + param);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
AlgorithmParameters :
p: bb0f976a a9984119 06cea3a8 cf608f99 8bbb5f00 83dc0c2b ab967a42 826f748e
48a6f081 d86a8ee4 5d5102c0 e86e657b 8b7ab43d a34a70af 96654397 95eb22c9
9ed614bc 862e10c8 d1cedf48 e84b6bd9 d3bd7027 c297fbb3 a134f282 989d4f0b
4b103b44 89a43822 6938a4cc 2cf2cec4 5e2f368e 0adb16ac f63bce6d 50fb82fd
q: d0447f68 44751e8d 4fe4663b f2d66b6f 40f78285
g: ad51837b 7c01e880 013f7768 4244ba4a 79429086 44017899 88e8604a b2f5d2e8
0c62070b 9793122f 38884ac8 5078d8db 3d0b4a18 71818d2c b10cb34e 388a4bc7
c8613b26 2f06506b 9c178b21 efbe10ca 98eb8681 555fb56b 3961475f 3f76fa4c
5abf694c b427b8f2 3b6ee737 d318f8d9 de19433f aa150e62 5931e24f c4dfcd9b
示例 2:对于算法DiffieHellman
Java
// Java program to demonstrate
// generateParameters() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of
// AlgorithmParameterGenerator
// and getting instance
// using getInstance() method
AlgorithmParameterGenerator
sr
= AlgorithmParameterGenerator
.getInstance("DiffieHellman");
// initializing the AlgorithmParameterGenerator with 1024
// using initialize() method
sr.init(1024);
// generating the Parameters
// using generateParameters() method
AlgorithmParameters param = sr.generateParameters();
// printing the keypair
System.out.println("AlgorithmParameters : " + param);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
AlgorithmParameters : SunJCE Diffie-Hellman Parameters:
p:
b994a636 18e1507a ea1e1e5f 8b43947a dc32d264 9b947295 114c86c5 7961fb52
2275eb40 0cf767a9 33ea6646 ebb1051f 4a528409 bbfebba1 d3555b98 4f0d7fdc
31a74160 adb21ab8 e0d3d455 f54786de 85feebd2 a3cdeac2 c47703de 8c80a793
07b4370c 94367fe7 67bb14f4 e3647633 c6af85ed e35bc9d7 e664f2ab c72fef61
g:
57e04b09 e3476752 f3b88c6e c70aa02b a55a3ef8 fd7f678b ca904ba4 10cc1b29
2f37f5e1 99208225 8ec24c47 86fd54b5 1baa2b83 ead0f5cc 365e21e1 81038c99
6878737d 7fcbba45 1fb3b6c5 104c9f7e 3f33a680 7ceb1e38 066c8169 8ae778d2
d639c9ef 753745c3 eede9971 dc2c3cf5 c97ab4e3 7014e711 45e2e1cd 1fa1762c
l:
1023