RsaKeyGenerator.java for RSA Key Generation

This section describes the initial draft of a RSA public key and private key generation implementation using the java.math.BigInteger class.

The first step of RSA public key encryption implementation is to write a program to generate a pair of public key and private key. Here is my initial draft using the java.math.BigInteger class:

/* RsaKeyGenerator.java
#- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
 */
import java.math.BigInteger;
import java.util.Random;
class RsaKeyGenerator {
   public static void main(String[] a) {
      if (a.length<1) {
         System.out.println("Usage:");
         System.out.println("java RsaKeyGenerator size");
         return;
      }
      int size = Integer.parseInt(a[0]);

      Random rnd = new Random();
      BigInteger p = BigInteger.probablePrime(size/2,rnd);
      BigInteger q = p.nextProbablePrime();
      BigInteger n = p.multiply(q);
      BigInteger m = (p.subtract(BigInteger.ONE)).multiply(
         q.subtract(BigInteger.ONE));
      BigInteger e = getCoprime(m);
      BigInteger d = e.modInverse(m);

      System.out.println("p: "+p);
      System.out.println("q: "+q);
      System.out.println("m: "+m);
      System.out.println("Modulus: "+n);
      System.out.println("Key size: "+n.bitLength());
      System.out.println("Public key: "+e);
      System.out.println("Private key: "+d);
   }
   public static BigInteger getCoprime(BigInteger m) {
      Random rnd = new Random();
      int length = m.bitLength()-1;
      BigInteger e = BigInteger.probablePrime(length,rnd);
      while (! (m.gcd(e)).equals(BigInteger.ONE) ) {
      	 e = BigInteger.probablePrime(length,rnd);
      }
      return e;
   }
}

Some notes on RsaKeyGenerator.java:

See the next tutorial for testing result of this program.

Last update: 2013.

Table of Contents

 About This Book

 Cryptography Terminology

 Cryptography Basic Concepts

 Introduction to AES (Advanced Encryption Standard)

 Introduction to DES Algorithm

 DES Algorithm - Illustrated with Java Programs

 DES Algorithm Java Implementation

 DES Algorithm - Java Implementation in JDK JCE

 DES Encryption Operation Modes

 DES in Stream Cipher Modes

 PHP Implementation of DES - mcrypt

 Blowfish - 8-Byte Block Cipher

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 Introduction of RSA Algorithm

RSA Implementation using java.math.BigInteger Class

 java.Math.BigInteger Class

 Generating Prime Number with BigInteger Class

 Performance of Prime Number Generation

 RSA Encryption Implementation using BigInteger Class

RsaKeyGenerator.java for RSA Key Generation

 RSA Keys Generated by RsaKeyGenerator.java

 RsaKeyValidator.java for RSA Key Validation

 64-bit RSA Key Validated by RsaKeyValidator.java

 Converting Byte Sequences to Positive Integers

 Cleartext Block Size for RSA Encryption

 Cleartext Message Padding and Revised Block Size

 Ciphertext Block Size for RSA Encryption

 RsaKeyEncryption.java for RSA Encryption Operation

 RsaKeyDecryption.java for RSA Decryption Operation

 Testing RsaKeyEncryption.java with a 16-bit Key

 Testing RsaKeyEncryption.java with a 64-bit Key

 Testing RsaKeyEncryption.java with a 3072-bit Key

 Introduction of DSA (Digital Signature Algorithm)

 Java Default Implementation of DSA

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Cipher - Public Key Encryption and Decryption

 MD5 Mesasge Digest Algorithm

 SHA1 Mesasge Digest Algorithm

 OpenSSL Introduction and Installation

 OpenSSL Generating and Managing RSA Keys

 OpenSSL Managing Certificates

 OpenSSL Generating and Signing CSR

 OpenSSL Validating Certificate Path

 "keytool" and "keystore" from JDK

 "OpenSSL" Signing CSR Generated by "keytool"

 Migrating Keys from "keystore" to "OpenSSL" Key Files

 Certificate X.509 Standard and DER/PEM Formats

 Migrating Keys from "OpenSSL" Key Files to "keystore"

 Using Certificates in IE (Internet Explorer)

 Using Certificates in Firefox

 Using Certificates in Google Chrome

 Outdated Tutorials

 References

 PDF Printing Version