Mastering Algorithms With C - Kyle Loudon [185]
We repeat this process 16 times, once for each round. After all 16 rounds are complete, we copy rblk into the first 32 bits of target and lblk into the second 32 bits (effectively negating the last swap of the left and right blocks, as is required). At last, we perform the final permutation by calling permute and passing it the table Des_Final (the same table as in Table 15.8).
The runtime complexity of des_encipher is O (1) because all of the steps in enciphering a block of data run in a constant amount of time.
des_decipher
The des_decipher operation (see Example 15.2) deciphers a 64-bit block of ciphertext enciphered using DES. Like des_encipher, des_decipher actually calls des_main to decipher the data, but with direction set to decipher. Thus, des_decipher works just like des_encipher, except that the subkeys are applied in reverse order. Specifically, in des_main, for each round i (starting at 0), we apply the subkey in element 15 - i of subkeys.
The runtime complexity of des_decipher is O (1) because all of the steps in deciphering a block of data run in a constant amount of time.
Example 15.2. Implementation of DES
/*****************************************************************************
* *
* --------------------------------- des.c -------------------------------- *
* *
*****************************************************************************/
#include #include #include #include "bit.h" #include "encrypt.h" /***************************************************************************** * * * Define a mapping for the key transformation. * * * *****************************************************************************/ static const int DesTransform[56] = { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 }; /***************************************************************************** * * * Define the number of rotations for computing subkeys. * * * *****************************************************************************/ static const int DesRotations[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; /***************************************************************************** * * * Define a mapping for the permuted choice for subkeys. * * * *****************************************************************************/ static const int DesPermuted[48] = { 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 }; /***************************************************************************** * * * Define a mapping for the initial permutation of data blocks. * * * *****************************************************************************/ static const int DesInitial[64] = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 }; /***************************************************************************** * * * Define a mapping for the expansion permutation of data blocks. * * * *****************************************************************************/ static const int DesExpansion[48]