RSA Keys Generated by RsaKeyGenerator.java

This section provides some RSA keys generated by my BigInteger implementation, RsaKeyGenerator.java, of RSA public key and private key generation algorithm.

Are you ready to see how my initial implementation of RSA public key and private key generation behaves? Here is the first test run using JDK 1.6:

C:\herong>javac RsaKeyGenerator.java

C:\herong>java RsaKeyGenerator 4
Exception in thread "main" java.lang.ArithmeticException: bitLength < 2
        at java.math.BigInteger.probablePrime(BigInteger.java:539)
        at RsaKeyGenerator.getCoprime(RsaKeyGenerator.java:35)
        at RsaKeyGenerator.main(RsaKeyGenerator.java:21)

Too bad. The program failed in the "getCoprime()" call to find a public key, "e". This is because the requested key size is too low, which does not give enough room to find a public key, "e".

Rerun the program with a higher key size. The problem should go away.

C:\herong>java RsaKeyGenerator 5
p: 3
q: 5
m: 8
Modulus: 15
Key size: 4
Public key: 5
Private key: 5

The program completed! But the result is not ideal. The private key is the same as the public key "5". This makes my private key not secure. The program could be enhanced to ensure the private key is different than the public key.

Rerun the program with a higher key size. Hope it will return a good pair of public key and private key.

C:\herong>java RsaKeyGenerator 7
p: 7
q: 11
m: 60
Modulus: 77
Key size: 7
Public key: 17
Private key: 53

Cool. The program generated a good pair of public key, {77,17} and {77,53}.

Rerun the program with different key sizes:

C:\herong>java RsaKeyGenerator 8
p: 11
q: 13
m: 120
Modulus: 143
Key size: 8
Public key: 53
Private key: 77

C:\herong>java RsaKeyGenerator 16
p: 151
q: 157
m: 23400
Modulus: 23707
Key size: 15
Public key: 10151
Private key: 4751

C:\herong>java RsaKeyGenerator 32
p: 34613
q: 34631
m: 1198613560
Modulus: 1198682803
Key size: 31
Public key: 756303523
Private key: 705860827

C:\herong>java RsaKeyGenerator 64
p: 2966449091
q: 2966449099
m: 8799820227293420820
Modulus: 8799820233226319009
Key size: 63
Public key: 3447872045926560979
Private key: 5997668047507921159

C:\herong>java RsaKeyGenerator 128
p: 17902136406704537069
q: 17902136406704537077
m: 320486487924256034368552058949822333168
Modulus: 320486487924256034404356331763231407313
Key size: 128
Public key: 138184930940463531660820083778072069237
Private key: 173448309040289888328993883042709949325

C:\herong>java RsaKeyGenerator 256
p: 248658744261550238073459677814507557459
q: 248658744261550238073459677814507557527
m: 618311710977310434529034534762836648859088873902738200302650613...
Modulus: 618311710977310434529034534762836648864062048787969205064...
Key size: 256
Public key: 394190853336940694532345943348534965939075733405768734...
Private key: 21429568381701961014089098585280129682302896350728470...

Output looks good. But we need test them with the encryption and decryption process.

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