- Код формы формирования ключа:
unit Unit_kluch;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm_kluch = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
Label3: TLabel;
StringGrid2: TStringGrid;
Label5: TLabel;
StringGrid3: TStringGrid;
Label4: TLabel;
Button4: TButton;
SaveDialog1: TSaveDialog;
Button5: TButton;
SaveDialog2: TSaveDialog;
Button6: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form_kluch: TForm_kluch;
implementation
uses Unit_zashifr, Unit_rasshifr;
{$R *.dfm}
function NOD(A: integer; B: integer): integer;
begin
while (a <> 0) and (b <> 0) do
if a >= b then
a := a mod b
else
b := b mod a;
NOD := a + b; { один - ноль }
end;
procedure TForm_kluch.Button1Click(Sender: TObject);
var
c,i,j,u,v: integer;
a: array[1..8] of integer;
b: array[1..8] of integer;
p: array[1..8] of integer;
begin
if Edit1.Text='' then
showmessage('Введите целое число >255 для генерации ключа!')
else if strtoint(Edit1.Text)<=255 then
showmessage('Введите целое число >255 для генерации ключа!')
else
begin
u:=strtoint(Edit1.Text);
randomize;
p[1]:=random(7)+1;
c:=1;
for i:=2 to 8 do
while (c<i) do
begin
p[i]:=random(8)+1;
c:=1;
for j:=1 to i-1 do
if (p[i]<>p[j]) then
c:=c+1;
end;
b[1]:=1;
for i:=2 to 8 do
b[i]:=b[i-1]*2;
i:=0;
v:=0;
while (i<>1) and (v<u) do
begin
v:=random(u);
i:=NOD(v,u)
end;
for i:=1 to 8 do
begin
a[i]:=(v*b[p[i]]) mod u
end;
label3.visible:=true;
label5.visible:=true;
stringgrid2.visible:=true;
stringgrid3.visible:=true;
button5.visible:=true;
button6.visible:=true;
stringgrid2.cells[0,0]:='a';
stringgrid3.cells[0,0]:='p';
stringgrid3.cells[0,1]:='v';
stringgrid3.cells[0,2]:='u';
stringgrid3.cells[0,3]:='b';
stringgrid3.cells[1,1]:=inttostr(v);
stringgrid3.cells[1,2]:=inttostr(u);
for i:=1 to 8 do
begin
stringgrid2.cells[i,0]:=inttostr(a[i]);
stringgrid3.cells[i,0]:=inttostr(p[i]);
stringgrid3.cells[i,3]:=inttostr(b[i])
end;
end;
end;
procedure TForm_kluch.Button4Click(Sender: TObject);
begin
Form_kluch.close
end;
procedure TForm_kluch.Button5Click(Sender: TObject);
var i: integer;
begin
if savedialog1.Execute then
begin
memo1.text:='';
for i:=0 to 7 do
memo1.text:=memo1.text+stringgrid2.cells[i+1,0]+';';
memo1.lines.SaveToFile(savedialog1.FileName);
end
end;
procedure TForm_kluch.Button6Click(Sender: TObject);
var i: integer;
begin
if savedialog2.Execute then
begin
memo1.text:='';
for i:=0 to 7 do
begin
memo1.text:=memo1.text+stringgrid3.cells[i+1,0]+';';
memo1.text:=memo1.text+stringgrid3.cells[i+1,3]+';'
end;
memo1.text:=memo1.text+stringgrid3.cells[1,1]+';';
memo1.text:=memo1.text+stringgrid3.cells[1,2]+';';
memo1.lines.SaveToFile(savedialog2.FileName);
end
end;
end.
- Код формы шифрования текста:
unit Unit_rashifr;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm_rasshifr = class(TForm)
Label4: TLabel;
Label5: TLabel;
StringGrid3: TStringGrid;
Label2: TLabel;
Button1: TButton;
Label1: TLabel;
Memo1: TMemo;
Memo2: TMemo;
OpenDialog1: TOpenDialog;
Button2: TButton;
SaveDialog1: TSaveDialog;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form_rasshifr: TForm_rasshifr;
implementation
{$R *.dfm}
function BinToInt(bin: string): integer;
var
i,j,h: integer;
begin
Result := 0;
for i := 1 to 8 do
begin
h:=1;
for j := 1 to 8-i do
h:=h*2;
Result := Result+StrToInt( bin[i])*h;
end;
end;
procedure TForm_rasshifr.Button1Click(Sender: TObject);
var k,l,a,i,j,n,s,u,v,d,v1: integer;
b: array[1..8] of integer;
p: array[1..8] of integer;
r: array[1..8] of integer;
m: array[1..8] of integer;
c: string;
// m: array[1..8] of string;
t: string;
begin
n:=0;
for i:=1 to 8 do
if (StringGrid3.Cells[i,0]='') or (StringGrid3.Cells[i,3]='') then n:=n+1;
if (StringGrid3.Cells[1,1]='') or (StringGrid3.Cells[1,2]='') then n:=n+1;
if n>0 then
showmessage('Введите секретный ключ!')
else
begin
Memo1.text:='';
c:=Memo2.Text;
v:=strtoint(stringgrid3.cells[1,1]);
u:=strtoint(stringgrid3.cells[1,2]);
v1:=2;
while ((v1*v) mod u <>1) do
v1:=v1+1;
for i:=1 to 8 do
begin
p[i]:=strtoint(stringgrid3.cells[i,0]);
b[i]:=strtoint(stringgrid3.cells[i,3]);
a:=a+((v*b[i]) mod u);
end;
l:=length(inttostr(a));
n:=length(c);
k:=Trunc(n/l);
for j:=1 to k do
begin
s:=strtoint(copy(c,(j-1)*l+1,l));
d:=(s*v1) mod u;
for i:=0 to 7 do
begin
r[8-i]:=Trunc(d/b[8-i]);
d:=d mod b[8-i]
end;
for i:=1 to 8 do
m[i]:=r[p[i]];
t:='';
for i:=1 to 8 do
t:=t+inttostr(m[i]);
Memo1.text:=Memo1.text+chr(bintoint(t));
end;
label1.visible:=true;
Memo1.visible:=true;
end;
end;
procedure TForm_rasshifr.Button2Click(Sender: TObject);
begin
if opendialog1.Execute then memo2.Lines.LoadFromFile(opendialog1.FileName);
end;
procedure TForm_rasshifr.Button3Click(Sender: TObject);
begin
if savedialog1.Execute then memo1.Lines.SaveToFile(savedialog1.FileName);
end;
end.
- Код формы дешифрования текста:
unit Unit_rasshifr;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm_rasshifr = class(TForm)
Label4: TLabel;
Label5: TLabel;
StringGrid3: TStringGrid;
Label2: TLabel;
Button1: TButton;
Label1: TLabel;
Memo1: TMemo;
Memo2: TMemo;
OpenDialog1: TOpenDialog;
Button2: TButton;
SaveDialog1: TSaveDialog;
Button3: TButton;
Button4: TButton;
Button5: TButton;
OpenDialog2: TOpenDialog;
Memo3: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form_rasshifr: TForm_rasshifr;
implementation
{$R *.dfm}
function BinToInt(bin: string): integer;
var
i,j,h: integer;
begin
Result := 0;
for i := 1 to 8 do
begin
h:=1;
for j := 1 to 8-i do
h:=h*2;
Result := Result+StrToInt( bin[i])*h;
end;
end;
procedure TForm_rasshifr.Button1Click(Sender: TObject);
var k,l,a,i,j,n,s,u,v,d,v1: integer;
b: array[1..8] of integer;
p: array[1..8] of integer;
r: array[1..8] of integer;
m: array[1..8] of integer;
c: string;
t: string;
begin
n:=0;
for i:=1 to 8 do
if (StringGrid3.Cells[i,0]='') or (StringGrid3.Cells[i,3]='') then n:=n+1;
if (StringGrid3.Cells[1,1]='') or (StringGrid3.Cells[1,2]='') then n:=n+1;
if n>0 then
showmessage('Введите секретный ключ!')
else
begin
Memo1.text:='';
c:=Memo2.Text;
v:=strtoint(stringgrid3.cells[1,1]);
u:=strtoint(stringgrid3.cells[1,2]);
v1:=2;
while ((v1*v) mod u <>1) do
v1:=v1+1;
for i:=1 to 8 do
begin
p[i]:=strtoint(stringgrid3.cells[i,0]);
b[i]:=strtoint(stringgrid3.cells[i,3]);
a:=a+((v*b[i]) mod u);
end;
l:=length(inttostr(a));
n:=length(c);
k:=Trunc(n/l);
for j:=1 to k do
begin
s:=strtoint(copy(c,(j-1)*l+1,l));
d:=(s*v1) mod u;
for i:=0 to 7 do
begin
r[8-i]:=Trunc(d/b[8-i]);
d:=d mod b[8-i]
end;
for i:=1 to 8 do
m[i]:=r[p[i]];
t:='';
for i:=1 to 8 do
t:=t+inttostr(m[i]);
Memo1.text:=Memo1.text+chr(bintoint(t));
end;
label1.visible:=true;
Memo1.visible:=true;
Button3.visible:=true;
end;
end;
procedure TForm_rasshifr.Button2Click(Sender: TObject);
begin
if opendialog1.Execute then memo2.Lines.LoadFromFile(opendialog1.FileName);
end;
procedure TForm_rasshifr.Button3Click(Sender: TObject);
begin
if savedialog1.Execute then memo1.Lines.SaveToFile(savedialog1.FileName);
end;
procedure TForm_rasshifr.Button4Click(Sender: TObject);
begin
Form_rasshifr.close
end;
procedure TForm_rasshifr.Button5Click(Sender: TObject);
var i,j: integer;
kl:string;
kluch: array[1..18] of string;
begin
if opendialog2.Execute then
begin
memo3.Lines.LoadFromFile(opendialog2.FileName);
kl:=memo3.Text;
for i:=1 to 18 do
begin
j:=pos(';',kl);
kluch[i]:=copy(kl,1,j-1);
delete(kl,1,j);
end;
for i:=1 to 8 do
begin
stringgrid3.cells[i,0]:=kluch[i*2-1];
stringgrid3.cells[i,3]:=kluch[i*2]
end;
stringgrid3.cells[1,1]:=kluch[17];
stringgrid3.cells[1,2]:=kluch[18]
end;
end;
end.