security

Get DSA parameters of a key pair example

This is an example of how to get DSA parameters of a key pair. In short, to get the DSA parameters of a key pair you should:

  • Generate a 1024-bit Digital Signature Algorithm (DSA) key pair. Create a KeyPairGenerator for the DSA algorithm and initialize it with 1024-bit key size.
  • Generate the KeyPair.
  • Get the private and public key from the key pair and cast them to sun.security.provider.DSAPrivateKey and sun.security.provider.DSAPublicKey correspondingly.
  • Get the DSAParams of the private key. THe DSAParams consist of three BigInteger parameters, the prime, the subPrime and the base. Retrieve them all with the API methods provided by the DSAParams Class.
  • Get the raw private and the raw public key.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;
 
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.DSAParams;

import sun.security.provider.DSAPrivateKey;
import sun.security.provider.DSAPublicKey;

public class DSAParamsOfKeyPair {
 
  public static void main(String[] args) {

    try {

	// Generate a 1024-bit Digital Signature Algorithm (DSA) key pair
	KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
	keyPairGenerator.initialize(1024);
	KeyPair keyPair = keyPairGenerator.genKeyPair();
	DSAPrivateKey privateKey = (DSAPrivateKey) keyPair.getPrivate();
	DSAPublicKey publicKey = (DSAPublicKey) keyPair.getPublic();


 /*
	* 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)
	*/
	DSAParams dsaParams = privateKey.getParams();
	BigInteger prime = dsaParams.getP();
	BigInteger subPrime = dsaParams.getQ();
	BigInteger base = dsaParams.getG();
	BigInteger x = privateKey.getX();
	BigInteger y = publicKey.getY();
	
    } catch (NoSuchAlgorithmException e) {
    }

 }

}

 
This was an example of how to get the DSA parameters of a key pair in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button