RSA Public Key Encryption Tests

This section provides the test result of RSA public key encryption and private key decryption using the javax.crypto.Cipher class.

Here is the result of my first test of JcePublicCipher.java with the RSA algorithm.

herong> java JcePublicCipher RSA encrypt rsa.pub \
   JcePublicCipher.java jce.cph

KeyFactory Object Info:
Algorithm = rsa
Provider = SunRsaSign version 12
toString = java.security.KeyFactory@1fb8ee3

Key Object Info:
Algorithm = RSA
Saved File = rsa.pub
Length = 94
Format = X.509
toString = Sun RSA public key, 512 bits
  modulus: 89805404347518883100317152161680408697320408608438740188039
6115229515610960443502949757801056895070926940886672738034912224273421
8612911902966014500229283
  public exponent: 65537

Cipher Object Info:
Block Size = 0
Algorithm = rsa
Provider = SunJCE version 1.6
toString = javax.crypto.Cipher@94948a
javax.crypto.IllegalBlockSizeException: Data must not be longer tha...
        at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
        at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..
        at javax.crypto.Cipher.doFinal(DashoA13*..)
        at JcePublicCipher.publicCipher(JcePublicCipher.java:95)
        at JcePublicCipher.main(JcePublicCipher.java:25)

We have a problem here! The error says, the input plaintext must be less than 53 bytes! This is because the RSA algorithm uses PKCS1 padding schema, which can only encrypt only 53 bytes at a time if your key size is 64 bytes (512 bits). I guess that it always pads 11 bytes.

To continue the test, I created a text file with 53 bytes, RSA_Input_53_Bytes.txt. Here is the result of the encryption step of the second test:

herong> javaJcePublicCipher RSA encrypt rsa.pub \ 
   RSA_Input_53_Bytes.txt rsa.cph

KeyFactory Object Info:
Algorithm = RSA
Provider = SunRsaSign version 12
toString = java.security.KeyFactory@1fb8ee3

Key Object Info:
Algorithm = RSA
Saved File = rsa.pub
Length = 94
Format = X.509
toString = Sun RSA public key, 512 bits
  modulus: 89805404347518883100317152161680408697320408608438740188039
6115229515610960443502949757801056895070926940886672738034912224273421
8612911902966014500229283
  public exponent: 65537

Cipher Object Info:
Block Size = 0
Algorithm = RSA
Provider = SunJCE version 12
toString = javax.crypto.Cipher@94948a

Cipher Process Info:
Input Size = 53
Output Size = 64

Here is the result of the decryption step:

herong> java JcePublicCipher RSA decrypt rsa.pri rsa.cph rsa.clr

KeyFactory Object Info:
Algorithm = RSA
Provider = SunRsaSign version 12
toString = java.security.KeyFactory@1fb8ee3

Key Object Info:
Algorithm = RSA
Saved File = rsa.pri
Length = 344
Format = PKCS#8
toString = Sun RSA private CRT key, 512 bits
  modulus:          89805404347518883100317152161680408697320408608438
7401880396115229515610960443502949757801056895070926940886672738034912
2242734218612911902966014500229283
  public exponent:  65537
  private exponent: 21604162609563799242559168423654627516089438975245
2077117450068704831516889727162134069384428521450311722251472165612082
8100869387183133950489977572509697
  prime p:          11254349822059181087482145123391808891111614563551
1624746047115252386382222849
  prime q:          79796172828656045903942442466972784531823950142255
130475957156528694706261667
  prime exponent p: 75694719899682109824240577825348664740727964103753
862813082850805040501426689
  prime exponent q: 49831667079030257575272782441152252201564899331249
152768504791877105882832159
  crt coefficient:  18772505426601783312760396170055433476231007074472
732727249064343427657206138

Cipher Object Info:
Block Size = 0
Algorithm = RSA
Provider = SunJCE version 12
toString = javax.crypto.Cipher@94948a

Cipher Process Info:
Input Size = 64
Output Size = 53

Now checking the decryption output with the original text:

comp RSA_Input_53_Bytes.txt rsa.clr
Comparing RSA_Input_53_Bytes.txt and rsa.clr...
Files compare OK

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

 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

 javax.crypto.Cipher - The Public Key Encryption Class

 JcePublicCipher.java - Public Key Encryption Sample Program

 DSA Public Key Encryption Tests

RSA Public Key Encryption Tests

 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

 Using Certificates in Google Chrome

 Using Certificates in Firefox

 Archived Tutorials

 References

 Full Version in PDF/EPUB