Cleartext Message Padding and Revised Block Size

This section discusses how to pad the cleartext message so that it can divided into blocks of the same size. The padding idea is borrowed from the PKCS5Padding schema.

Once the block size is determined, we need to find a schema to pad the cleartext message if it is not multiples of the block size.

Instead of developing a new padding schema, we can borrow the idea from the PKCS5Padding schema, which is defined as:

But this padding schema has a limitation: The maximum number of bytes we can pad is 256, because that is the maximum value we can present in a single byte.

So we have to modify our block size logic and padding schema to make it work:

Example 1:

Given the RSA key size: x = 16
Given the number of bytes in the cleartext message: y = 12
The block size should be: min(floor((16-1)/8),256) = 1
The number of padding bytes should be: 1 - 12 mod 1 = 1
The padding byte value should be: 1 mod 1 = 0
The padding byte sequence should be: 0x00

Example 2:

Given the RSA key size: x = 64
Given the number of bytes in the cleartext message: y = 12
The block size should be: min(floor((64-1)/8),256) = 7
The number of padding bytes should be: 7 - 12 mod 7 = 2
The padding byte value should be: 2 mod 7 = 0x02
The padding byte sequence should be: 0x0202

Example 3:

Given the RSA key size: x = 64
Given the number of bytes in the cleartext message: y = 21
The block size should be: min(floor((64-1)/8),256) = 7
The number of padding bytes should be: 7 - 21 mod 7 = 7
The padding byte value should be: 7 mod 7 = 0x00
The padding byte sequence should be: 0x00000000000000

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