security
Diffie-Helman key pair generation and parameters
This is an example of how to generate key pairs with the Diffie-Helman algorithm. The DH algorithm is be used to generate private/public key pairs. The private key can be used to generate a digital signature for a document by the owner of the document. Then the public key can be used by the one who receives the document to verify the authenticity of the signature. Generating private/public key pairs with the Diffie-Helman algorithm implies that you should:
- Create a KeyPairGenerator Object that generates private/public keys for the DH algorithm, using the
getInstance(String algortihm)
API method. - Initialize the KeyGenerator so as to generate keys with a 1024-bit length, using the
initialize(int keysize)
API method. - Create a KeyPair Object , with the
genKeyPair()
API method, that generates the key pair. - Create the PrivateKey and PublicKey Objects of the key pair, with the
getPrivate()
andgetPublic()
API methods of the KeyPair. - Return for both keys the names of their primary encoded formats, using for both their
getformat()
ΑPI methods
as described in the code snippet below.
package com.javacodegeeks.snippets.core; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; public class Main { public static void main(String[] argv) throws Exception { String algo = "DH"; //Change this to RSA, DSA ... // Generate a 1024-bit Digital Signature Algorithm KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(algo); keyGenerator.initialize(1024); KeyPair kpair = keyGenerator.genKeyPair(); PrivateKey priKey = kpair.getPrivate(); PublicKey pubKey = kpair.getPublic(); String frm = priKey.getFormat(); System.out.println("Private key format :" + frm); System.out.println("Diffie-Helman Private key parameters are:" + priKey); frm = pubKey.getFormat(); System.out.println("Public key format :" + frm); System.out.println("Diffie-Helman Public key parameters are:" + pubKey); } }
Output:
Private key format :PKCS#8
Diffie-Helman Private key parameters are:SunJCE Diffie-Hellman Private Key:
x:
a391eed7 d10d95d3 3952005c 117c56ad a3d686c5 8a60d504 2fde2db6 11686543
0025c0b7 e038f63f cb82151b a7cb24fb f6c2ab69 9c517155 67818cec 782cf977
p:
fd7f5381 1d751229 52df4a9c 2eece4e7 f611b752 3cef4400 c31e3f80 b6512669
455d4022 51fb593d 8d58fabf c5f5ba30 f6cb9b55 6cd7813b 801d346f f26660b7
6b9950a5 a49f9fe8 047b1022 c24fbba9 d7feb7c6 1bf83b57 e7c6a8a6 150f04fb
83f6d3c5 1ec30235 54135a16 9132f675 f3ae2b61 d72aeff2 2203199d d14801c7
g:
f7e1a085 d69b3dde cbbcab5c 36b857b9 7994afbb fa3aea82 f9574c0b 3d078267
5159578e bad4594f e6710710 8180b449 167123e8 4c281613 b7cf0932 8cc8a6e1
3c167a8b 547c8d28 e0a3ae1e 2bb3a675 916ea37f 0bfa2135 62f1fb62 7a01243b
cca4f1be a8519089 a883dfe1 5ae59f06 928b665e 807b5525 64014c3b fecf492a
l:
512
Public key format :X.509
Diffie-Helman Public key parameters are:SunJCE Diffie-Hellman Public Key:
y:
d3fabd76 139865f1 63507aa2 6a9480a9 f180692b ba0e6979 f335ee25 2e26762c
f7df3af9 d7ea612e 7540f071 f50051ae 7d061113 fd0d2d0c d0cc4ae1 03406d44
84398cc6 59a93dcd 6ec827d1 06edb2f0 02d48ee5 f2c9cb94 785f39df cc88ec65
5a224a1c 318b51fe 9c40445b fedb5f14 3fe83f51 82d0357c 1004652e 93c9ad81
p:
fd7f5381 1d751229 52df4a9c 2eece4e7 f611b752 3cef4400 c31e3f80 b6512669
455d4022 51fb593d 8d58fabf c5f5ba30 f6cb9b55 6cd7813b 801d346f f26660b7
6b9950a5 a49f9fe8 047b1022 c24fbba9 d7feb7c6 1bf83b57 e7c6a8a6 150f04fb
83f6d3c5 1ec30235 54135a16 9132f675 f3ae2b61 d72aeff2 2203199d d14801c7
g:
f7e1a085 d69b3dde cbbcab5c 36b857b9 7994afbb fa3aea82 f9574c0b 3d078267
5159578e bad4594f e6710710 8180b449 167123e8 4c281613 b7cf0932 8cc8a6e1
3c167a8b 547c8d28 e0a3ae1e 2bb3a675 916ea37f 0bfa2135 62f1fb62 7a01243b
cca4f1be a8519089 a883dfe1 5ae59f06 928b665e 807b5525 64014c3b fecf492a
l:
512
This was an example of how to generate a key pair using the Diffie-Helman algorithm in Java.