Cryptography Reference
In-Depth Information
private static byte[][] block(byte[] msg,int blockSize) {
//Create a 2D array of bytes corresponding to the message-all blocks should be
//full
int numberOfBlocks=msg.length/blockSize;
byte[][] ba=new byte[numberOfBlocks][blockSize];
for (int i=0;i<numberOfBlocks;i++)
for (int j=0;j<blockSize;j++)
ba[i][j]=msg[i*blockSize+j];
return ba;
}
This method “unblocks” the message; that is, after the enciphering or deciphering trans-
formation, it takes the 2D array of blocks, then converts it back to a linear array of bytes.
The method must be careful to take into account that the enciphering or deciphering trans-
formation may produce an integer smaller than the block size. In that case, it fills in the lin-
ear array from the rear of the block.
private static byte[] unBlock(byte[][] ba,int blockSize) {
//Create the 1D array in which to place the enciphered blocks
byte[] m2=new byte[ba.length*blockSize];
//Place the blocks in the 1D array
for (int i=0;i<ba.length;i++) {
int j=blockSize-1;
int k=ba[i].length-1;
while (k>=0) {
m2[i*blockSize+j]=ba[i][k];
k--;
j--;
}
}
return m2;
}
This method removes the padding. All it has to do is examine the numerical value in the
last block, then remove exactly that many blocks.
private static byte[] unPad(byte[] msg,int blockSize) {
//Determine the amount of padding-just look at last block
int numberOfPads=(msg[msg.length-1]+256)%256;
//Chop off the padding and return the array
byte[] answer=new byte[msg.length-numberOfPads];
System.arraycopy(msg,0,answer,0,answer.length);
return answer;
}
Search WWH ::




Custom Search