for RSA Key Validation

This section provides a tutorial example on how to validate RSA keys by encrypting and decrypting some random sample messages.

Before writing the RSA encryption and decryption programs, I want to validate RSA keys generated from by program.

Here is my RSA key validation program:

#- Copyright (c) 2013,, All Rights Reserved.
import java.math.BigInteger;
import java.util.Random;
class RsaKeyValidator {
   private BigInteger n, e, d;
   public static void main(String[] a) {
      if (a.length<1) {
         System.out.println("java RsaKeyValidator input");
      String input = a[0];

      RsaKeyValidator validator = new RsaKeyValidator(input);

// Reading in RSA public key and private key
   RsaKeyValidator(String input) {
      try {
         BufferedReader in = new BufferedReader(new FileReader(input));
         String line = in.readLine();
         while (line!=null) {
            if (line.indexOf("Modulus: ")>=0) {
               n = new BigInteger(line.substring(9));
            if (line.indexOf("Public key: ")>=0) {
               e = new BigInteger(line.substring(12));
            if (line.indexOf("Private key: ")>=0) {
               d = new BigInteger(line.substring(13));
            line = in.readLine();
      } catch (Exception ex) {
      System.out.println("--- Reading public key and private key ---");
      System.out.println("Modulus: "+n);
      System.out.println("Key size: "+n.bitLength());
      System.out.println("Public key: "+e);
      System.out.println("Private key: "+d);

// Testing encryption and description
   public void test() {
      Random rnd = new Random();
      int size = rnd.nextInt(n.bitLength()-1);
      BigInteger text = new BigInteger(size,rnd);
      BigInteger cipher = text.modPow(e,n);
      BigInteger decrypted = cipher.modPow(d,n);
      boolean isPassed = text.equals(decrypted);
      System.out.println("--- RSA encryption test ---");
      System.out.println("Is test passed: "+isPassed);
      System.out.println("Original text: "+text);
      System.out.println("Cipher text: "+cipher);
      System.out.println("Decrypted text: "+decrypted);

Some notes on

Testing result is presented in the next tutorial.

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 for RSA Key Generation

 RSA Keys Generated by for RSA Key Validation

 64-bit RSA Key Validated by

 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 for RSA Encryption Operation for RSA Decryption Operation

 Testing with a 16-bit Key

 Testing with a 64-bit Key

 Testing 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


 PDF Printing Version