Смекни!
smekni.com

Использование современных симметрических DES и асимметрических RSA алгоритмов шифрования (стр. 5 из 5)

B_tekst_32:odnom_1_32;

begin

TextVBinarn (Ish_tekst_bykv,Ish_tekst_64_binar);

Perestanovka_IP (Ish_tekst_64_binar);

//Delim na levyju i na pravyju 4asti

j:=0;

for i:=1 to 64 do

if i<33 then

levaja[1,i]:=Ish_tekst_64_binar[i]

else

begin

j:=j+1;

pravaja[1,j]:=Ish_tekst_64_binar[i];

end;

//16 rayndov wifrovanija

for i:=2 to 17 do

begin

for j:=1 to 32 do

levaja[i,j]:=pravaja[i-1,j];

Perestanovka_E(i-1,pravaja,Tekst_48_posle_raswirenija);

for j:=1 to 48 do

Tekst_48_posle_raswirenija[j]:=Tekst_48_posle_raswirenija[j] xor podklu4i[i-1,j];

s_blok(Tekst_48_posle_raswirenija,B_tekst_32);

Perestanovka_P(B_tekst_32);

for j:=1 to 32 do

pravaja[i,j]:=levaja[i-1,j] xor B_tekst_32[j];

end;

//!!!

//Menjaem mestami

for i:=1 to 32 do

begin

j:= pravaja[17][i];

pravaja[17][i] := levaja[17][i];

levaja[17][i] := j;

end;

//Skladivanie levoi i pravoi

j:=0;

for i:=1 to 64 do

if i<33 then

Ish_tekst_64_binar[i]:=levaja[17,i]

else

begin

j:=j+1;

Ish_tekst_64_binar[i]:=pravaja[17,j];

end;

Perestanovka_IP_1(Ish_tekst_64_binar);

BinarnVText(Ish_tekst_64_binar,Ish_tekst_bykv);

kodirovat:= Ish_tekst_bykv;

end;

function Raskodirovat(Wifr_tekst_bykv:string):string;

var Wifr_tekst_64_binar:odnom_1_64;

i,j:integer;

levaja,pravaja:dvym_1_17_1_32;

Tekst_48_posle_raswirenija:odnom_1_48;

B_tekst_32:odnom_1_32;

begin

TextVBinarn (Wifr_tekst_bykv,Wifr_tekst_64_binar);

Perestanovka_IP (Wifr_tekst_64_binar);

//Delim na levyju i na pravyju 4asti

j:=0;

for i:=1 to 64 do

if i<33 then

levaja[17,i]:=Wifr_tekst_64_binar[i]

else

begin

j:=j+1;

pravaja[17,j]:=Wifr_tekst_64_binar[i];

end;

//!!!!!!!!

//Menjaem mestami

for i:=1 to 32 do

begin

j:= pravaja[17][i];

pravaja[17][i]:=levaja[17][i];

levaja[17][i]:=j;

end;

//16 raundov wifrovanija

for i:=17 downto 2 do

begin

for j:=1 to 32 do

pravaja[i-1,j]:=levaja[i,j];

Perestanovka_E(i,levaja,Tekst_48_posle_raswirenija);

for j:=1 to 48 do

Tekst_48_posle_raswirenija[j]:=Tekst_48_posle_raswirenija[j] xor podklu4i[i-1,j];

s_blok(Tekst_48_posle_raswirenija,B_tekst_32);

Perestanovka_P(B_tekst_32);

for j:=1 to 32 do

levaja[i-1,j]:=pravaja[i,j] xor B_tekst_32[j];

end;

//Skladivanie levoi i pravoi

j:=0;

for i:=1 to 64 do

if i<33 then

Wifr_tekst_64_binar[i]:=levaja[1,i]

else

begin

j:=j+1;

Wifr_tekst_64_binar[i]:=pravaja[1,j];

end;

Perestanovka_IP_1(Wifr_tekst_64_binar);

BinarnVText(Wifr_tekst_64_binar,Wifr_tekst_bykv);

Raskodirovat:=Wifr_tekst_bykv;

end;

end.



unit Podkluchi_unit;

interface

uses DES_unit;

var

podklu4i:dvym_1_16_1_48;

matrica_PC1:array[1..56] of byte=(57, 49, 41, 33, 25, 17, 09,

01, 58, 50, 42, 34, 26, 18,

10, 02, 59, 51, 43, 35, 27,

19, 11, 03, 60, 52, 44, 36,

63, 55, 47, 39, 31, 23, 15,

07, 62, 54, 46, 38, 30, 22,

14, 06, 61, 53, 45, 37, 29,

21, 13, 05, 28, 20, 12, 04);

matrica_PC2:array[1..48] of byte=(14, 17, 11, 24, 01, 05,

03, 28, 15, 06, 21, 10,

23, 19, 12, 04, 26, 08,

16, 07, 27, 20, 13, 02,

41, 52, 31, 37, 47, 55,

30, 40, 51, 45, 33, 48,

44, 49, 39, 56, 34, 53,

46, 42, 50, 36, 29, 32);

matrica_sdvigov:array[1..16] of byte=(1,1,2,2,

2,2,2,2,

1,2,2,2,

2,2,2,1);

procedure Formirovanie_16_podklju4ei(Klju4_64:string);

implementation

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++

procedure Perestanovka_PC1(Klju4_64_do_PC1: odnom_1_64; var Klju4_64_posle_PC1:odnom_1_56);

var i:integer;

begin

for i:=1 to 56 do

Klju4_64_posle_PC1[i]:=Klju4_64_do_PC1[matrica_PC1[i]];

end;

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++

procedure Perestanovka_PC2(Klju4_56_do_PC2:odnom_1_56; k:integer);

var i:integer;

begin

for i:=1 to 48 do

podklu4i[k][i]:=Klju4_56_do_PC2[matrica_PC2[i]];

end;

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++

procedure Formirovanie_16_podklju4ei(Klju4_64:string);

var

i,j,k:integer;

c_vrem,d_vrem: dvym_1_17_1_28;

c: dvym_1_17_1_28;

d: dvym_1_17_1_28;

c_d: odnom_1_56;

Klju4ik_64:odnom_1_64;

Klju4ik_56:odnom_1_56;

begin

TextVBinarn (Klju4_64,Klju4ik_64);//ключиз 64 в 64

Perestanovka_PC1(Klju4ik_64,Klju4ik_56);//битовыйключ 64, битовыйключ 56

//Razbivaem na c i d

j:=0;

for i:=1 to 56 do

if i<29 then

c[1,i]:=Klju4ik_56[i]

else

begin

j:=j+1;

d[1,j]:=Klju4ik_56[i];

end;

//Kopiryem с i d vo vremennie c_vrem i d_vrem

for j:=1 to 28 do

begin

c_vrem[1,j]:=c[1,j];

d_vrem[1,j]:=d[1,j];

end;

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

//Delaem sdvigi LSi

for i:=2 to 17 do

begin

k:=0;

for j:=1 to 28 do

if j+matrica_sdvigov[i-1]>28 then

begin

k:=k+1;

c[i,j]:=c_vrem[i-1,k];

d[i,j]:=d_vrem[i-1,k];

c_vrem[i,j]:=c[i,j];

d_vrem[i,j]:=d[i,j];

end

else

begin

c[i,j]:=c_vrem[i-1,j+matrica_sdvigov[i-1]];

d[i,j]:=d_vrem[i-1,j+matrica_sdvigov[i-1]];

c_vrem[i,j]:=c[i,j];

d_vrem[i,j]:=d[i,j];

end;

end;

//Sobiraem с i d vmeste

for i:=2 to 17 do

begin

k:=0;

for j:=1 to 28 do

begin

k:=k+1;

c_d[k]:=c[i,j];

end;

for j:=1 to 28 do

begin

k:=k+1;

c_d[k]:=d[i,j];

end;

Perestanovka_PC2(c_d,i-1);

end;

end;

end.



unit RSA_unit;

interface

function NOD(a{3}:int64;b{fi}:int64;var x:int64;var y:int64):int64;

function Nomer_v_alfavite(a:char):byte;

function Vozvedenie_po_modylju(osnovanie:int64; stepen:int64;modyl:int64):int64;

procedure Wifrovanie_RSA(Ka:int64; P:int64; Q:int64; nazv_ish_f:string;nazv_vih_f:string;var Kb:int64);

procedure RasWifrovanie_RSA(Kb:int64; P:int64; Q:int64; nazv_ish_f:string;nazv_vih_f:string);

var

alf:string;

implementation

function Vozvedenie_v_stepen(osnovanie:int64; stepen:int64):int64;

var

rez:longint;

i:longint;

begin

rez:=1;

for i:= 1 to stepen do

rez := rez*osnovanie;

Vozvedenie_v_stepen:= rez;

end;

function Vozvedenie_po_modylju(osnovanie:int64; stepen:int64;modyl:int64):int64;

var

rez:int64;

i:longint;

t:int64;//osnovanie vo vremennoi stepeni

begin

rez:=1;

t:=osnovanie;

while(stepen<> 0) do

begin

if(stepen mod 2 = 1) then

rez := (rez * t) mod modyl;

t:=(t*t) mod modyl;

stepen:= stepen div 2;

end;

Vozvedenie_po_modylju:=rez;

end;

//Vozvrawaet nomer bykvi v alfavite

function Nomer_v_alfavite(a:char):byte;

var

i:byte;

begin

i:=0;

while( (i<=Length(alf)) and (alf[i+1] <> a) ) do

i:=i+1;

Nomer_v_alfavite:= i;

end;

//Naibolwii obwii delitel

function NOD(a{3}:int64;b{fi}:int64;var x:int64;var y:int64):int64;

var

x1,y1:longint;

x2,y2:longint;

q:longint;

r:longint;

begin

if(b=0)then

begin

NOD:=a;

x:=1;

y:=0;

end

else

begin

x2:=1;

x1:=0;

y2:=0;

y1:=1;

while (b>0)do

begin

q:=a div b;

r:=a-q*b;

x:=x2-q*x1;

y:=y2-q*y1;

a:=b;

b:=r;

x2:=x1;

x1:=x;

y2:=y1;

y1:=y;

end;

NOD:=a;

x:=x2;

y:=y2;

end;

end;

//RSA wifrovanie failov

procedure Wifrovanie_RSA(Ka:int64; P:int64; Q:int64; nazv_ish_f:string;nazv_vih_f:string;var Kb:int64);

var

N:int64;

M,C:int64;

f:int64;//4islo vzaimnoprostih 4isel s N

i:integer;

ish_text,vihod_text:string;

bykva:char;

ish_f,vihod_f:TextFile;

begin

N := Q*P; //Nahodim N

f := (P-1)*(Q-1); //Nahodin 4islo vzaimnoprostih 4isel s N

NOD(Ka,f,Kb,M); //Nahodim Kb

while(Kb < 0) do

Kb:= Kb + f; //Esli Kb otricatelno to yveli4ivaem ego

assignfile(ish_f,nazv_ish_f);

reset(ish_f);

assignfile(vihod_f,nazv_vih_f);

rewrite(vihod_f);

while not EOF(ish_f) do

begin

read(ish_f,bykva);

C := Nomer_v_alfavite(bykva);

C:=Vozvedenie_po_modylju(C,Ka,N);

writeln(vihod_f,C);

end;

closefile(ish_f);

closefile(vihod_f);

end;

//RSA raswifrovanie failov

procedure RasWifrovanie_RSA(Kb:int64; P:int64; Q:int64; nazv_ish_f:string;nazv_vih_f:string);

var

N:int64;

M,C:int64;

f:int64;//4islo vzaimnoprostih 4isel s N

i:integer;

ish_text,vihod_text:string;

bykva:char;

ish_f,vihod_f:TextFile;

begin

N := Q*P; //Nahodim N

f := (P-1)*(Q-1); //Nahodin 4islo vzaimnoprostih 4isel s N

assignfile(ish_f,nazv_ish_f);

reset(ish_f);

assignfile(vihod_f,nazv_vih_f);

rewrite(vihod_f);

while not EOF(ish_f) do

begin

readln(ish_f,C);

C:=Vozvedenie_po_modylju(C,Kb,N);

bykva:= alf[C+1];

write(vihod_f,bykva);

end;

closefile(ish_f);

closefile(vihod_f);

end;

end.


Зашифрованный и разшифрованный текст записывается в директорию с исходным файлом.


Скриншоты работы программы


Выводы

В результате выполнения данной работы были получены практические навыки использования современных как симметрических (DES), так и асимметрических (RSA) алгоритмов шифрования.