Смекни!
smekni.com

Шифрование DES - теория и практика (стр. 4 из 5)

Текст программы

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Memo1: TMemo;

Memo2: TMemo;

Label1: TLabel;

Label2: TLabel;

Button1: TButton;

Button2: TButton;

Memo3: TMemo;

Label3: TLabel;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

c, d, k: array [0..16] of AnsiString;

r, l, rez, z: AnsiString;

w, desK, desR: byte;

implementation

{$R *.dfm}

procedure DecToBin(input: string; var output: ansistring);

var

a, b: double;

ost, q, w: byte;

st: AnsiString;

str: string[8];

begin

str:= ' ';

for w:= 1 to 8 do

begin

q:= Ord(input[w]);

b:= q;

While b <> 1 do

begin

a:= q / 2;

b:= Int(a);

if a = b then

ost:= 0

else ost:= 1;

q:= Round(b);

st:= st + IntToStr(ost);

if b = 1 then st:= st + '1';

end;

ost:= 1;

for q:= Length(st) downto 1 do // ïåðåâîðîò ñòðîêè

begin

str[ost]:= st[q];

Inc(ost);

end;

case Length(st) of

1: Insert('0000000', str, 1); // äîáàâëåíèå íåäîñòàþùèõ íîëåé

2: Insert('000000', str, 1);

3: Insert('00000', str, 1);

4: Insert('0000', str, 1);

5: Insert('000', str, 1);

6: Insert('00', str, 1);

7: Insert('0', str, 1);

end;

output:= output + str;// + ' ';

str:= ' ';

st:= '';

end;

end;

procedure BeginPerestanovka(input: ansistring; var output: ansistring);

begin

output:=

input[58] + input[50] + input[42] + input[34] + input[26] + input[18]

+ input[10] + input[2]

+ input[60] + input[52] + input[44] + input[36] + input[28] + input[20]

+ input[12] + input[4]

+ input[62] + input[54] + input[46] + input[38] + input[30] + input[22]

+ input[14] + input[6]

+ input[64] + input[56] + input[48] + input[40] + input[32] + input[24]

+ input[16] + input[8]

+ input[57] + input[49] + input[41] + input[33] + input[25] + input[17]

+ input[9] + input[1]

+ input[59] + input[51] + input[43] + input[35] + input[27] + input[19]

+ input[11] + input[3]

+ input[61] + input[53] + input[45] + input[37] + input[29] + input[21]

+ input[13] + input[5]

+ input[63] + input[55] + input[47] + input[39] + input[31] + input[23]

+ input[15] + input[7];

end;

procedure PerestanovkaKeyB(input: AnsiString; var output: AnsiString);

begin

output:= '';

output:=

input[57]+input[49]+input[41]+input[33]+input[25]+input[17]+input[9]

+input[1]+input[58]+input[50]+input[42]+input[34]+input[26]+input[18]

+input[10]+input[2]+input[59]+input[51]+input[43]+input[35]+input[27]

+input[19]+input[11]+input[3]+input[60]+input[52]+input[44]+input[36]

+input[63]+input[55]+input[47]+input[39]+input[31]+input[23]+input[15]

+input[7]+input[62]+input[54]+input[46]+input[38]+input[30]+input[22]

+input[14]+input[6]+input[61]+input[53]+input[45]+input[37]+input[29]

+input[21]+input[13]+input[5]+input[28]+input[20]+input[12]+input[4];

end;

procedure pocledovatelnostiK;

var

w: byte;

bufer: AnsiString;

begin

for w:= 0 to 16 do

begin

bufer:= Concat(c[w], d[w]);

k[w]:=

Concat(bufer[14], bufer[17], bufer[11], bufer[24], bufer[1], bufer[5],

bufer[3], bufer[28], bufer[15], bufer[6], bufer[21], bufer[10],

bufer[23], bufer[19], bufer[12], bufer[4], bufer[26], bufer[8],

bufer[16], bufer[7], bufer[27], bufer[20], bufer[13], bufer[2],

bufer[41], bufer[52], bufer[31], bufer[37], bufer[47], bufer[55],

bufer[30], bufer[40], bufer[51], bufer[45], bufer[33], bufer[48],

bufer[44], bufer[49], bufer[39], bufer[56], bufer[34], bufer[53],

bufer[46], bufer[42], bufer[50], bufer[36], bufer[29], bufer[32]);

end;

end;

procedure FuncE;

const

s1 : array[0..3, 0..15] of string[4] =

(('1110','0100','1101','0001','0010','1111','1011','1000','0011','1010','0110','1100','0101','1001','0000','0111'),

('0000','1111','0111','0100','1110','0010','1101','0001','1010','0110','1100','1011','1001','0101','0011','1000'),

('0100','0001','1110','1000','1001','0110','0010','1011','1111','1100','1001','0111','0011','1010','0101','0000'),

('1111','1100','1000','0010','0100','1001','0001','0111','0101','1011','0011','1110','1010','0000','1001','1101'));

s2 : array[0..3, 0..15] of string[4] =

(('1111','0001','1000','1110','0110','1011','0011','0100','1001','0111','0010','1101','1100','0000','0101','1010'),

('0011','1101','0100','0111','1111','0010','1000','1110','1100','0000','0001','1010','0110','1001','1011','0101'),

('0000','1110','0111','1011','1010','0100','1101','0001','0101','1000','1100','0110','1001','0011','0010','1111'),

('1101','1000','1010','0001','0011','1111','0100','0010','1011','0110','0111','1100','0000','0101','1110','1001'));

s3 : array [0..3, 0..15] of string[4] =

(('1010','0000','1001','1110','0110','0011','1111','0101','0001','1101','1100','0111','1011','0100','0010','1000'),

('1101','0111','0000','1001','0011','0100','0110','1010','0010','1000','0101','1110','1100','1011','1111','0001'),

('1101','0110','0100','1001','1000','1111','0011','0000','1011','0001','0010','1100','0101','1010','1110','0111'),

('0001','1010','1101','0000','0110','1001','1000','0111','0100','1111','1110','0011','1011','0101','0010','1100'));

s4 : array [0..3, 0..15] of string[4] =

(('0111','1101','1110','0011','0000','0110','1001','1010','0001','0010','1000','0101','1011','1100','0100','1111'),

('1101','1000','1011','0101','0110','1111','0000','0011','0100','0111','0010','1100','0001','1010','1110','1001'),

('1010','0110','1001','0000','1100','1011','0111','1101','1111','0001','0011','1110','0101','0010','1000','0100'),

('0011','1111','0000','0110','1010','0001','1101','1000','1001','0100','0101','1011','1100','0111','0010','1110'));

s5 : array [0..3, 0..15] of string[4] =

(('0010','1100','0100','0001','0111','1010','1011','0110','1000','0101','0011','1111','1101','0000','1110','1001'),

('1110','1011','0010','1100','0100','0111','1101','0001','0101','0000','1111','1010','0011','1001','1000','0110'),

('0100','0010','0001','1011','1010','1101','0111','1000','1111','1001','1100','0101','0110','0011','0000','1110'),

('1011','1000','1100','0111','0001','1110','0010','1101','0110','1111','0000','1001','1010','0100','0101','0011'));

s6 : array [0..3, 0..15] of string[4] =

(('1100','0001','1010','1111','1001','0010','0110','1000','0000','1101','0011','0100','1110','0111','0101','1011'),

('1010','1111','0100','0010','0111','1100','1001','0101','0110','0001','1101','1110','0000','1011','0011','1000'),

('1001','1110','1111','0101','0010','1000','1100','0011','0111','0000','0100','1010','0001','1101','1011','0110'),

('0100','0011','0010','1100','1001','0101','1111','1010','1011','1110','0001','0111','0110','0000','1000','1101'));

s7 : array [0..3, 0..15] of string[4] =

(('0100','1011','0010','1110','1111','0000','1000','1101','0011','1100','1001','0111','0101','1010','0110','0001'),

('1101','0000','1011','0111','0100','1001','0001','1010','1110','0011','0101','1100','0011','1111','1000','0110'),

('0001','0100','1011','1101','1100','0011','0111','1110','1010','1111','0110','1000','0000','0101','1001','0010'),

('0110','1011','1101','1000','0001','0100','1010','0111','1001','0101','0000','1111','1110','0010','0011','1100'));

s8 : array [0..3, 0..15] of string[4] =

(('1101','0010','1000','0100','0110','1111','1011','0001','1010','1001','0011','1110','0101','0000','1100','0111'),

('0001','1111','1101','1000','1010','0011','0111','0100','1100','0101','0110','1011','0000','1110','1001','0011'),

('0111','1011','0100','0001','1001','1100','1110','0010','0000','0110','1010','1101','1111','0011','0101','1000'),

('0010','0001','1110','0111','0100','1010','1000','1101','1111','1100','1001','0000','0011','0101','0110','1011'));

var

_1, _2: AnsiString;

p: string[6];

v, b, x, a, j: byte;

u: string[2];

o: string[4];

function s(var a, b: byte): byte;

begin

if u = '00' then a:= 0

else if u = '01' then a:= 1

else if u = '10' then a:= 2

else if u = '11' then a:= 3;

if o = '0000' then b:= 0

else if o = '0001' then b:= 1

else if o = '0010' then b:= 2

else if o = '0011' then b:= 3

else if o = '0100' then b:= 4

else if o = '0101' then b:= 5

else if o = '0110' then b:= 6

else if o = '0111' then b:= 7

else if o = '1000' then b:= 8

else if o = '1001' then b:= 9

else if o = '1010' then b:= 10

else if o = '1011' then b:= 11

else if o = '1100' then b:= 12

else if o = '1101' then b:= 13

else if o = '1110' then b:= 14

else if o = '1111' then b:= 15;

end;

begin

for a:= 1 to 16 do

begin

z:= Concat(r[32],r[1],r[2],r[3],r[4],r[5],r[4],r[5],r[6],r[7],r[8],r[9],

r[8],r[9],r[10],r[11],r[12],r[13],r[12],r[13],r[14],r[15],r[16],r[17],

r[16],r[17],r[18],r[19],r[20],r[21],r[20],r[21],r[21],r[23],r[24],r[25],

r[24],r[25],r[26],r[27],r[28],r[29],r[28],r[29],r[30],r[31],r[32],r[1]);

_1:= k[a];

r:= z;

for j:= 1 to 48 do // xor

begin

v:= StrToInt(r[j]);

b:= StrToInt(_1[j]);

x:= v xor b;

_2:= Concat(_2, IntToStr(x));

end;

p:= Copy(_2, 1, 6); u:= p[1] + p[6]; o:= Copy(p, 2, 5); s(v, b);

rez:= rez + s1[v, b];

p:= Copy(_2, 7, 12); u:= p[1] + p[6]; o:= Copy(p, 2, 5); s(v, b);

rez:= rez + s2[v, b];

p:= Copy(_2, 13, 18); u:= p[1] + p[6]; o:= Copy(p, 2, 5); s(v, b);

rez:= rez + s3[v, b];

p:= Copy(_2, 19, 24); u:= p[1] + p[6]; o:= Copy(p, 2, 5); s(v, b);

rez:= rez + s4[v, b];

p:= Copy(_2, 25, 30); u:= p[1] + p[6]; o:= Copy(p, 2, 5); s(v, b);

rez:= rez + s5[v, b];

p:= Copy(_2, 31, 36); u:= p[1] + p[6]; o:= Copy(p, 2, 5); s(v, b);

rez:= rez + s6[v, b];

p:= Copy(_2, 37, 42); u:= p[1] + p[6]; o:= Copy(p, 2, 5); s(v, b);

rez:= rez + s7[v, b];

p:= Copy(_2, 43, 48); u:= p[1] + p[6]; o:= Copy(p, 2, 5); s(v, b);

rez:= rez + s8[v, b];

// Ð ïåðåñòàíîâêà

_2:= rez;

rez:= Concat(_2[16], _2[7], _2[20], _2[21], _2[29], _2[12], _2[28], _2[17],

_2[1], _2[15], _2[23], _2[26], _2[5], _2[18], _2[31], _2[10],

_2[2], _2[8], _2[24], _2[14], _2[32], _2[27], _2[3], _2[9],

_2[19], _2[13], _2[30], _2[6], _2[22], _2[11], _2[4], _2[25]);

for w:= 1 to 32 do

begin

v:= StrToInt(r[w]);

b:= StrToInt(l[w]);

x:= v xor b;

_2:= Concat(_2, IntToStr(x));

end;

l:= r;

r:= _2;

end;

_2:= Concat(l, r);

rez:= Concat(

_2[40], _2[8], _2[48], _2[16], _2[56], _2[24], _2[64], _2[32],

_2[39], _2[7], _2[47], _2[15], _2[55], _2[23], _2[63], _2[31],

_2[38], _2[6], _2[46], _2[14], _2[54], _2[22], _2[62], _2[30],

_2[37], _2[5], _2[45], _2[13], _2[53], _2[21], _2[61], _2[29],

_2[36], _2[4], _2[44], _2[12], _2[52], _2[20], _2[60], _2[28],

_2[35], _2[3], _2[43], _2[11], _2[51], _2[19], _2[59], _2[27],

_2[34], _2[2], _2[42], _2[10], _2[50], _2[18], _2[58], _2[26],

_2[33], _2[1], _2[41], _2[9], _2[49], _2[17], _2[57], _2[25]);

end;

procedure FuncER;

const

s1 : array[0..3, 0..15] of string[4] =

(('1110','0100','1101','0001','0010','1111','1011','1000','0011','1010','0110','1100','0101','1001','0000','0111'),

('0000','1111','0111','0100','1110','0010','1101','0001','1010','0110','1100','1011','1001','0101','0011','1000'),

('0100','0001','1110','1000','1001','0110','0010','1011','1111','1100','1001','0111','0011','1010','0101','0000'),