Смекни!
smekni.com

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

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&bsol;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&bsol;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&bsol;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&bsol;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&bsol;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&bsol;Z_20_RSA.txt','RSA_uncrypted&bsol;R_20_RSA.txt');

razmer:=1024*20;

end;

if Form1.RadioButton9.Checked=true then

begin

Raswifrovanie_RSA(Kb,P,Q,'RSA_crypted&bsol;Z_50_RSA.txt','RSA_uncrypted&bsol;R_50_RSA.txt');

razmer:=1024*20;

end;

if Form1.RadioButton8.Checked=true then

begin

Raswifrovanie_RSA(Kb,P,Q,'RSA_crypted&bsol;Z_100_RSA.txt','RSA_uncrypted&bsol;R_100_RSA.txt');

razmer:=1024*20;

end;

if Form1.RadioButton7.Checked=true then

begin

Raswifrovanie_RSA(Kb,P,Q,'RSA_crypted&bsol;Z_500_RSA.txt','RSA_uncrypted&bsol;R_500_RSA.txt');

razmer:=1024*20;

end;

if Form1.RadioButton6.Checked=true then

begin

Raswifrovanie_RSA(Kb,P,Q,'RSA_crypted&bsol;Z_1_RSA.txt','RSA_uncrypted&bsol;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;