var G: array[1..16, 1..48] of Byte; L, R, F: TByte32; C: array[1..56] of Byte;
procedure DES_Init(Key: TBlock; FCode: Boolean); var n, h: Byte; begin C[1] := Ord(Key[7] and 128 > 0); C[29] := Ord(Key[7] and 2 > 0); C[2] := Ord(Key[6] and 128 > 0); C[30] := Ord(Key[6] and 2 > 0); C[3] := Ord(Key[5] and 128 > 0); C[31] := Ord(Key[5] and 2 > 0); C[4] := Ord(Key[4] and 128 > 0); C[32] := Ord(Key[4] and 2 > 0); C[5] := Ord(Key[3] and 128 > 0); C[33] := Ord(Key[3] and 2 > 0); C[6] := Ord(Key[2] and 128 > 0); C[34] := Ord(Key[2] and 2 > 0); C[7] := Ord(Key[1] and 128 > 0); C[35] := Ord(Key[1] and 2 > 0); C[8] := Ord(Key[0] and 128 > 0); C[36] := Ord(Key[0] and 2 > 0);
C[9] := Ord(Key[7] and 64 > 0); C[37] := Ord(Key[7] and 4 > 0); C[10] := Ord(Key[6] and 64 > 0); C[38] := Ord(Key[6] and 4 > 0); C[11] := Ord(Key[5] and 64 > 0); C[39] := Ord(Key[5] and 4 > 0); C[12] := Ord(Key[4] and 64 > 0); C[40] := Ord(Key[4] and 4 > 0); C[13] := Ord(Key[3] and 64 > 0); C[41] := Ord(Key[3] and 4 > 0); C[14] := Ord(Key[2] and 64 > 0); C[42] := Ord(Key[2] and 4 > 0); C[15] := Ord(Key[1] and 64 > 0); C[43] := Ord(Key[1] and 4 > 0); C[16] := Ord(Key[0] and 64 > 0); C[44] := Ord(Key[0] and 4 > 0);
C[17] := Ord(Key[7] and 32 > 0); C[45] := Ord(Key[7] and 8 > 0); C[18] := Ord(Key[6] and 32 > 0); C[46] := Ord(Key[6] and 8 > 0); C[19] := Ord(Key[5] and 32 > 0); C[47] := Ord(Key[5] and 8 > 0); C[20] := Ord(Key[4] and 32 > 0); C[48] := Ord(Key[4] and 8 > 0); C[21] := Ord(Key[3] and 32 > 0); C[49] := Ord(Key[3] and 8 > 0); C[22] := Ord(Key[2] and 32 > 0); C[50] := Ord(Key[2] and 8 > 0); C[23] := Ord(Key[1] and 32 > 0); C[51] := Ord(Key[1] and 8 > 0); C[24] := Ord(Key[0] and 32 > 0); C[52] := Ord(Key[0] and 8 > 0);
C[25] := Ord(Key[7] and 16 > 0); C[53] := Ord(Key[3] and 16 > 0); C[26] := Ord(Key[6] and 16 > 0); C[54] := Ord(Key[2] and 16 > 0); C[27] := Ord(Key[5] and 16 > 0); C[55] := Ord(Key[1] and 16 > 0); C[28] := Ord(Key[4] and 16 > 0); C[56] := Ord(Key[0] and 16 > 0);
if FCode then begin for n := 1 to 16 do begin for h := 1 to 48 do begin G[n, h] := C[Sc[n, h]]; end; end; end else begin for n := 1 to 16 do begin for h := 1 to 48 do begin G[17 - n, h] := C[Sc[n, h]]; end; end; end; end;
procedure DES_Code(Input: TBlock; var Output: TBlock); var n: Byte; z: Word; begin L[1] := Ord(Input[7] and 64 > 0); R[1] := Ord(Input[7] and 128 > 0); L[2] := Ord(Input[6] and 64 > 0); R[2] := Ord(Input[6] and 128 > 0); L[3] := Ord(Input[5] and 64 > 0); R[3] := Ord(Input[5] and 128 > 0); L[4] := Ord(Input[4] and 64 > 0); R[4] := Ord(Input[4] and 128 > 0); L[5] := Ord(Input[3] and 64 > 0); R[5] := Ord(Input[3] and 128 > 0); L[6] := Ord(Input[2] and 64 > 0); R[6] := Ord(Input[2] and 128 > 0); L[7] := Ord(Input[1] and 64 > 0); R[7] := Ord(Input[1] and 128 > 0); L[8] := Ord(Input[0] and 64 > 0); R[8] := Ord(Input[0] and 128 > 0); L[9] := Ord(Input[7] and 16 > 0); R[9] := Ord(Input[7] and 32 > 0); L[10] := Ord(Input[6] and 16 > 0); R[10] := Ord(Input[6] and 32 > 0); L[11] := Ord(Input[5] and 16 > 0); R[11] := Ord(Input[5] and 32 > 0); L[12] := Ord(Input[4] and 16 > 0); R[12] := Ord(Input[4] and 32 > 0); L[13] := Ord(Input[3] and 16 > 0); R[13] := Ord(Input[3] and 32 > 0); L[14] := Ord(Input[2] and 16 > 0); R[14] := Ord(Input[2] and 32 > 0); L[15] := Ord(Input[1] and 16 > 0); R[15] := Ord(Input[1] and 32 > 0); L[16] := Ord(Input[0] and 16 > 0); R[16] := Ord(Input[0] and 32 > 0); L[17] := Ord(Input[7] and 4 > 0); R[17] := Ord(Input[7] and 8 > 0); L[18] := Ord(Input[6] and 4 > 0); R[18] := Ord(Input[6] and 8 > 0); L[19] := Ord(Input[5] and 4 > 0); R[19] := Ord(Input[5] and 8 > 0); L[20] := Ord(Input[4] and 4 > 0); R[20] := Ord(Input[4] and 8 > 0); L[21] := Ord(Input[3] and 4 > 0); R[21] := Ord(Input[3] and 8 > 0); L[22] := Ord(Input[2] and 4 > 0); R[22] := Ord(Input[2] and 8 > 0); L[23] := Ord(Input[1] and 4 > 0); R[23] := Ord(Input[1] and 8 > 0); L[24] := Ord(Input[0] and 4 > 0); R[24] := Ord(Input[0] and 8 > 0); L[25] := Input[7] and 1; R[25] := Ord(Input[7] and 2 > 0); L[26] := Input[6] and 1; R[26] := Ord(Input[6] and 2 > 0); L[27] := Input[5] and 1; R[27] := Ord(Input[5] and 2 > 0); L[28] := Input[4] and 1; R[28] := Ord(Input[4] and 2 > 0); L[29] := Input[3] and 1; R[29] := Ord(Input[3] and 2 > 0); L[30] := Input[2] and 1; R[30] := Ord(Input[2] and 2 > 0); L[31] := Input[1] and 1; R[31] := Ord(Input[1] and 2 > 0); L[32] := Input[0] and 1; R[32] := Ord(Input[0] and 2 > 0);
for n := 1 to 16 do begin z := ((R[32] xor G[n, 1]) shl 5) or ((R[5] xor G[n, 6]) shl 4) or ((R[ 1] xor G[n, 2]) shl 3) or ((R[2] xor G[n, 3]) shl 2) or ((R[ 3] xor G[n, 4]) shl 1) or ( R[4] xor G[n, 5]); F[ 9] := L[ 9] xor SA1[z]; F[17] := L[17] xor SB1[z]; F[23] := L[23] xor SC1[z]; F[31] := L[31] xor SD1[z];
Output[0] := (L[8] shl 7) or (R[8] shl 6) or (L[16] shl 5) or (R[16] shl 4) or (L[24] shl 3) or (R[24] shl 2) or (L[32] shl 1) or R[32]; Output[1] := (L[7] shl 7) or (R[7] shl 6) or (L[15] shl 5) or (R[15] shl 4) or (L[23] shl 3) or (R[23] shl 2) or (L[31] shl 1) or R[31]; Output[2] := (L[6] shl 7) or (R[6] shl 6) or (L[14] shl 5) or (R[14] shl 4) or (L[22] shl 3) or (R[22] shl 2) or (L[30] shl 1) or R[30]; Output[3] := (L[5] shl 7) or (R[5] shl 6) or (L[13] shl 5) or (R[13] shl 4) or (L[21] shl 3) or (R[21] shl 2) or (L[29] shl 1) or R[29]; Output[4] := (L[4] shl 7) or (R[4] shl 6) or (L[12] shl 5) or (R[12] shl 4) or (L[20] shl 3) or (R[20] shl 2) or (L[28] shl 1) or R[28]; Output[5] := (L[3] shl 7) or (R[3] shl 6) or (L[11] shl 5) or (R[11] shl 4) or (L[19] shl 3) or (R[19] shl 2) or (L[27] shl 1) or R[27]; Output[6] := (L[2] shl 7) or (R[2] shl 6) or (L[10] shl 5) or (R[10] shl 4) or (L[18] shl 3) or (R[18] shl 2) or (L[26] shl 1) or R[26]; Output[7] := (L[1] shl 7) or (R[1] shl 6) or (L[9] shl 5) or (R[9] shl 4) or (L[17] shl 3) or (R[17] shl 2) or (L[25] shl 1) or R[25]; end;
function StrToKey(aKey: string): TBlock; var Key : TBlock; I : Integer; begin FillChar(Key, SizeOf(TBlock), 0); for I := 1 to Length(aKey) do begin Key[I mod SizeOf(TBlock)] := Key[I mod SizeOf(TBlock)] + Ord(aKey[I]); end;