Использование современных симметрических (DES) и асимметрических (RSA) алгоритмов шифрования
p align="left"> razmer:=1024*500; end; if Form1.RadioButton6.Checked=true then begin Raswifrovanie_ECB('DES_crypted\Z_1_7_ECB.txt','DES_uncrypted\R_1_7_ECB.txt'); razmer:=1024*1024; end; t2:=Time; vremja:=round((t2 - t1)*24*60*60)+1; Label33.Caption := IntToStr(vremja-1)+ 'сек'; Label34.Caption := FloatToStr(razmer/vremja-1)+'байт/сек'; end; //CBC zawifrovanie failov klju4om na 5 bait procedure TForm1.Button7Click(Sender: TObject); var klu4:string; vektor:string; t1,t2:TDateTime; vremja:integer; razmer:integer; begin klu4:=Edit10.text; while( Length(klu4)<8 ) do klu4:=klu4 + '*'; Formirovanie_16_podklju4ei(klu4); vektor:=Edit1.text; while( Length(vektor)<8 ) do vektor:=vektor+ '0'; t1:=Time; if Form1.RadioButton10.Checked=true then begin Wifrovanie_CBC('20.txt','DES_crypted\Z_20_5_CBС.txt',vektor); razmer:=1024*20; end; if Form1.RadioButton9.Checked=true then begin Wifrovanie_CBC('50.txt','DES_crypted\Z_50_5_CBС.txt',vektor); razmer:=1024*50; end; if Form1.RadioButton8.Checked=true then begin Wifrovanie_CBC('100.txt','DES_crypted\Z_100_5_CBС.txt',vektor); razmer:=1024*100; end; if Form1.RadioButton7.Checked=true then begin Wifrovanie_CBC('500.txt','DES_crypted\Z_500_5_CBС.txt',vektor); razmer:=1024*500; end; if Form1.RadioButton6.Checked=true then begin Wifrovanie_CBC('1.txt','DES_crypted\Z_1_5_CBС.txt',vektor); razmer:=1024*1024; end; t2:=Time; vremja:=round((t2 - t1)*24*60*60)+1; Label34.Caption := IntToStr(vremja-1)+ 'сек'; Label34.Caption := FloatToStr(razmer/vremja-1)+'байт/сек'; end; //CBC raswifrovanie failov klju4om na 5 bait procedure TForm1.Button8Click(Sender: TObject); var klu4:string; vektor:string; t1,t2:TDateTime; vremja:integer; razmer:integer; begin klu4:=Edit10.text; while( Length(klu4)<8 ) do klu4:=klu4 + '*'; Formirovanie_16_podklju4ei(klu4); vektor:=Edit1.text; while( Length(vektor)<8 ) do vektor:=vektor+ '0'; t1:=Time; if Form1.RadioButton10.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_20_5_CBС.txt','DES_uncrypted\R_20_5_CBС.txt',vektor); razmer:=1024*20; end; if Form1.RadioButton9.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_50_5_CBС.txt','DES_uncrypted\R_50_5_CBС.txt',vektor); razmer:=1024*50; end; if Form1.RadioButton8.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_100_5_CBС.txt','DES_uncrypted\R_100_5_CBС.txt',vektor); razmer:=1024*100; end; if Form1.RadioButton7.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_500_5_CBС.txt','DES_uncrypted\R_500_5_CBС.txt',vektor); razmer:=1024*500; end; if Form1.RadioButton6.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_1_5_CBС.txt','DES_uncrypted\R_1_5_CBС.txt',vektor); razmer:=1024*1024; end; t2:=Time; vremja:=round((t2 - t1)*24*60*60)+1; Label33.Caption := IntToStr(vremja-1)+ 'сек'; Label34.Caption := FloatToStr(razmer/vremja-1)+'байт/сек'; end; //CBC zawifrovanie failov klju4om na 6 bait procedure TForm1.Button9Click(Sender: TObject); var klu4:string; vektor:string; t1,t2:TDateTime; vremja:integer; razmer:integer; begin klu4:=Edit11.text; while( Length(klu4)<8 ) do klu4:=klu4 + '*'; Formirovanie_16_podklju4ei(klu4); vektor:=Edit1.text; while( Length(vektor)<8 ) do vektor:=vektor+ '0'; t1:=Time; if Form1.RadioButton10.Checked=true then begin Wifrovanie_CBC('20.txt','DES_crypted\Z_20_6_CBС.txt',vektor); razmer:=1024*20; end; if Form1.RadioButton9.Checked=true then begin Wifrovanie_CBC('50.txt','DES_crypted\Z_50_6_CBС.txt',vektor); razmer:=1024*50; end; if Form1.RadioButton8.Checked=true then begin Wifrovanie_CBC('100.txt','DES_crypted\Z_100_6_CBС.txt',vektor); razmer:=1024*100; end; if Form1.RadioButton7.Checked=true then begin Wifrovanie_CBC('500.txt','DES_crypted\Z_500_6_CBС.txt',vektor); razmer:=1024*500; end; if Form1.RadioButton6.Checked=true then begin Wifrovanie_CBC('1.txt','DES_crypted\Z_1_6_CBС.txt',vektor); razmer:=1024*1024; end; t2:=Time; vremja:=round((t2 - t1)*24*60*60)+1; Label33.Caption := IntToStr(vremja-1)+ 'сек'; Label34.Caption := FloatToStr(razmer/vremja-1)+'байт/сек'; end; //CBC raswifrovanie failov klju4om na 6 bait procedure TForm1.Button10Click(Sender: TObject); var klu4:string; vektor:string; t1,t2:TDateTime; vremja:integer; razmer:integer; begin klu4:=Edit11.text; while( Length(klu4)<8 ) do klu4:=klu4 + '*'; Formirovanie_16_podklju4ei(klu4); vektor:=Edit1.text; while( Length(vektor)<8 ) do vektor:=vektor+ '0'; t1:=Time; if Form1.RadioButton10.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_20_6_CBС.txt','DES_uncrypted\R_20_6_CBС.txt',vektor); razmer:=1024*20; end; if Form1.RadioButton9.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_50_6_CBС.txt','DES_uncrypted\R_50_6_CBС.txt',vektor); razmer:=1024*50; end; if Form1.RadioButton8.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_100_6_CBС.txt','DES_uncrypted\R_100_6_CBС.txt',vektor); razmer:=1024*100; end; if Form1.RadioButton7.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_500_6_CBС.txt','DES_uncrypted\R_500_6_CBС.txt',vektor); razmer:=1024*500; end; if Form1.RadioButton6.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_1_6_CBС.txt','DES_uncrypted\R_1_6_CBС.txt',vektor); razmer:=1024*1024; end; t2:=Time; vremja:=round((t2 - t1)*24*60*60); Label33.Caption := IntToStr(vremja)+ 'сек'; Label34.Caption := FloatToStr(razmer/vremja)+'байт/сек'; end; //CBC zawifrovanie failov klju4om na 7 bait procedure TForm1.Button11Click(Sender: TObject); var klu4:string; vektor:string; t1,t2:TDateTime; vremja:integer; razmer:integer; begin klu4:=Edit12.text; while( Length(klu4)<8 ) do klu4:=klu4 + '*'; Formirovanie_16_podklju4ei(klu4); vektor:=Edit1.text; while( Length(vektor)<8 ) do vektor:=vektor+ '0'; t1:=Time; if Form1.RadioButton10.Checked=true then begin Wifrovanie_CBC('20.txt','DES_crypted\Z_20_7_CBС.txt',vektor); razmer:=1024*20; end; if Form1.RadioButton9.Checked=true then begin Wifrovanie_CBC('50.txt','DES_crypted\Z_50_7_CBС.txt',vektor); razmer:=1024*50; end; if Form1.RadioButton8.Checked=true then begin Wifrovanie_CBC('100.txt','DES_crypted\Z_100_7_CBС.txt',vektor); razmer:=1024*100; end; if Form1.RadioButton7.Checked=true then begin Wifrovanie_CBC('500.txt','DES_crypted\Z_500_7_CBС.txt',vektor); razmer:=1024*500; end; if Form1.RadioButton6.Checked=true then begin Wifrovanie_CBC('1.txt','DES_crypted\Z_1_7_CBС.txt',vektor); razmer:=1024*1024; end; t2:=Time; vremja:=round((t2 - t1)*24*60*60)+1; Label33.Caption := IntToStr(vremja-1)+ 'сек'; Label34.Caption := FloatToStr(razmer/vremja-1)+'байт/сек'; end; //CBC raswifrovanie failov klju4om na 7 bait procedure TForm1.Button12Click(Sender: TObject); var klu4:string; vektor:string; t1,t2:TDateTime; vremja:integer; razmer:integer; begin klu4:=Edit12.text; while( Length(klu4)<8 ) do klu4:=klu4 + '*'; Formirovanie_16_podklju4ei(klu4); vektor:=Edit1.text; while( Length(vektor)<8 ) do vektor:=vektor+ '0'; t1:=Time; if Form1.RadioButton10.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_20_7_CBС.txt','DES_uncrypted\R_20_7_CBС.txt',vektor); razmer:=1024*20; end; if Form1.RadioButton9.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_50_7_CBС.txt','DES_uncrypted\R_50_7_CBС.txt',vektor); razmer:=1024*50; end; if Form1.RadioButton8.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_100_7_CBС.txt','DES_uncrypted\R_100_7_CBС.txt',vektor); razmer:=1024*100; end; if Form1.RadioButton7.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_500_7_CBС.txt','DES_uncrypted\R_500_7_CBС.txt',vektor); razmer:=1024*500; end; if Form1.RadioButton6.Checked=true then begin Raswifrovanie_CBC('DES_crypted\Z_1_7_CBС.txt','DES_uncrypted\R_1_7_CBС.txt',vektor); razmer:=1024*1024; end; t2:=Time; vremja:=round((t2 - t1)*24*60*60); Label33.Caption := IntToStr(vremja)+ 'сек'; Label34.Caption := FloatToStr(razmer/vremja)+'байт/сек'; end; //S4itivaem alfavit dlja RSA procedure TForm1.FormCreate(Sender: TObject); begin alf:=Form1.Label38.Caption; end; //Generacija prostih 4isel s pomowiju reweta Eratosfena procedure TForm1.Button13Click(Sender: TObject); const n = 5000; var a:array[2..n] of boolean; i,j:integer; begin for i:=2 to n do a[i] := false; for i:=2 to n do begin if(a[i] = false) then begin j:=2; while i*j<=n do begin a[i*j]:=true; j:=j+1; end; end; end; ListBox4.Clear; for i:=1 to n do begin if(a[i] = false) then ListBox4.Items.Add(IntToStr(i)); end; end; //Generacija vozmognih variantov klju4a Ka procedure TForm1.Button14Click(Sender: TObject); var i:integer; f:int64;//4islo vzaimno prostih 4isel s N N,P,Q:int64; x,y:int64; k:integer; begin P:=StrToInt(Edit5.Text); Q:=StrToInt(Edit4.Text); N:=Q*P; //Vi4isljaem koli4estvo vzaimno prostih s N 4isel f:=(P-1)*(Q-1); //Vivodim vse vozmognie Ka ListBox3.Clear; k:=0; for i:=(f div 2) to f do begin if(NOD(i,f,x,y)= 1) then begin ListBox3.Items.Add(IntToStr(i)); inc(k); if k=100 then break; end; end; end; //Zawifrovanie failov klju4om Ka procedure TForm1.Button15Click(Sender: TObject); var Ka:int64; Kb:int64; Q,P:int64; i:integer; t1,t2:TDateTime; vremja:integer; razmer:integer; begin P:= StrToInt(Edit5.Text); //S4itivaem P Q:= StrToInt(Edit4.Text); //S4itivaem Q if(ListBox3.ItemIndex <> -1) then begin Ka:= StrToInt(ListBox3.Items[ListBox3.ItemIndex]);//Opredeljaem sly4ainim obrazom Ka Kb := -1; t1:=Time; if Form1.RadioButton10.Checked=true then begin Wifrovanie_RSA(Ka, P, Q, '20RSA.txt','RSA_crypted\Z_20_RSA.txt',Kb); razmer:=1024*20; end; if Form1.RadioButton9.Checked=true then begin Wifrovanie_RSA(Ka, P, Q, '50RSA.txt','RSA_crypted\Z_50_RSA.txt',Kb); razmer:=1024*50; end; if Form1.RadioButton8.Checked=true then begin Wifrovanie_RSA(Ka, P, Q, '100RSA.txt','RSA_crypted\Z_100_RSA.txt',Kb); razmer:=1024*100; end; if Form1.RadioButton7.Checked=true then begin Wifrovanie_RSA(Ka, P, Q, '500RSA.txt','RSA_crypted\Z_500_RSA.txt',Kb); razmer:=1024*500; end; if Form1.RadioButton6.Checked=true then begin Wifrovanie_RSA(Ka, P, Q, '1RSA.txt','RSA_crypted\Z_1_RSA.txt',Kb); razmer:=1024*1024; end; t2:=Time; end; Label13.Caption := IntToStr(Ka);; //Vivodin Ka Label12.Caption := IntToStr(Kb); //Vivodim Kb vremja:=round((t2 - (t1+0.0000001))*24*60*60*1000); Label33.Caption := (IntToStr(vremja-round(0.0000001)))+ 'милисек'; Label34.Caption := FloatToStr((razmer/vremja)-0.0000001)+'байт/милисек'; end; //Raswifrovat faili klju4om Ka1 procedure TForm1.Button16Click(Sender: TObject); var Ka:int64; Kb:int64; Q,P:int64; i:integer; ish_f,vihod_f:TextFile; t1,t2:TDateTime; vremja:integer; razmer:integer; begin P:= StrToInt(Edit5.Text); //S4itivaem P Q:= StrToInt(Edit4.Text); //S4itivaem Q if(ListBox3.ItemIndex <> -1) then begin Ka:= StrToInt(ListBox3.Items[ListBox3.ItemIndex]);//Opredeljaem sly4ainim obrazom Ka Kb:=StrToInt(Label12.Caption); t1:=Time; if Form1.RadioButton10.Checked=true then begin Raswifrovanie_RSA(Kb,P,Q,'RSA_crypted\Z_20_RSA.txt','RSA_uncrypted\R_20_RSA.txt'); razmer:=1024*20; end; if Form1.RadioButton9.Checked=true then begin Raswifrovanie_RSA(Kb,P,Q,'RSA_crypted\Z_50_RSA.txt','RSA_uncrypted\R_50_RSA.txt'); razmer:=1024*20; end; if Form1.RadioButton8.Checked=true then begin Raswifrovanie_RSA(Kb,P,Q,'RSA_crypted\Z_100_RSA.txt','RSA_uncrypted\R_100_RSA.txt'); razmer:=1024*20; end; if Form1.RadioButton7.Checked=true then begin Raswifrovanie_RSA(Kb,P,Q,'RSA_crypted\Z_500_RSA.txt','RSA_uncrypted\R_500_RSA.txt'); razmer:=1024*20; end; if Form1.RadioButton6.Checked=true then begin Raswifrovanie_RSA(Kb,P,Q,'RSA_crypted\Z_1_RSA.txt','RSA_uncrypted\R_1_RSA.txt'); razmer:=1024*20; end; t2:=Time; end; vremja:=round((t2 - t1)*24*60*60)+1; Label33.Caption := IntToStr(vremja-1)+ 'милисек'; Label34.Caption := FloatToStr((razmer/vremja)-1)+'байт/сек'; end; end. Подключаемые модули unit DES_ECB_CBC_unit; interface procedure Wifrovanie_ECB(nazv_ish_f:string;nazv_vih_f:string); procedure Raswifrovanie_ECB(nazv_ish_f:string;nazv_vih_f:string); procedure Wifrovanie_CBC(nazv_ish_f:string;nazv_vih_f:string;vektor:string); procedure Raswifrovanie_CBC(nazv_ish_f:string;nazv_vih_f:string;vektor:string); implementation uses DES_unit; procedure Wifrovanie_ECB(nazv_ish_f:string;nazv_vih_f:string); var i:integer; ish_text,vihod_text:string; bykva:byte; ish_f,vihod_f:file of byte; begin 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); ish_text:=ish_text+chr(bykva); if Length(ish_text) = 8 then begin vihod_text:= Kodirovat(ish_text); for i:=1 to 8 do begin bykva:=byte(vihod_text[i]); write(vihod_f,bykva); end; ish_text := ''; end; end; if(Length(ish_text) <> 0) then begin while length(ish_text)<8 do ish_text:=ish_text+'0'; vihod_text := Kodirovat(ish_text); for i:=1 to 8 do begin bykva:=byte(vihod_text[i]); write(vihod_f,bykva); end; end; end; procedure Raswifrovanie_ECB(nazv_ish_f:string;nazv_vih_f:string); var i:integer; ish_text,vihod_text:string; bykva:byte; ish_f,vihod_f:file of byte; begin 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); ish_text:=ish_text+chr(bykva); if Length(ish_text) = 8 then begin vihod_text:= Raskodirovat(ish_text); for i:=1 to 8 do begin bykva:=byte(vihod_text[i]); write(vihod_f,bykva); end; ish_text := ''; end; end; if(Length(ish_text) <> 0) then begin while length(ish_text)<8 do ish_text:=ish_text+'0'; vihod_text := Raskodirovat(ish_text); for i:=1 to 8 do begin bykva:=byte(vihod_text[i]); write(vihod_f,bykva); end; end; end; procedure Wifrovanie_CBC(nazv_ish_f:string;nazv_vih_f:string;vektor:string); var i:integer; ish_text,vihod_text:string; bykva:byte; ish_f,vihod_f:file of byte; begin 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); ish_text:=ish_text+chr(bykva); if Length(ish_text) = 8 then begin for i:=1 to 8 do ish_text[i]:=chr(ord(ish_text[i]) xor ord(vektor[i])); vihod_text:= Kodirovat(ish_text); vektor := vihod_text; for i:=1 to 8 do begin bykva:=byte(vihod_text[i]); write(vihod_f,bykva); end; ish_text := ''; end; end; if(Length(ish_text) <> 0) then begin while length(ish_text)<8 do ish_text:=ish_text+'0'; for i:=1 to 8 do ish_text[i] := chr(ord(ish_text[i]) xor ord(vektor[i])); vihod_text := Kodirovat(ish_text); for i:=1 to 8 do begin bykva:=byte(vihod_text[i]); write(vihod_f,bykva); end; end; end; procedure Raswifrovanie_CBC(nazv_ish_f:string;nazv_vih_f:string;vektor:string); var i:integer; ish_text,vihod_text,tmp:string; bykva:byte; ish_f,vihod_f:file of byte; begin 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); ish_text:=ish_text+chr(bykva); if Length(ish_text) = 8 then begin tmp := ish_text; vihod_text:= Raskodirovat(ish_text); for i:=1 to 8 do vihod_text[i] := chr(ord(vihod_text[i]) xor ord(vektor[i])); vektor := tmp; for i:=1 to 8 do begin bykva:=byte(vihod_text[i]); write(vihod_f,bykva); end; ish_text := ''; end; end; if(Length(ish_text) <> 0) then begin while length(ish_text)<8 do ish_text:=ish_text+'0'; tmp := ish_text; vihod_text:= Raskodirovat(ish_text); for i:=1 to 8 do vihod_text[i] := chr(ord(vihod_text[i]) xor ord(vektor[i])); vektor := tmp; for i:=1 to 8 do begin bykva:=byte(vihod_text[i]); write(vihod_f,bykva); end; end; end; end. unit DES_unit; interface type odnom_1_64=array[1..64] of byte; type odnom_1_56=array[1..56] of byte; type odnom_1_48=array[1..48] of byte; type odnom_1_32=array[1..32] of byte; type dvym_1_17_1_32=array[1..17,1..32] of byte; type dvym_1_17_1_28=array[1..17,1..28] of byte; type dvym_1_16_1_48=array[1..16,1..48] of byte;//16 подключей procedure Perestanovka_IP (var Klju4_64_posle_IP: odnom_1_64); procedure Perestanovka_E (n:integer; Tekst_32_do_E: dvym_1_17_1_32; var Tekst_48_posle_E: odnom_1_48); procedure Perestanovka_P(var Tekst_32_posle_P:odnom_1_32); procedure S_Blok (e: odnom_1_48; var p: odnom_1_32); procedure Perestanovka_IP_1 (var Tekst_64_posle_IP1: odnom_1_64); procedure TextVBinarn (Tekst_bykv: string; var Tekst_64_binar: odnom_1_64); procedure BinarnVText(Tekst_64_binar: odnom_1_64;var Tekst_bykv: string); function Kodirovat(Ish_tekst_bykv:string):string; function Raskodirovat(Wifr_tekst_bykv:string):string; implementation uses podkluchi_unit; var matrica_IP:array[1..64] of byte=(58, 50, 42, 34, 26, 18, 10, 02, 60, 52, 44, 36, 28, 20, 12, 04, 62, 54, 46, 38, 30, 22, 14, 06, 64, 56, 48, 40, 32, 24, 16, 08, 57, 49, 41, 33, 25, 17, 09, 01, 59, 51, 43, 35, 27, 19, 11, 03, 61, 53, 45, 37, 29, 21, 13, 05, 63, 55, 47, 39, 31, 23, 15, 07); matrica_E:array[1..48] of byte=(32, 01, 02, 03, 04, 05, 04, 05, 06, 07, 08, 09, 08, 09, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 01); matrica_P:array[1..32] of byte=(16, 07, 20, 21, 29, 12, 28, 17, 01, 15, 23, 26, 05, 18, 31, 10, 02, 08, 24, 14, 32, 27, 03, 09, 19, 13, 30, 06, 22, 11, 04, 25); matrica_IP_1:array[1..64] of byte=(40, 08, 48, 16, 56, 24, 64, 32, 39, 07, 47, 15, 55, 23, 63, 31, 38, 06, 46, 14, 54, 22, 62, 30, 37, 05, 45, 13, 53, 21, 61, 29, 36, 04, 44, 12, 52, 20, 60, 28, 35, 03, 43, 11, 51, 19, 59, 27, 34, 02, 42, 10, 50, 18, 58, 26, 33, 01, 41, 09, 49, 17, 57, 25); procedure Perestanovka_IP (var Klju4_64_posle_IP: odnom_1_64); var Vrem_klju4_64: odnom_1_64; i:integer; begin //Kopirivanie binarnogo predstavlenia for i:=1 to 64 do Vrem_klju4_64[i]:=Klju4_64_posle_IP[i]; //IP perestanovka for i:=1 to 64 do Klju4_64_posle_IP[i]:=Vrem_klju4_64[matrica_IP[i]]; end; procedure Perestanovka_E (n:integer; Tekst_32_do_E: dvym_1_17_1_32; var Tekst_48_posle_E: odnom_1_48); var i:integer; begin for i:=1 to 48 do Tekst_48_posle_E[i]:=Tekst_32_do_E[n,matrica_E[i]]; end; procedure Perestanovka_P(var Tekst_32_posle_P:odnom_1_32); var i:integer; Vrem_tekst_32:odnom_1_32; begin //Kopiy binarnogo predstavlenia for i:=1 to 32 do Vrem_tekst_32[i]:=Tekst_32_posle_P[i]; //P perestanovka bit for i:=1 to 32 do Tekst_32_posle_P[i]:=Vrem_tekst_32[matrica_P[i]]; end; procedure S_Blok (e: odnom_1_48; var p: odnom_1_32); var q,i,j:integer;//s4et4iki k:integer;//4islo na perese4enii neobhodimoi stro4ki i stolbika h:integer; symma_2_krainih,symma_4_vnytrennih:integer; s_blok:array[0..3,0..15] of byte; B_dvym_8_6:array[1..8,1..6] of byte; f:textfile; begin //Razbitie na 8 4astei po 6 bit q:=0; for i:=1 to 8 do for j:=1 to 6 do begin q:=q+1; B_dvym_8_6[i,j]:=e[q]; end; //S4itivaem S blok AssignFile(f,'S.txt'); Reset(f); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! for q:=1 to 8 do begin symma_2_krainih:=B_dvym_8_6[q,6]+B_dvym_8_6[q,1]*2; symma_4_vnytrennih:=B_dvym_8_6[q,2]+2*B_dvym_8_6[q,3]+4*B_dvym_8_6[q,4]+8*B_dvym_8_6[q,5]; for i:=0 to 3 do for j:=0 to 15 do read(f,s_blok[i,j]); k:=s_blok[symma_2_krainih,symma_4_vnytrennih]; h:=4*q; for j:=1 to 4 do begin p[h]:=k mod 2; k:=k div 2; h:=h-1; end end; closefile(f); end; procedure Perestanovka_IP_1 (var Tekst_64_posle_IP1: odnom_1_64); var Vrem_tekst_64: odnom_1_64; i:integer; begin //Kopiy binarnogo predstavlenia for i:=1 to 64 do Vrem_tekst_64[i]:=Tekst_64_posle_IP1[i]; //IP perestanovka bit for i:=1 to 64 do Tekst_64_posle_IP1[i]:=Vrem_tekst_64[matrica_IP_1[i]]; end; //Perevodit text v binarnoe predstavlenie procedure TextVBinarn (Tekst_bykv: string; var Tekst_64_binar: odnom_1_64); var j,h,a,i:integer; alfav_ASCII:array[1..8] of integer; begin //Perevod simvolov v ASCII-kodi for i:=1 to 8 do alfav_ASCII[i]:=ord(Tekst_bykv[i]);//vozvrawaet № simvola v ASC //Perevod chisel v binarniy kod for j:=1 to 8 do begin h:=8*j; a:=alfav_AscII[j]; for i:=1 to 8 do begin Tekst_64_binar[h]:=a mod 2; a:=a div 2;//sdvig vpravo na 1 h:=h-1; end end; end; //+++++++++++++++++++++++++++++++++++++++++++++++++++++++ //Perevodit binarnoe predstavlenie v tekst procedure BinarnVText(Tekst_64_binar: odnom_1_64;var Tekst_bykv: string); var Vrem_tekst:string; a,i,j:integer; begin Vrem_tekst:=''; for i:=1 to 8 do begin a:=0; for j:=(i-1)*8+1 to i*8 do a:= a*2+Tekst_64_binar[j]; Vrem_tekst:=concat(Vrem_tekst,char(a));//prilirljaet k s a end; Tekst_bykv:=Vrem_tekst; end; function Kodirovat(Ish_tekst_bykv:string):string; var Ish_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 (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) алгоритмов шифрования.
Страницы: 1, 2
|