. , , ,

,,,

(DES) (RSA) — ,

(DES), (RSA)



1.         DES 4 . 56 .

2.         1 , 10 , 20 5,6,7 . ,

3.         DES OFB CFB

4.         RSA. 3 / . 20 , 50 , 100 , 500 , 1 .

5.         3 . / / .

6.         , .

 

.

1.         , ( 1, )

2.         =( ) mod 23

3.         [0.._-1] ( ->0, ->1, ->2 .)


1.

/

A

B

15

2000

5000

, (@)

RSA

RSA 1978 . : . (Rivest), . (Shamir) . (Adleman). . RSA , , .

.

RSA A, B,

 

ZN={0,1,2,...,N-1} (1)

N - :

 

N = P*Q. (2)

Q - . Q .

ZN N N.

A , :

(3)

, (4)

- , 1 N, N.

(4) , A .

, , K B, ,

 

KB * A = 1 ( mod ( ) (5)

, (P,Q) . , K B N .

A , K B - .

( A, ) :

(6)

, .. , A N, N > 2512.

, .. , , ( K B, ) :

 (7)

:

 

DB ( ()) = . (8)

(8) (6) (7), :

(9)

, , (,N)=1,

(10)

(9) (10),

, , .

KB (5).

,

K B, ,

, , , : K B (P,Q), N. , N .

N. N Q, " " - {,Q, A },

K B.

, , N ( , Q 100 ).

RSA

, , RSA. , - . , RSA , .. . .

1. Q.

2. N=*Q.

3. (8):

 

4. A :

 

5. kB,

 

6. (N, A) .

, .

7. ,

 

i=0,1,2,...,N-1 .

 

8. , ,

 

9. C1, 2, 3,...,Ci, ... .

10. C1, 2, 3,...,Ci, ..., kB,

.

Mi, . RSA , , A B.

 

DES

DES 64- 56- . DES ( , . , ).

64- , , , (.1).

.1. DES

, , , , . , . DES .2.


.2. DES

8- T, IP (.1) : 58 T 1, 50 - 2 .., : T(0) = IP(T).

T(0) 32 : L(0) - , R(0) - .

1: IP

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

, 16 . i- :

L(i) = R(i-1)

R(i) = L(i-1) xor f(R(i-1), K(i)) ,

xor - .

f . - 32- R(i-1), (i-1)- , 48- K(i), 64- K. (i) .

16- R(16) L(16) ( ), 64- R(16)L(16).

IP-1 (.2).

2: IP-1

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

IP-1 IP : 1- IP-1 40, 40- IP 1, 2- IP-1 8, 8- IP 2 ..

. . , IP-1, R(16)L(16) , , .

:


R(i-1) = L(i), i = 1, 2, ..., 16;

L(i-1) = R(i) xor f(L(i), K(i)), i = 1, 2, ..., 16 .

16- L(0) R(0), 64- L(0)R(0).

IP. - 64- .

f(R(i-1),K(i)). . 3.

.3. f(R(i-1), K(i))

f -:

           - 32- 48-,

           S1, S2, ... , S8 - 6- 4-,

           - 32- .

.3. 3 (R(i-1)) - 32, 1 2, - 31, 32 1.

3: E

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

(R(i-1)) 48- , 2 ( xor) 48- (i). 48- , 6- B(1)B(2)B(3)B(4)B(5)B(6)B(7)B(8). :

E(R(i-1)) xor K(i) = B(1)B(2)...B(8) .

S1, S2, ... , S8 .4.


4

S1, S2, ..., S8

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

 

0

1

2

3

14 4 13 1 2 15 11 8 3 10 6 12 5 9 0 7

0 15 7 4 14 2 13 1 10 6 12 11 9 5 3 8

4 1 14 8 13 6 2 11 15 12 9 7 3 10 5 0

15 12 8 2 4 9 1 7 5 11 3 14 10 0 6 13

S1

0

1

2

3

15 1 8 14 6 11 3 4 9 7 2 13 12 0 5 10

3 13 4 7 15 2 8 14 12 0 1 10 6 9 11 5

0 14 7 11 10 4 13 1 5 8 12 6 9 3 2 15

13 8 10 1 3 15 4 2 11 6 7 12 0 5 14 9

S2

0

1

2

3

10 0 9 14 6 3 15 5 1 13 12 7 11 4 2 8

13 7 0 9 3 4 6 10 2 8 5 14 12 11 15 1

13 6 4 9 8 15 3 0 11 1 2 12 5 10 14 7

1 10 13 0 6 9 8 7 4 15 14 3 11 5 2 12

S3

0

1

2

3

7 13 14 3 0 6 9 10 1 2 8 5 11 12 4 15

13 8 11 5 6 15 0 3 4 7 2 12 1 10 14 9

10 6 9 0 12 11 7 13 15 1 3 14 5 2 8 4

3 15 0 6 10 1 13 8 9 4 5 11 12 7 2 14

S4

0

1

2

3

2 12 4 1 7 10 11 6 8 5 3 15 13 0 14 9

14 11 2 12 4 7 13 1 5 0 15 10 3 9 8 6

4 2 1 11 10 13 7 8 15 9 12 5 6 3 0 14

11 8 12 7 1 14 2 13 6 15 0 9 10 4 5 3

S5

0

1

2

3

12 1 10 15 9 2 6 8 0 13 3 4 14 7 5 11

10 15 4 2 7 12 9 5 6 1 13 14 0 11 3 8

9 14 15 5 2 8 12 3 7 0 4 10 1 13 11 6

4 3 2 12 9 5 15 10 11 14 1 7 6 0 8 13

S6

0

1

2

3

4 11 2 14 15 0 8 13 3 12 9 7 5 10 6 1

13 0 11 7 4 9 1 10 14 3 5 12 2 15 8 6

1 4 11 13 12 3 7 14 10 15 6 8 0 5 9 2

6 11 13 8 1 4 10 7 9 5 0 15 14 2 3 12

S7

0

1

2

3

13 2 8 4 6 15 11 1 10 9 3 14 5 0 12 7

1 15 13 8 10 3 7 4 12 5 6 11 0 14 9 2

7 11 4 1 9 12 14 2 0 6 10 13 15 3 5 8

2 1 14 7 4 10 8 13 15 12 9 0 3 5 6 11

S8

.4. . - Sj 6- B(j) = b1b2b3b4b5b6, b1b6 , b2b3b4b5 - . Sj(B(j)) 4- , .

, (1)=011011. S1((1)) 1 13. 13 1 5. , S1(011011)=0101.

6- B(1), B(2), ..., B(8), 32- S1(B(1))S2(B(2))S3(B(3))...S8(B(8)).

, . P (.5). , 16 1, 7 - 2 ..

5: P

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

,

f(R(i-1), K(i)) = P(S1(B(1)),...S8(B(8)))

, 48- (i), i=1...16. K(i), K. K 64- , 8,16,24,32,40,48,56,64.

G (.6).

6

G

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

G(K) 28- C(0)  D(0), C(0) 57, 49, ..., 44, 36 K, D(0) 63, 55, ..., 12, 4 K. C(0) D(0) C(i) D(i), i=1...16. , .7.


7

()

 

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

 

1

1

2

2

2

2

2

2

1

2

2

2

2

2

2

1

"" H (.8).

8:

H

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

K(i) 14, 17, ..., 29, 32 C(i)D(i). :

K(i) = H(C(i)D(i))

- .4.

.4. - K(i)

, ,
K(16), - K(15) .


RSA

0123456789@

@@@


unit main;

interface

uses

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

Dialogs, StdCtrls, ComCtrls,DES_unit,Podkluchi_unit,DES_ECB_CBC_unit,RSA_unit;

type

TForm1 = class(TForm)

Button17: TButton;

Label28: TLabel;

Label29: TLabel;

Label30: TLabel;

Label31: TLabel;

Label32: TLabel;

Label33: TLabel;

Label34: TLabel;

Edit10: TEdit;

Edit11: TEdit;

Edit12: TEdit;

Button18: TButton;

Button19: TButton;

Button20: TButton;

Button21: TButton;

Button22: TButton;

Edit1: TEdit;

Button1: TButton;

Button2: TButton;

Button3: TButton;

Button4: TButton;

Button5: TButton;

Button6: TButton;

Label5: TLabel;

Button7: TButton;

Button8: TButton;

RadioButton6: TRadioButton;

RadioButton7: TRadioButton;

RadioButton8: TRadioButton;

RadioButton9: TRadioButton;

RadioButton10: TRadioButton;

Button9: TButton;

Edit4: TEdit;

Edit5: TEdit;

ListBox3: TListBox;

Button10: TButton;

ListBox4: TListBox;

Label12: TLabel;

Label13: TLabel;

Label14: TLabel;

Label15: TLabel;

Label36: TLabel;

Label37: TLabel;

Label38: TLabel;

GroupBox1: TGroupBox;

GroupBox2: TGroupBox;

Label1: TLabel;

Label2: TLabel;

Label3: TLabel;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure Button4Click(Sender: TObject);

procedure Button5Click(Sender: TObject);

procedure Button6Click(Sender: TObject);

procedure Button7Click(Sender: TObject);

procedure Button8Click(Sender: TObject);

procedure Button9Click(Sender: TObject);

procedure Button10Click(Sender: TObject);

procedure Button11Click(Sender: TObject);

procedure Button12Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

procedure Button13Click(Sender: TObject);

procedure Button14Click(Sender: TObject);

procedure Button15Click(Sender: TObject);

procedure Button16Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

//Zawifrovanie failov klju4om na 5 bait

procedure TForm1.Button1Click(Sender: TObject);

var

klu4:string;

t1,t2:TDateTime;

vremja:integer;

razmer:integer;

begin

klu4:=Edit10.text;

while( Length(klu4)<8 ) do

klu4:=klu4+'0';

Formirovanie_16_podklju4ei(klu4);

razmer:=0;

t1:=Time;

if Form1.RadioButton10.Checked=true then

begin

Wifrovanie_ECB('20.txt','DES_crypted\Z_20_5_ECB.txt');

razmer:=1024*20;

end;

if Form1.RadioButton9.Checked=true then

begin

Wifrovanie_ECB('50.txt','DES_crypted\Z_50_5_ECB.txt');

razmer:=1024*50;

end;

if Form1.RadioButton8.Checked=true then

begin

Wifrovanie_ECB('100.txt','DES_crypted\Z_100_5_ECB.txt');

razmer:=1024*100;

end;

if Form1.RadioButton7.Checked=true then

begin

Wifrovanie_ECB('500.txt','DES_crypted\Z_500_5_ECB.txt');

razmer:=1024*500;

end;

if Form1.RadioButton6.Checked=true then

begin

Wifrovanie_ECB('1.txt','DES_crypted\Z_1_5_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;

//Raswifrovanie failov klju4om na 5 bait

procedure TForm1.Button2Click(Sender: TObject);

var

klu4:string;

t1,t2:TDateTime;

vremja:integer;

razmer:integer;

begin

klu4:=Edit10.text;

razmer:=0;

while( Length(klu4)<8 ) do

klu4:=klu4+'0';

Formirovanie_16_podklju4ei(klu4);

t1:=Time;

if Form1.RadioButton10.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_20_5_ECB.txt','DES_uncrypted\R_20_5_ECB.txt');

razmer:=1024*20;

end;

if Form1.RadioButton9.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_50_5_ECB.txt','DES_uncrypted\R_50_5_ECB.txt');

razmer:=1024*50;

end;

if Form1.RadioButton8.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_100_5_ECB.txt','DES_uncrypted\R_100_5_ECB.txt');

razmer:=1024*100;

end;

if Form1.RadioButton7.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_500_5_ECB.txt','DES_uncrypted\R_500_5_ECB.txt');

razmer:=1024*500;

end;

if Form1.RadioButton6.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_1_5_ECB.txt','DES_uncrypted\R_1_5_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;

//Zawifrovanie failov klju4om na 6 bait

procedure TForm1.Button3Click(Sender: TObject);

var

klu4:string;

t1,t2:TDateTime;

vremja:integer;

razmer:integer;

begin

klu4:=Edit11.text;

while( Length(klu4)<8 ) do

klu4:=klu4+'0';

Formirovanie_16_podklju4ei(klu4);

t1:=Time;

if Form1.RadioButton10.Checked=true then

begin

Wifrovanie_ECB('20.txt','DES_crypted\Z_20_6_ECB.txt');

razmer:=1024*20;

end;

if Form1.RadioButton9.Checked=true then

begin

Wifrovanie_ECB('50.txt','DES_crypted\Z_50_6_ECB.txt');

razmer:=1024*50;

end;

if Form1.RadioButton8.Checked=true then

begin

Wifrovanie_ECB('100.txt','DES_crypted\Z_100_6_ECB.txt');

razmer:=1024*100;

end;

if Form1.RadioButton7.Checked=true then

begin

Wifrovanie_ECB('500.txt','DES_crypted\Z_500_6_ECB.txt');

razmer:=1024*500;

end;

if Form1.RadioButton6.Checked=true then

begin

Wifrovanie_ECB('1.txt','DES_crypted\Z_1_6_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;

//Raswifrovanie failov klju4om na 6 bait

procedure TForm1.Button4Click(Sender: TObject);

var

klu4:string;

t1,t2:TDateTime;

vremja:integer;

razmer:integer;

begin

klu4:=Edit11.text;

while( Length(klu4)<8 ) do

klu4:=klu4+'0';

Formirovanie_16_podklju4ei(klu4);

t1:=Time;

if Form1.RadioButton10.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_20_6_ECB.txt','DES_uncrypted\R_20_6_ECB.txt');

razmer:=1024*20;

end;

if Form1.RadioButton9.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_50_6_ECB.txt','DES_uncrypted\R_50_6_ECB.txt');

razmer:=1024*50;

end;

if Form1.RadioButton8.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_100_6_ECB.txt','DES_uncrypted\R_100_6_ECB.txt');

razmer:=1024*100;

end;

if Form1.RadioButton7.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_500_6_ECB.txt','DES_uncrypted\R_500_6_ECB.txt');

razmer:=1024*500;

end;

if Form1.RadioButton6.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_1_6_ECB.txt','DES_uncrypted\R_1_6_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;

//Zawifrovanie failov klju4om na 7 bait

procedure TForm1.Button5Click(Sender: TObject);

var

klu4:string;

t1,t2:TDateTime;

vremja:integer;

razmer:integer;

begin

klu4:=Edit12.text;

while( Length(klu4)<8 ) do

klu4:=klu4+'0';

Formirovanie_16_podklju4ei(klu4);

t1:=Time;

if Form1.RadioButton10.Checked=true then

begin

Wifrovanie_ECB('20.txt','DES_crypted\Z_20_7_ECB.txt');

razmer:=1024*20;

end;

if Form1.RadioButton9.Checked=true then

begin

Wifrovanie_ECB('50.txt','DES_crypted\Z_50_7_ECB.txt');

razmer:=1024*50;

end;

if Form1.RadioButton8.Checked=true then

begin

Wifrovanie_ECB('100.txt','DES_crypted\Z_100_7_ECB.txt');

razmer:=1024*100;

end;

if Form1.RadioButton7.Checked=true then

begin

Wifrovanie_ECB('500.txt','DES_crypted\Z_500_7_ECB.txt');

razmer:=1024*500;

end;

if Form1.RadioButton6.Checked=true then

begin

Wifrovanie_ECB('1.txt','DES_crypted\Z_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;

//Raswifrovanie failov klju4om na 7 bait

procedure TForm1.Button6Click(Sender: TObject);

var

klu4:string;

t1,t2:TDateTime;

vremja:integer;

razmer:integer;

begin

klu4:=Edit12.text;

while( Length(klu4)<8 ) do

klu4:=klu4+'0';

Formirovanie_16_podklju4ei(klu4);

t1:=Time;

if Form1.RadioButton10.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_20_7_ECB.txt','DES_uncrypted\R_20_7_ECB.txt');

razmer:=1024*20;

end;

if Form1.RadioButton9.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_50_7_ECB.txt','DES_uncrypted\R_50_7_ECB.txt');

razmer:=1024*50;

end;

if Form1.RadioButton8.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_100_7_ECB.txt','DES_uncrypted\R_100_7_ECB.txt');

razmer:=1024*100;

end;

if Form1.RadioButton7.Checked=true then

begin

Raswifrovanie_ECB('DES_crypted\Z_500_7_ECB.txt','DES_uncrypted\R_500_7_ECB.txt');

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) .

(DES), (RSA)

 

 

 

! , , , .
. , :