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. It is done with JDK 1.8.

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

KeyFactory Object Info:
Algorithm = rsa
Provider = SunRsaSign version 1.8
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.8
toString = javax.crypto.Cipher@94948a
Exception: javax.crypto.IllegalBlockSizeException: 
   Data must not be longer than 53 bytes

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:

>java JcePublicCipher RSA encrypt rsa.pub RSA_Input_53_Bytes.txt rsa.cph

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

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

Cipher Object Info:
Block Size = 0
Algorithm = RSA
Provider = SunJCE version 1.8
toString = javax.crypto.Cipher@ed3ef1

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

Here is the result of the decryption step:

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

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

Key Object Info:
Algorithm = RSA
Saved File = rsa.pri
Length = 344
Format = PKCS#8
toString = sun.security.rsa.RSAPrivateCrtKeyImpl@fffe4919

Cipher Object Info:
Block Size = 0
Algorithm = RSA
Provider = SunJCE version 1.8
toString = javax.crypto.Cipher@ed3ef1

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

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

Cipher - Secret Key Encryption and Decryption

 javax.crypto.Cipher - The Secret Key Encryption Class

 JceSecretCipher.java - Secret Key Encryption Sample Program

 Blowfish and DES Secret Key Encryption Tests

 JcePublicCipher.java - Private and Public Key Encryption

 DSA Public Key Encryption Tests

RSA Public Key Encryption Tests

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 PDF Printing Version