📄 e470. generating a parameter set for the diffie-hellman key agreement algorithm.txt
字号:
Two parties use a key agreement protocol to generate identical secret keys for encryption without ever having to transmit the secret key. The protocol works by both parties agreeing on a set of values (a prime, a base, and a private value) which are used to generate a key pair. This example demonstrates how to generate the set of values.
The two parties then exchange the generated public keys and then use it to compute the secret encryption key. This is demonstrated in e471 Generating a Secret Key Using the Diffie-Hellman Key Agreement Algorithm.
// Returns a comma-separated string of 3 values.
// The first number is the prime modulus P.
// The second number is the base generator G.
// The third number is bit size of the random exponent L.
public static String genDhParams() {
try {
// Create the parameter generator for a 1024-bit DH key pair
AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance("DH");
paramGen.init(1024);
// Generate the parameters
AlgorithmParameters params = paramGen.generateParameters();
DHParameterSpec dhSpec
= (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class);
// Return the three values in a string
return ""+dhSpec.getP()+","+dhSpec.getG()+","+dhSpec.getL();
} catch (NoSuchAlgorithmException e) {
} catch (InvalidParameterSpecException e) {
}
return null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -