Cryptography Tutorials - Herong's Tutorial Examples - Version 5.36, by Dr. Herong Yang
Generating Prime Number with BigInteger Class
This section provides a tutorial example on how to generate probable prime numbers using the java.math.BigInteger class in Java.
The first thing I want to try with the java.Math.BigInteger Class is to generate probable prime numbers. I am interested how fast large prime numbers can be generated and how good they are. Here is my first Java program using the java.Math.BigInteger Class:
/* PrimeGenerator.java #- Copyright (c) 2013, HerongYang.com, All Rights Reserved. */ import java.math.BigInteger; import java.util.Random; class PrimeGenerator { public static void main(String[] a) { if (a.length<2) { System.out.println("Usage:"); System.out.println("java PrimeGenerator length certainty"); return; } int length = Integer.parseInt(a[0]); int certainty = Integer.parseInt(a[1]); Random rnd = new Random(); long t1 = System.currentTimeMillis(); BigInteger p = new BigInteger(length,certainty,rnd); long t2 = System.currentTimeMillis(); boolean ok = p.isProbablePrime(certainty); long t3 = System.currentTimeMillis(); BigInteger two = new BigInteger("2"); System.out.println("Probable prime: "+p); System.out.println("Validation: "+ok); System.out.println("Bit length: "+length); System.out.println("Certainty: "+certainty); System.out.println("Probability (%): " +(100.0-100.0/(two.pow(certainty)).doubleValue())); System.out.println("Generation time (milliseconds): "+(t2-t1)); System.out.println("Validation time (milliseconds): "+(t3-t2)); } }
Compile and run it on my Windows 7 computer with JDK 1.6 starting with a small bit length and certainty to validate my program:
C:\herong>javac PrimeGenerator.java C:\herong>java PrimeGenerator 4 4 Probable prime: 13 Validation: true Bit length: 4 Certainty: 4 Probability (%): 93.75 Generation time (milliseconds): 1 Validation time (milliseconds): 39
Reviewing the output, we can see that prime number generated by the program, 13, does meet specified criteria:
I think my program works correctly. What do you think?
Last update: 2013.
Table of Contents
Introduction to AES (Advanced Encryption Standard)
DES Algorithm - Illustrated with Java Programs
DES Algorithm Java Implementation
DES Algorithm - Java Implementation in JDK JCE
DES Encryption Operation Modes
PHP Implementation of DES - mcrypt
Blowfish - 8-Byte Block Cipher
Secret Key Generation and Management
Cipher - Secret Key Encryption and Decryption
►RSA Implementation using 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
OpenSSL Introduction and Installation
OpenSSL Generating and Managing RSA Keys
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)