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:

• The number of bytes to be padded equals to "blockSize - numberOfBytes(cleartext) mod blockSize".
• All padded bytes have the same value - the number of bytes padded.

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:

• Cleartext message block size "blockSize" should be calculated as "min(floor((RsaKeySize-1)/8), 256)".
• Number of padding bytes "numberOfPaddingBytes" should equal to "blockSize - numberOfBytes(cleartext) mod blockSize".
• All padding bytes should have the same value as "numberOfPaddingBytes mod blockSize".

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.