Contact Us Forum Customers Area

DidiSoft Ltd.

Java generate RSA key

<< Back to Java examples

This example demonstrates how to generate an RSA based OpenPGP key pair with OpenPGP Library for Java.

The generated keys have no expiration date. An overloaded version exists that accepts expiration time parameter.

import com.didisoft.pgp.*;
 
public class GenerateKeyPairRSA {
 public static void main(String[] args) throws Exception {
  // initialize the KeyStore where the key will be generated
  KeyStore ks = new KeyStore("pgp.keystore", "changeit");
 
  // key primary user Id
  String userId = "demo2@didisoft.com";
 
  // preferred hashing algorithms
  String[] hashingAlgorithms = new String[]
				 {HashAlgorithm.SHA1,
				  HashAlgorithm.SHA256,
				  HashAlgorithm.SHA384,
				  HashAlgorithm.SHA512,
				  HashAlgorithm.MD5};
 
  // preferred compression algorithms
  String[] compressions = new String[]
				{CompressionAlgorithm.ZIP,
				CompressionAlgorithm.ZLIB,
				CompressionAlgorithm.UNCOMPRESSED};
 
  // preferred symmetric key algorithms
  String[] cyphers = new String[]
			 {CypherAlgorithm.CAST5,
			  CypherAlgorithm.AES_128,
			  CypherAlgorithm.AES_192,
			  CypherAlgorithm.AES_256,
			  CypherAlgorithm.TWOFISH};
 
  String privateKeyPassword = "changeit";
 
  int keySizeInBytes = 2048;
  ks.generateKeyPair(keySizeInBytes,
			userId,
			KeyAlgorithm.RSA,
			privateKeyPassword,
			compressions,
			hashingAlgorithms,
			cyphers);
 }
}

After the key pair is generated usually we will export the public key and send it to our partners.

Below is a screenshot of the generated key properties when we open it with PGP (r) 10:

RSA OpenPGP key properties

<< Back to Java examples