Смекни!
smekni.com

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

('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:= 16 downto 1 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 TForm1.Button1Click(Sender: TObject);

var

output, put, key, c0, d0, k0: AnsiString;

e1: string[1];

e2: string[2];

begin

DecToBin(Form1.Memo1.Text, output);

BeginPerestanovka(output, put);

l:= Copy(put, 1, 32);

r:= Copy(put, 33, 64);

DecToBin(Form1.Memo3.Text, key);

PerestanovkaKeyB(key, key);

c0:= Copy(key, 1, 28);

d0:= Copy(key, 29, 56);

c[0]:= c0;

d[0]:= d0;

for w:= 1 to 2 do

begin

e1:= c[w-1];

c[w]:= Copy(c[w-1], 2, 28) + e1;

e1:= d[w-1];

d[w]:= Copy(d[w-1], 2, 28) + e1;

end;

for w:= 3 to 8 do

begin

e2:= c[w-1];

c[w]:= Copy(c[w-1], 3, 28) + e2;

e2:= d[w-1];

d[w]:= Copy(d[w-1], 3, 28) + e2;

end;

e1:= c[8];

c[9]:= Copy(c[8], 2, 28) + e1;

e1:= d[8];

d[w]:= Copy(d[8], 2, 28) + e1;

for w:= 10 to 15 do

begin

e2:= c[w-1];

c[w]:= Copy(c[w-1], 3, 28) + e2;

e2:= d[w-1];

d[w]:= Copy(d[w-1], 3, 28) + e2;

end;

e1:= c[15];

c[16]:= Copy(c[15], 2, 28) + e1;

e1:= d[15];

d[16]:= Copy(d[15], 2, 28) + e1;

pocledovatelnostiK;

FuncE;

Form1.Memo2.Text:= rez;

end;

procedure TForm1.Button2Click(Sender: TObject);

var

output, put, key, c0, d0, k0: AnsiString;

e1: string[1];

e2: string[2];

begin

DecToBin(Form1.Memo2.Text, output);

BeginPerestanovka(output, put);

l:= Copy(put, 1, 32);

r:= Copy(put, 33, 64);

DecToBin(Form1.Memo3.Text, key);

PerestanovkaKeyB(key, key);

c0:= Copy(key, 1, 28);

d0:= Copy(key, 29, 56);

c[0]:= c0;

d[0]:= d0;

for w:= 1 to 2 do

begin

e1:= c[w-1];

c[w]:= Copy(c[w-1], 2, 28) + e1;

e1:= d[w-1];

d[w]:= Copy(d[w-1], 2, 28) + e1;

end;

for w:= 3 to 8 do

begin

e2:= c[w-1];

c[w]:= Copy(c[w-1], 3, 28) + e2;

e2:= d[w-1];

d[w]:= Copy(d[w-1], 3, 28) + e2;

end;

e1:= c[8];

c[9]:= Copy(c[8], 2, 28) + e1;

e1:= d[8];

d[w]:= Copy(d[8], 2, 28) + e1;

for w:= 10 to 15 do

begin

e2:= c[w-1];

c[w]:= Copy(c[w-1], 3, 28) + e2;

e2:= d[w-1];

d[w]:= Copy(d[w-1], 3, 28) + e2;

end;

e1:= c[15];

c[16]:= Copy(c[15], 2, 28) + e1;

e1:= d[15];

d[16]:= Copy(d[15], 2, 28) + e1;

pocledovatelnostiK;

FuncER;

Form1.Memo1.Text:= rez;

end;

end.