Generating a Parameter Set for the Diffie-Hellman Key Agreement Algorithm - Java Security

Java examples for Security:Key

Description

Generating a Parameter Set for the Diffie-Hellman Key Agreement Algorithm

Demo Code

import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidParameterSpecException;

import javax.crypto.spec.DHParameterSpec;

public class Main {
  public static String genDhParams() {
    try {/*w w  w  . j a  v  a  2  s.c o  m*/
      AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator
          .getInstance("DH");
      paramGen.init(1024);

      AlgorithmParameters params = paramGen.generateParameters();
      DHParameterSpec dhSpec = (DHParameterSpec) params
          .getParameterSpec(DHParameterSpec.class);

      return "" + dhSpec.getP() + "," + dhSpec.getG() + "," + dhSpec.getL();
    } catch (NoSuchAlgorithmException e) {
    } catch (InvalidParameterSpecException e) {
    }
    return null;
  }
}

Related Tutorials