security
Generate Keys from DSA parameters example
This is an example of how to generate keys from the DSA Parameters in Java. Using the DSA parameters to generate keys implies that you should:
- Create five BigInteger Objects, to be used as the DSA Parameters, the prime, the subPrime, the base and the private and public keys.
- Create a new KeyFactory for the DSA algorithm.
- Create a DSAPrivateKeySpec, using the private key and the DSA parameters (prime, subprime and base).
- Generate the PrivateKey,using the
generatePrivate(KeySpec keySpec)
API method of the KeyFactory, with the DSAPrivateKeySpec. - Create a DSAPublicKeySpec, using the public key and the DSA parameters (prime, subprime and base).
- Generate the PublicKey, using the
generatePublic(KeySpec keySpec)
API method of the KeyFactory, with the DSAPublicKeySpec.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.core; import java.math.BigInteger; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.DSAPrivateKeySpec; import java.security.spec.DSAPublicKeySpec; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import java.util.Random; public class GenerateDSAKeys { public static void main(String[] args) { try { Random random = new Random(); /* * DSA requires three parameters to create a key pair * prime (P) * subprime (Q) * base (G) * These three values are used to create a private key (X) * and a public key (Y) */ BigInteger prime = new BigInteger(128, random); BigInteger subPrime = new BigInteger(128, random); BigInteger base = new BigInteger(128, random); BigInteger x = new BigInteger(128, random); BigInteger y = new BigInteger(128, random); // Get the DSA key factory KeyFactory keyFactory = KeyFactory.getInstance("DSA"); // Get the private key KeySpec privateKeySpec = new DSAPrivateKeySpec(x, prime, subPrime, base); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); // Get the public key KeySpec publicKeySpec = new DSAPublicKeySpec(y, prime, subPrime, base); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); System.out.println(privateKey + "n" + publicKey); } catch (InvalidKeySpecException e) { } catch (NoSuchAlgorithmException e) { } } }
Example Output:
Sun DSA Private Key
parameters:
p:
07a2730f f524fcba fa29f292 06e5238e
q:
41ea7d7b 0f590de0 4f85fee6 a42da5a9
g:
f2b7cebe 25cc2646 2237ae0c 6daf4a07
x: fba1abf7 a6bdb1cf 2a7c788b e559eb61
Sun DSA Public Key
Parameters:
p:
07a2730f f524fcba fa29f292 06e5238e
q:
41ea7d7b 0f590de0 4f85fee6 a42da5a9
g:
f2b7cebe 25cc2646 2237ae0c 6daf4a07
y:
56326f19 90047fb0 5419e33a 94bf5e55
This was an example of how to generate keys from the DSA Parameters in Java.
Sir, I have copied your code in my netbeans ide and tried running. The code executed with only public key displaying but private key was displayed in below format:
sun.security.provider.DSAPrivateKey@5099a kindly provide how to convert this into meaningful format
Thanx