RSA Implementation using java.math.BigInteger Class

This chapter provides tutorial notes and example codes on RSA implementation using Java BigInteger class. Topics include introduction of the java.math.BigInteger class; generating large probable prime numbers; generating RSA public key and private key; validating RSA keys; determining cleartext and ciphertext block sizes; padding last block of cleartext; implementing RSA encryption and decryption operations.

These sections are omitted from this Web preview version. To view the full content, see information on how to obtain the full version this book.

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

Conclusions:

• java.math.BigInteger class is designed to offer you enough methods to build a full solution for RSA public key encryption.
• The probablePrime() method can be used to generate very large positive probable prime numbers. The probability of generated numbers are prime numbers is 100-100/(2**100) percent.
• The gcd() method can be used to calculate the greatest common divisor for coprime number generation.
• The modInverse() method can be used to calculate the private key from the public key.
• The modPow() method can be used to carry out the RSA encryption and decryption operations.
• Cleartext block size can be set to "min(floor((RsaKeySize-1)/8), 256)".
• The block of cleartext can be padded using the same idea as the PKCS5Padding schema.
• Ciphertext block size can be set to "1+floor((RsaKeySize-1)/8)".
• A simple full implementation of RSA public key algorithm is presented using the java.math.BigInteger class.
• The implementation passed tests with RSA keys up to 3072 bits.