Пример 1:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
key = '134567';
crname = 'crypt.txt';
dcrname = 'decrypt.txt';
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Shifr: TButton;
Label1: TLabel;
Rasshif: TButton;
Label2: TLabel;
Label3: TLabel;
Memo1: TMemo;
Label4: TLabel;
Label5: TLabel;
procedure ShifrClick(Sender: TObject);
procedure RasshifClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure Crypt_Ko (FileName : TFileName);
var
f, f1 : File of Char;
j : Integer;
i, i2 : Char;
begin
AssignFile (f, FileName);
AssignFile (f1, crname);
Reset (f);
Rewrite (f1);
while not Eof(f) do
begin
Read (f, i);
j := Ord(i) xor StrToInt(key);
i2 := Chr(j);
Write (f1, i2);
end;
CloseFile (f);
CloseFile (f1);
Form1.Label1.Caption := 'Файл обработан';
end;
procedure Decrypt_Ko (FileName : TFileName);
var
f, f1 : File of Char;
i, i2 : Char;
begin
AssignFile (f, FileName);
AssignFile (f1, dcrname);
try
Reset (f);
Rewrite (f1);
finally
While not Eof(f) do
begin
Read (f, i);
i2 := Chr(Ord(i) xor StrToInt(key));
Write (f1, i2);
end;
CloseFile (f);
CloseFile (f1);
end;
end;
procedure TForm1.ShifrClick(Sender: TObject);
begin
OpenDialog1.Execute;
Crypt_Ko (OpenDialog1.FileName);
end;
procedure TForm1.RasshifClick(Sender: TObject);
begin
OpenDialog1.Execute;
DeCrypt_Ko (OpenDialog1.FileName);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
f: File of Char;
k,k1,j,kol,kol1: Integer;
i : Char;
Filename: TFilename;
s1:array[0..20] of string;
s:WideString;
col:array[0..19] of integer;
begin
Filename:='Unit1.pas';
AssignFile (f, FileName);
k:=0;
try
Reset(f);
finally
While not Eof(f) do
begin
Read (f, i);
k1 := Ord(i);
k:=k+k1;
end;
CloseFile (f);
Label2.Caption:=IntToStr(k);
end;
begin
s1[0]:='*';
s1[1]:='/';
s1[2]:='+'; s1[3]:='-';s1[4]:='div';
s1[5]:='mod'; s1[6]:='not';s1[7]:='and';s1[8]:='or';
s1[9]:='xor';s1[10]:='>';s1[11]:='<';
s1[12]:=':=';s1[13]:='=';s1[14]:='<>';s1[15]:='>=';s1[16]:='<=';
s1[17]:='if';s1[18]:='case';
Memo1.Lines.LoadFromFile('Unit1.pas');
s:=Memo1.Text;
kol1:=0;
for j:=0 to 18 do
begin
kol:=0;
while pos(s1[j],s)<>0 do
begin
{if ((s[pos(s1,s)-1] in punct) and (s[pos(s1,s)+length(s1)] in punct)) or
((pos(s1,s)=1) and (s[pos(s1,s)+length(s1)] in punct)) then }
kol:=kol+1;
delete(s,pos(s1[j],s),length(s1[j]));
end;
col[j]:=kol;
kol1:=kol1+kol;
Label4.Caption:=inttostr(kol1);
end;
end;
end;
end.
Задание: Используя теоретические сведения и примеры, программно реализовать нижеописанный алгоритм, осуществляющий шифрование и дешифрование на любом (по выбору студента) языке программирования.
