unit mainunit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, ExtCtrls, Buttons, Grids;
type
{ TMainForm }
TMainForm = class(TForm)
Key: TLabeledEdit;
Chiaro: TLabeledEdit;
Cifrato: TLabeledEdit;
Label1: TLabel;
Report: TMemo;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
MainForm: TMainForm;
implementation
{ TMainForm }
function createKey(key,chiaro:string):string;
var s,k:string;
j:integer;
begin
j:= 1;
s:= UpperCase(chiaro);
k:= UpperCase(key);
if length(k)>=length(s)
then
k:= copy(k,1,length(s))
else
while length(k) <> length(s) do
begin
k:= k + k[j];
j:=j+1;
end;
result:= k;
end;
function Control(str:string):boolean;
var i:integer;
controllo: boolean;
car: byte;
begin
controllo := true;
if length(str) = 0 then
controllo := false
else
for i:= 1 to length(str) do
begin
car:= ord(str[i]);
if not( ((car>64) and (car<91)) or ((car>96) and (car<123))) then
controllo:= false;
end;
result:= controllo;
end;
procedure TMainForm.SpeedButton1Click(Sender: TObject);
var s,k:string;
l,i: integer;
codice: string;
Rect: TRect;
begin
s:= UpperCase(Chiaro.Text);
if Control(s) and Control(Key.Text) then
begin
Report.Clear;
Report.Lines.Add('--- Start Encoding ---');
Report.Lines.Add('');
k:= createKey(Key.Text,s);
for i:= 1 to Length(s) do
begin
l:= Ord(s[i]) + Ord(k[i]) - 129;
if (l>26) then l:=l-26;
codice:= codice + Chr(l+64);
Report.Lines.Add(' '+s[i]+' + '+k[i]+' = '+Chr(l+64));
end;
Cifrato.Text:= codice;
Report.Lines.Add('');
Report.Lines.Add('--- End Encoding ---');
end
else if not(Control(s)) then
ShowMessage('Il TESTO IN CHIARO contiene caratteri non alfabetici [A-Z,a-z] o è vuota')
else
ShowMessage('La CHIAVE contiene caratteri non alfabetici [A-Z,a-z] o è vuota');
end;
procedure TMainForm.FormCreate(Sender: TObject);
var i,j,k,car:byte;
begin
k:= 1;
for i:= 0 to 25 do
begin
for j:= 0 to 25 do
begin
car := j+k;
if car>26 then car:= car-26;
StringGrid1.Cells[i,j]:= Chr(car+64);
end;
inc(k);
end;
end;
procedure TMainForm.SpeedButton2Click(Sender: TObject);
var s,k:string;
l,i: integer;
codice: string;
begin
s:= UpperCase(Cifrato.Text);
if Control(s) and Control(Key.Text) then
begin
k:= createKey(Key.Text,s);
for i:= 1 to Length(s) do
begin
l:= Ord(s[i]) - Ord(k[i]) +1;
if (l<0) then l:=l+26;
codice:= codice + Chr(l+64);
end;
Chiaro.Text:= codice;
end
else if not(Control(s)) then
ShowMessage('Il TESTO CIFRATO contiene caratteri non alfabetici [A-Z,a-z] o è vuota')
else
ShowMessage('La CHIAVE contiene caratteri non alfabetici [A-Z,a-z] o è vuota');
end;
initialization
{$I mainunit.lrs}
end.