Cryptography Reference
In-Depth Information
message? She doesn't. This is why we append an entire block to messages that are already
perfect multiples of the block size.
Note that PKCS#5 padding has a limitation: It cannot be used for ciphers in which the
ciphertext block size exceeds 255 bytes. This should be simple to see if you note that each
byte of padding in PKCS#5 contains a binary number revealing the number of bytes of
padding. Clearly, 11111111 (base 2) = 255 is the largest number we can write in a byte, so a com-
plete block of 255 bytes would be padded as shown in Figure 5.4.
FIGURE 5.4
(message)
11111111
11111111
........
11111111
255 bytes of padding
Java Algorithm. Block ciphers are difficult to write, not because the enciphering trans-
formations are any more difficult, but because you must pad/unpad and block/unblock the
messages. To do all this, we will write a Ciphers class; it will contain methods to do all the
blocking and padding activities, and methods to encipher and decipher using various trans-
formations. The first will be the block affine transformation. For better readability (hope-
fully), the explanation for the code is interspersed with the code:
import java.math.*;
public class Ciphers {
The following is the padding method. You pass in the message and the block size. It
computes the number of blocks, then pads using the PKCS#5 scheme. This means padding
is added even if the message is a perfect multiple of the block size. It also means that any
ciphers using this method are effectively limited to a maximum block size of 255 bytes.
private static byte[] pad(byte[] msg,int blockSize) {
//Check that block size is proper for PKCS#5 padding
if (blockSize<1||blockSize>255) throw new
IllegalArgumentException(“Block size must be between 1 and 255.”);
//Pad the message
int numberToPad=blockSize-msg.length%blockSize;
byte[] paddedMsg=new byte[msg.length+numberToPad];
System.arraycopy(msg,0,paddedMsg,0,msg.length);
for (int i=msg.length;i<paddedMsg.length;i++) paddedMsg[i]=(byte)numberToPad;
return paddedMsg;
}
This method takes a padded message, then converts it to a 2-dimensional byte array.
Each “vector” in this 2D array is a block. The enciphering methods will work with this 2D
array.
 
Search WWH ::




Custom Search