security

Generate Public/Private key pairs for DSA DH RSA algorithm example

With this example we are going to demonstrate how to generate private/public key pairs for the DSA, the DH and the RSA algorithms. In short, to generate key pairs for these three algorithms you should:

  • Create a KeyPairGenerator for the DSA algorithm, using the getInstance(String algorithm) API method and initialize it with a 1024-bit key size.
  • Generate the KeyPair, with the genKeyPair() API method of the KeyPairGenerator.
  • Get the PrivateKey and the PublicKey of the key pair, using the getPrivate() and getPublic() API methods of the KeyPair. They are the private/public keys generated for the DSA algorithm.
  • Create a new KeyPairGenerator for the DH algorithm and initialize it with a 576-bit key size.
  • Get the PrivateKey component and the PublicKey component of the key pair, which are the keys generated for the DH algorithm.
  • Create a KeyPairGenerator for the RSA algorithm and initialize it with a 1024-bit key size.
  • Get the PrivateKey component and the PublicKey component of the key pair, which are the private/public keys for the RSA algorithm.

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

package com.javacodegeeks.snippets.core;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

public class GenerateKeyPairsDSADHRSA {

  public static void main(String[] args) {
    try {

	// Generate a 1024-bit Digital Signature Algorithm (DSA) key pair
	KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
	keyGen.initialize(1024);
	KeyPair keypair = keyGen.genKeyPair();
	PrivateKey privateKey = keypair.getPrivate();
	PublicKey publicKey = keypair.getPublic();

	System.out.println(privateKey + "n" + publicKey);

	// Generate a 576-bit DH key pair
	keyGen = KeyPairGenerator.getInstance("DH");
	keyGen.initialize(576);
	keypair = keyGen.genKeyPair();
	privateKey = keypair.getPrivate();
	publicKey = keypair.getPublic();

	System.out.println(privateKey + "n" + publicKey);

	// Generate a 1024-bit RSA key pair
	keyGen = KeyPairGenerator.getInstance("RSA");
	keyGen.initialize(1024);
	keypair = keyGen.genKeyPair();
	privateKey = keypair.getPrivate();
	publicKey = keypair.getPublic();

	System.out.println(privateKey + "n" + publicKey);

    } catch (java.security.NoSuchAlgorithmException e) {
    }

 }

}

Example Output:

Sun DSA Private Key parameters: 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 q: 9760508f 15230bcc b292b982 a2eb840b f0581cf5 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 x: 18cf950a b9602056 b2f1d0c8 6c075b12 f6894806 Sun DSA Public Key Parameters: 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 q: 9760508f 15230bcc b292b982 a2eb840b f0581cf5 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 y: 2b81ffb4 10acbde2 15daa89c e4961509 5b9c3689 ad798e98 1d328f5b 5c7a3375 d226114b aa7f7e66 04d60415 afaa84f7 76614e44 0ef1c1a4 06b40511 39d32eeb 79c33469 e64447ae 482ca94b ad88cee3 f6fe6f5e 6a902272 952da508 aed72ea8 544857a0 e9dc4cce 74eec128 992cc7ed b239675d c1aa57e7 b9dbded7 f1448fd5 SunJCE Diffie-Hellman Private Key: x: 092d306a cc86a89a d6844197 4dead1ac 006ce2a9 61b2b221 92c66bd8 40bb9cf5 264b3d77 54cb2351 f9e4a71d 2459b1c4 p: b6fe209a 63b09c50 e4ec4917 272e928d efe1ff98 7b354041 29a82420 209324ee 8e2f5689 12ed3237 332e4094 c315df3b 86773b74 ae71f813 4f44b39b 5d998b76 52e0a616 4ade2d83 g: 7ad475df 891ad2f1 1f89f94d 8e124e73 14b588d5 c4eb909e 61798c5d 6e38c67e 19c8326d de286af5 c8058e3f 331cfae4 b56fe7d6 5f3ddd53 af3d647e 3b11cfcc 151d4ddc c97b1e4d l: 384 SunJCE Diffie-Hellman Public Key: y: 9d0f673b 024da446 c0fa73c2 4b777c1a 8d9a6708 6d0952b2 b642a37d 384ee400 3e63a2d4 f53866b8 3c09a9e9 a0195a22 3231ac65 d5f0acec 06aac6bb 8e377014 eeb66321 626fca99 p: b6fe209a 63b09c50 e4ec4917 272e928d efe1ff98 7b354041 29a82420 209324ee 8e2f5689 12ed3237 332e4094 c315df3b 86773b74 ae71f813 4f44b39b 5d998b76 52e0a616 4ade2d83 g: 7ad475df 891ad2f1 1f89f94d 8e124e73 14b588d5 c4eb909e 61798c5d 6e38c67e 19c8326d de286af5 c8058e3f 331cfae4 b56fe7d6 5f3ddd53 af3d647e 3b11cfcc 151d4ddc c97b1e4d l: 384 Sun RSA private CRT key, 1024 bits modulus: 142795061206030156726823177842195212572733038446594476120002420667927534409519068558395518665627720209527364803003440972707778595637832828503837796763890798422674268360041827644014108615150017047702034363289390360559582108377079803357502707024421147092908192899128245187341917555253172209927989072475929943817 public exponent: 65537 private exponent: 11022784299575912276744410893139227923241168217973380757298812062789651594942657854905212764231054771197933053670360374764311029118388029348321031079062566611530973858986084317200914650824379666767850628230371401229516832587018745768363355996789380250588515090793644954067508136858791705777895018366379320065 prime p: 12811756130537958988309716536996674278475522585909476350080520842363443313044778433549466928648004400759405954143764793720000137328472481040147569588268393 prime q: 11145627480815486474855863821179440875907430560012948472080319656001653385746240419372627351045543771415800874613508983801278121451165482943030722233847969 prime exponent p: 8196849635373249010174808343321643537184775959033589321434857239731742040617781706802709130997922158839157905568580462954965984988370708607555847732366353 prime exponent q: 7330362715803132787977971196183796024753009436322048888293300244634927840545343863713907513497964099353575310106750045451981184199911283874657876015770465 crt coefficient: 3422434439743052795992777884062098413125265428162742206664206195976078438193159213380558751862436178937969586397680690388569450064986044869523985807958768 Sun RSA public key, 1024 bits modulus: 142795061206030156726823177842195212572733038446594476120002420667927534409519068558395518665627720209527364803003440972707778595637832828503837796763890798422674268360041827644014108615150017047702034363289390360559582108377079803357502707024421147092908192899128245187341917555253172209927989072475929943817 public exponent: 65537

 
This was an example of how to generate private/public key pairs for the DSA, the DH and the RSA algorithms in Java.

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