Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Playfair;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, Grids, ComCtrls,About, Buttons, ExtCtrls;
- type
- TForm1 = class(TForm)
- Button1: TButton;
- grid: TStringGrid;
- Edit1: TEdit;
- Edit3: TEdit;
- Edit2: TEdit;
- Edit4: TEdit;
- Button2: TButton;
- cb_hlg: TComboBox;
- cb_dummy: TComboBox;
- cb_geser: TComboBox;
- cb_tabel: TComboBox;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- Label5: TLabel;
- Label6: TLabel;
- Label7: TLabel;
- Label8: TLabel;
- Label9: TLabel;
- CheckBox1: TCheckBox;
- CheckBox2: TCheckBox;
- BitBtn1: TBitBtn;
- Label10: TLabel;
- Image1: TImage;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure cb_tabelChange(Sender: TObject);
- procedure Edit1Change(Sender: TObject);
- procedure Edit4Change(Sender: TObject);
- procedure Edit2Change(Sender: TObject);
- procedure cb_hlgChange(Sender: TObject);
- procedure BitBtn1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- pjg_plain,pjg_key,pjg_cip,kol1,bar1,kol2,bar2,geser : integer;
- c,dum_c,X1,X2,HasilEnkrip,HasilDekrip : string;
- Key,PlainT,CipherT,Ekstrak,List_Huruf : string;
- implementation
- {$R *.dfm}
- //-----fungsi Uppercase string
- function Upper(s:string):string;
- var i:byte;
- begin
- for i:=1 to length(s) do
- s[i]:=upcase(s[i]);
- result :=s;
- end;
- //---------------------------------
- //-----Ekstrak Kunci-----------
- function EkstrakKunci(e_key:string;var pjg:integer) : string;
- var i,j:integer;
- Hasil : string;
- isi_key : char;
- begin
- for i:=1 to pjg do
- begin
- isi_key:=e_key[i];
- if not (isi_key=#32) then
- for j:=i+1 to pjg do
- if e_key[j]=isi_key then
- e_key[j]:=#32;
- end;
- for i:=1 to pjg do
- begin
- isi_key:=e_key[i];
- if not (isi_key=#32) then
- Hasil:=Concat(hasil,isi_key);
- end;
- Result:=Hasil;
- end;
- //------------------------------------
- //--------Generate Susunan Huruf-----------
- function GenerateHuruf(Ekstrak : string):string;
- var i : integer;
- found : boolean;
- isi_key : char;
- begin
- pjg_key:=length(Ekstrak);
- for isi_key:='A' to 'Z' do
- begin
- found:=false;
- i:=0;
- while not found and (i<pjg_key) do
- begin
- i:=i+1;
- if Ekstrak[i]=isi_key then found:=true;
- end;
- if not found then
- Ekstrak:=Concat(Ekstrak,isi_key);
- end;
- Result:=Ekstrak;
- end;
- //------------------------------------------
- //-----generate tabel Kunci (Baris)-------------------
- procedure TabelKunciBaris(List : string;var huruf_hilang:string);
- var a,i,j : integer;
- begin
- a:=1;
- for i := 0 to 4 do
- for j := 0 to 4 do
- begin
- if List[a] <> huruf_hilang then
- Form1.Grid.Cells[j,i]:=List[a]
- else
- begin
- Form1.Grid.Cells[j,i]:=List[a+1];
- a:=a+1;
- end;
- a:=a+1;
- end;
- end;
- //----------------------------------------
- //-----generate tabel Kunci (Kolom)-------------------
- procedure TabelKunciKolom(List : string;var huruf_hilang:string);
- var a,i,j : integer;
- begin
- a:=1;
- for i := 0 to 4 do
- for j := 0 to 4 do
- begin
- if List[a] <> huruf_hilang then
- Form1.Grid.Cells[i,j]:=List[a]
- else
- begin
- Form1.Grid.Cells[i,j]:=List[a+1];
- a:=a+1;
- end;
- a:=a+1;
- end;
- end;
- //----------------------------------------
- //-----fungsi enkrip setiap 2 huruf---------------------
- function Enkrip(Huruf1:string;var Huruf2:string):string;
- var i,j : integer;
- begin
- with Form1.Grid do
- begin
- for i := 0 to 4 do
- for j := 0 to 4 do
- begin
- if (Cells[i,j]) = (Huruf1) then
- begin
- kol1:=i;
- bar1:=j;
- end;
- if (Cells[i,j]) = (Huruf2) then
- begin
- kol2:=i;
- bar2:=j;
- end;
- end;
- if (kol1 <> kol2) or (bar1 <> bar2) then
- Enkrip:=concat(Cells[kol2,bar1],Cells[kol1,bar2]);
- if (kol1 = kol2) then
- begin
- bar1:=bar1+geser;
- bar2:=bar2+geser;
- if(bar1 > 4) then
- bar1:=bar1-5;
- if(bar2 > 4) then
- bar2:=bar2-5;
- Enkrip:=concat(Cells[kol2,bar1],Cells[kol1,bar2]);
- end;
- if (bar1 = bar2) then
- begin
- kol1:=kol1+geser;
- kol2:=kol2+geser;
- if(kol1 > 4) then
- kol1:=kol1-5;
- if(kol2 > 4) then
- kol2:=kol2-5;
- Enkrip:=concat(Cells[kol1,bar2],Cells[kol2,bar1]);
- end;
- end;
- end;
- //-----------------------------------------------------------
- //-----Enkrip Kata-------------------------------------------
- function Enkripsi(plainteks:string;var pjg : integer;var dummy:string):string;
- var a,k : integer;
- Hasil : string;
- cipher : array[1..100] of string;
- begin
- a:=0;
- k:=1;
- while k <= pjg do
- begin
- if(plainteks[k] <> plainteks[k+1]) then
- begin
- a:=a+1;
- X1:=plainteks[k];
- X2:=plainteks[k+1];
- if(plainteks[k+1] = '') then
- X2:=dummy;
- cipher[a]:=Enkrip(X1,X2);
- end;
- if(plainteks[k] = plainteks[k+1]) then
- begin
- a:=a+1;
- X1:=plainteks[k];
- X2:=dummy;
- cipher[a]:=Enkrip(X1,X2);
- k:=k-1;
- end;
- k:=k+2;
- Hasil:=concat(Hasil,cipher[a]);
- end;
- Result:=Hasil;
- end;
- //---------------------------------------------
- //-----fungsi dekrip setiap 2 huruf---------------------
- function Dekrip(Huruf1:string;var Huruf2:string):string;
- var i,j : integer;
- begin
- with Form1.Grid do
- begin
- for i := 0 to 4 do
- for j := 0 to 4 do
- begin
- if (Cells[i,j]) = (Huruf1) then
- begin
- kol1:=i;
- bar1:=j;
- end;
- if (Cells[i,j]) = (Huruf2) then
- begin
- kol2:=i;
- bar2:=j;
- end;
- end;
- if (kol1 <> kol2) or (bar1 <> bar2) then
- Dekrip:=concat(Cells[kol2,bar1],Cells[kol1,bar2]);
- if (kol1 = kol2) then
- begin
- bar1:=bar1-geser;
- bar2:=bar2-geser;
- if(bar1 < 0) then
- bar1:=bar1+5;
- if(bar2 < 0) then
- bar2:=bar2+5;
- Dekrip:=concat(Cells[kol2,bar1],Cells[kol1,bar2]);
- end;
- if (bar1 = bar2) then
- begin
- kol1:=kol1-geser;
- kol2:=kol2-geser;
- if(kol1 < 0) then
- kol1:=kol1+5;
- if(kol2 < 0) then
- kol2:=kol2+5;
- Dekrip:=concat(Cells[kol1,bar2],Cells[kol2,bar1]);
- end;
- end;
- end;
- //-----------------------------------------------------------
- //-----Dekripsi Kata-------------------------------------------
- function Dekripsi(cipherteks:string;var pjg : integer;var dummy:string):string;
- var a,k : integer;
- Hasil,Eks_Hasil : string;
- plain : array[1..100] of string;
- begin
- a:=0;
- k:=1;
- while k <= pjg do
- begin
- a:=a+1;
- X1:=cipherteks[k];
- X2:=cipherteks[k+1];
- plain[a]:=Dekrip(X1,X2);
- k:=k+2;
- Hasil:=concat(Hasil,plain[a]);
- end;
- pjg:=length(Hasil);
- for k:= 1 to pjg do
- begin
- if Hasil[k] <> dummy then
- Eks_Hasil:=concat(Eks_Hasil,Hasil[k]);
- end;
- Result:=Eks_Hasil;
- end;
- //---------------------------------------------
- procedure TForm1.BitBtn1Click(Sender: TObject);
- begin
- About.AboutBox.Show;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- c:=cb_hlg.Text;
- geser:=strtoint(cb_geser.Text);
- dum_c:=cb_dummy.Text;
- PlainT:=upper(edit1.Text);
- pjg_plain:=length(PlainT);
- Key:=upper(edit2.Text);
- pjg_key:=length(Key);
- Ekstrak:=EkstrakKunci(Key,pjg_key);
- List_Huruf:=GenerateHuruf(Ekstrak);
- if cb_tabel.Text = 'Kolom' then
- TabelKunciKolom(List_Huruf,c);
- if cb_tabel.Text = 'Baris' then
- TabelKunciBaris(List_Huruf,c);
- HasilEnkrip:=Enkripsi(PlainT,pjg_plain,dum_c);
- Edit3.Clear;
- Edit3.Font.Color:=clblue;
- Edit3.Text:=HasilEnkrip;
- end;
- procedure TForm1.Button2Click(Sender: TObject);
- begin
- CipherT:=edit4.Text;
- Pjg_cip:=length(CipherT);
- Key:=upper(edit2.Text);
- pjg_key:=length(Key);
- c:=cb_hlg.Text;
- geser:=strtoint(cb_geser.Text);
- dum_c:=cb_dummy.Text;
- Ekstrak:=EkstrakKunci(Key,pjg_key);
- List_Huruf:=GenerateHuruf(Ekstrak);
- if cb_tabel.Text = 'Kolom' then
- TabelKunciKolom(List_Huruf,c);
- if cb_tabel.Text = 'Baris' then
- TabelKunciBaris(List_Huruf,c);
- HasilDekrip:=Dekripsi(CipherT,pjg_cip,dum_c);
- Edit3.Clear;
- Edit3.Font.Color:=clgreen;
- Edit3.Text:=HasilDekrip;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement