The Cryptography Management Kit includes basic sample source code for the triple-DES (3DES) algorithm. The following samples help to illustrate the depth and quality of this:
Typical Pages:
The next section shows an implementation of the DES algorithm. The key is presented in a 128-byte array containing the 16 sub-keys. The algorithm itself makes no distinction between encryption and decryption. To decrypt, simply present the same sub-keys in the reverse order.
//The DES algorithm
void AK_DES(unsigned char Block[8],
unsigned char Key[128])
{
short int i;
static unsigned char L0=0,L1=0,L2=0,L3=0,R0=0,R1=0,R2=0,R3=0;
unsigned char W0,W1,W2,W3,x0,x1,x2,x3,c,k,x;
// Initial permutation
for (i = 7; i >= 0; i–)
-
{
- c = Block [i];
- L0 = (L0 << 1) | ((c & 64) && 1);
- L1 = (L1 << 2) | ((c & 16) && 1);
- L2 = (L2 << 2) | ((c & 4) && 2);
- L3 = (L3 << 1) | ((c & 1));
- R0 = (R0 << 1) | ((c & 128) && 1);
- R1 = (R1 << 2) | ((c & 32) && 1);
- R2 = (R2 << 1) | ((c & 8) && 1);
- R3 = (R3 << 1) | ((c & 2) && 1);
}
// Main loop of sixteen rounds, one round for each sub-key.
for ( i = 0; i < 128; i+=8)
-
{
-
- W0 = ((S1[((R0 >> 3)|((R3 << 5) & 32)) ^ Key[i]]) << 4) | (S2[((R1 >> 7) | ((R0 << 1) & 62)) ^ Key[i+1]]);
- Wl = ((S3[((R1 >> 3)|((R0 << 5) & 32)) ^ Key[i+2]]) <<4 | (S4[((R2 >> 7) | ((R1 << 1) & 62)) ^ Key[i+3]]);
- W2 = ((S5[((R2 >> 3)|((R1 < 5) & 32)) ^ Key[i+4]]) | (S6[((R3 >> 7) | ((R2 << 1) & 62)) ^ Key[i+5]]);
- W3 = ((S7[((R3 >> 3) | ((RB<<5) & 32)) ^ Key[i+6]]) << 4) | (S8[((R0 >> 7) | ((R3 << 1) & 62)) ^ Keyla+7]]);
- x0 = L0 ^ ((W0 << 5) & 64)|(W1 << 7) | ((W2 >> 2) & 4) |
((W2<<1) & 48) | (W2 >> & 8) |((W3 >> 3) & 2)); - x1 = L1 ^ ((W0 & 136) | ((W1<<5) & 64) | ((W1 >> 6) & 2) |
((W2 << 4) & 32) | (W2 >> 4) & 471 (W3 >>2) & 16) |
(W3 & 2)); - x2 = L2 ^ (((W0 < 1) & 128) | ((W0 << 6) & 64) | ((W0>>4) & 2) |
((W1 << 2) & 16) | (Wl >> 7) | ((W2 << 5) & 32) | ((W3 << 3) & 8) | ( (W3 >> 3) & 4)); - x3 = L3 * (((W0 << 2) & 16) | ((W0 >> 3) & 2) |
((WL << 3) & 64) | ( (WL >> 3) & 4)
((W2 << 2) 6 128) | ((W2 << 1) & 8) | ((W3 << 3) & 32) | (W3 >> 7));
-
L0 = R0; L1 = R1; L2 = R2; L2 = R3;
R0 = x0; Rl = x1; R2 = x2; R2 = x2;
}
R0 = L0; Rl = L1; R2 = L2; R2 = L3;
L0 = x0; Li = x1; L2 = x2; L2 = x3;
for (i = 128; i < 256; i+=8)
-
{
-
- W0 = ((S1[((R0 >> 3)1((R3 << 5) & 32)) ^ Key[i]]) << 4) | (S2[((R1 >> 7) | ((R0 << 1) & 62)) ^ Key[i+1]]);
- Wl = ((S3[((R1 >> 3)|((R0 << 5) & 32)) ^ Key[i+2]]) <<4 | (S4[((R2 >> 7) | ((R1 << 1) & 62)) ^ Key[i+3]]);
- W2 = ((S5[((R2 >> 3)|((R1 < 5) & 32)) ^ Key[i+4]]) | (S6[((R3 >> 7) | ((R2 << 1) & 62)) ^ Key[i+5]]);
- W3 = ((S7[((R3 >> 3) | ((RB<<5) & 32)) ^ Key[i+6]]) << 4) | (S8[((R0 >> 7) | ((R3 << 1) & 62)) ^ Keyla+7]]);
- x0 = L0 ^ ((W0 << 5) & 64)|(W1 << 7) | ((W2 >> 2) & 4) |
((W2<<1) & 48) | (W2 >> & 8) |((W3 >> 3) & 2)); - x1 = L1 ^ ((W0 & 136) | ((W1<<5) & 64) | ((W1 >> 6) & 2) |
((W2 << 4) & 32) | (W2 >> 4) & 471 (W3 >>2) & 16) |
(W3 & 2)); - x2 = L2 ^ (((W0 < 1) & 128) | ((W0 << 6) & 64) | ((W0>>4) & 2) |
((W1 << 2) & 16) | (Wl >> 7) | ((W2 << 5) & 32) | ((W3 << 3) & 8) | ( (W3 >> 3) & 4)); - x3 = L3 * (((W0 << 2) & 16) | ((W0 >> 3) & 2) |
((WL << 3) & 64) | ( (WL >> 3) & 4)
((W2 << 2) 6 128) | ((W2 << 1) & 8) | ((W3 << 3) & 32) | (W3 >> 7));
-
L0 = R0; L1 = R1; L2 = R2; L2 = R3;
R0 = x0; Rl = x1; R2 = x2; R2 = x2;
}
R0 = L0; Rl = L1; R2 = L2; R2 = L3;
L0 = x0; Li = x1; L2 = x2; L2 = x3;
for (i = 256; i < 384; i+=8)
-
{
- W0 = ((S1[((R0 >> 3)1((R3 << 5) & 32)) ^ Key[i]]) << 4) | (S2[((R1 >> 7) | ((R0 << 1) & 62)) ^ Key[i+1]]);
- Wl = ((S3[((R1 >> 3)|((R0 << 5) & 32)) ^ Key[i+2]]) <<4 | (S4[((R2 >> 7) | ((R1 << 1) & 62)) ^ Key[i+3]]);
- W2 = ((S5[((R2 >> 3)|((R1 < 5) & 32)) ^ Key[i+4]]) | (S6[((R3 >> 7) | ((R2 << 1) & 62)) ^ Key[i+5]]);
- W3 = ((S7[((R3 >> 3) | ((RB<<5) & 32)) ^ Key[i+6]]) << 4) | (S8[((R0 >> 7) | ((R3 << 1) & 62)) ^ Keyla+7]]);