Tuesday, 14 August 2012

How to Generate Diffie-Hellman or DSA Parameters?

Java provides a cryptography algorithm parameter generator for the Diffie-Hellman protocol and DSA (Digital Signature Algorithm).

For the Diffie-Hellman protocol:
AlgorithmParameterGenerator apg
    = AlgorithmParameterGenerator.getInstance("DiffieHellman");
apg.init(512);

AlgorithmParameters ap = apg.generateParameters();
DHParameterSpec dps = (DHParameterSpec)
    ap.getParameterSpec(DHParameterSpec.class);

System.out.println("Diffie-Hellman");
System.out.println("L :" + dps.getL());
System.out.println("P :" + dps.getP());
System.out.println("G :" + dps.getG());
The generated output is:
Diffie-Hellman
L :511
P :8266077972939539471508511844788321242135959675971529125376989640166743180991072673735061975276152293501435845844404151584495666060797732285698897055150387
G :2019537923026914368988722563706571073299959195070870668709287200992494530060354049564673739869083497959173115010462554070989621918531942099145869042943787
For the DSA:
AlgorithmParameterGenerator apg
    = AlgorithmParameterGenerator.getInstance("DSA");
apg.init(512);

AlgorithmParameters ap = apg.generateParameters();
DSAParameterSpec dps = (DSAParameterSpec)
    ap.getParameterSpec(DSAParameterSpec.class);

System.out.println("DSA");
System.out.println("P :" + dps.getP());
System.out.println("Q :" + dps.getQ());
System.out.println("G :" + dps.getG());
The generated output is:
DSA
P :8197719696944927589177120726843658828192492967000538281179885268382184665678774153545503826581953282213697665107441101650781498069116236522448234275281441
Q :1284267634964328541131115801052780210043074500583
G :570004927801867375990232729480351281559717810987031405643673487612900545268295697120844288618676632560887588778557548200272181177045246836759460884667572