-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unit3.pas
164 lines (146 loc) · 3.09 KB
/
Unit3.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
lbl1: TLabel;
lbl2: TLabel;
lbl4: TLabel;
btn1: TButton;
edt1: TEdit;
edt2: TEdit;
btn2: TButton;
mmo1: TMemo;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure edt1KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
flagText: Boolean;
FileNameText, OutputFileName: String;
implementation
{$R *.dfm}
procedure TForm3.btn1Click(Sender: TObject);
var
openDialog : TOpenDialog;
begin
flagText:= True;
openDialog := TOpenDialog.Create(openDialog);
openDialog.Title:= 'Âûáåðèòå ôàéë äëÿ îòêðûòèÿ';
openDialog.InitialDir := GetCurrentDir;
openDialog.Options := [ofFileMustExist];
openDialog.Filter := 'Text file|*.*';
openDialog.FilterIndex := 1;
if openDialog.Execute then
begin
FileNameText:= openDialog.FileName;
end
else
begin
Application.MessageBox('Âûáîð ôàéëà äëÿ îòêðûòèÿ îñòàíîâëåí!', 'Ïðåäóïðåæäåíèå!');
flagText:=False;
end;
openDialog.Free;
lbl1.Caption := FileNameText;
end;
function Nod(a, b: integer): integer;
begin
while a * b <> 0 do
if a > b then
a := a mod b
else
b := b mod a;
Nod := a + b;
end;
function evklid(a, b: integer): integer;
var
d0, d1, x0, x1, y0, y1, q, d2, x2, y2: integer;
begin
d0:=a; d1:=b;
x0:=1; x1:=0;
y0:=0; y1:=1;
while d1>1 do
begin
q:=d0 div d1;
d2:=d0 mod d1;
x2:=x0-q*x1;
y2:=y0-q*y1;
d0:=d1; d1:=d2;
x0:=x1; x1:=x2;
y0:=y1; y1:=y2;
end;
if y1 < 0 then
evklid := y1 + a
else
evklid := y1;
end;
function fast_exp(a: longword; z, n: longword): longword;
var
x: longword;
begin
x := 1;
while z <> 0 do
begin
while (z mod 2) = 0 do
begin
z := z div 2;
a := (a * a) mod n;
end;
z := z - 1;
x := (x * a) mod n;
end;
fast_exp := x;
end;
function SwapW(i: Word): Word;
begin
asm
mov ax, [i]
xchg al,ah
mov [i], ax
end;
SwapW := i;
end;
procedure TForm3.btn2Click(Sender: TObject);
var
n, i, d, countText: integer;
arr : array[1..100000] of word;
File_text: file of word;
begin
if flagText then
begin
countText := 0;
AssignFile(File_text, FileNameText);
reset(File_text);
while not Eof(File_text) do
begin
inc(countText);
read(File_text, arr[countText]);
arr[countText] := SwapW(arr[countText]);
end;
CloseFile(File_text);
d := StrToInt(edt2.Text);
n := StrToInt(edt1.Text);
OutputFileName := FileNameText + '.decryptionRSA';
AssignFile(output, OutputFileName);
Rewrite(output);
for i:= 1 to countText do
begin
write(chr(fast_exp(arr[i], d, n)));
//mmo1.Text := mmo1.Text + IntToStr(fast_exp(arr[i], d, n) mod 256) + ' ';
end;
CloseFile(output);
end;
end;
procedure TForm3.edt1KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9', #8]) then
Key := #0;
end;
end.