Advertisement
Guest User

Untitled

a guest
May 29th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.14 KB | None | 0 0
  1. unit Playfair;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7. Dialogs, StdCtrls, Grids, ComCtrls,About, Buttons, ExtCtrls;
  8.  
  9. type
  10. TForm1 = class(TForm)
  11. Button1: TButton;
  12. grid: TStringGrid;
  13. Edit1: TEdit;
  14. Edit3: TEdit;
  15. Edit2: TEdit;
  16. Edit4: TEdit;
  17. Button2: TButton;
  18. cb_hlg: TComboBox;
  19. cb_dummy: TComboBox;
  20. cb_geser: TComboBox;
  21. cb_tabel: TComboBox;
  22. Label1: TLabel;
  23. Label2: TLabel;
  24. Label3: TLabel;
  25. Label4: TLabel;
  26. Label5: TLabel;
  27. Label6: TLabel;
  28. Label7: TLabel;
  29. Label8: TLabel;
  30. Label9: TLabel;
  31. CheckBox1: TCheckBox;
  32. CheckBox2: TCheckBox;
  33. BitBtn1: TBitBtn;
  34. Label10: TLabel;
  35. Image1: TImage;
  36. procedure Button1Click(Sender: TObject);
  37. procedure Button2Click(Sender: TObject);
  38. procedure cb_tabelChange(Sender: TObject);
  39. procedure Edit1Change(Sender: TObject);
  40. procedure Edit4Change(Sender: TObject);
  41. procedure Edit2Change(Sender: TObject);
  42. procedure cb_hlgChange(Sender: TObject);
  43. procedure BitBtn1Click(Sender: TObject);
  44. procedure FormCreate(Sender: TObject);
  45. private
  46. { Private declarations }
  47. public
  48. { Public declarations }
  49. end;
  50.  
  51. var
  52. Form1: TForm1;
  53. pjg_plain,pjg_key,pjg_cip,kol1,bar1,kol2,bar2,geser : integer;
  54. c,dum_c,X1,X2,HasilEnkrip,HasilDekrip : string;
  55. Key,PlainT,CipherT,Ekstrak,List_Huruf : string;
  56.  
  57. implementation
  58.  
  59. {$R *.dfm}
  60.  
  61. //-----fungsi Uppercase string
  62. function Upper(s:string):string;
  63. var i:byte;
  64. begin
  65. for i:=1 to length(s) do
  66. s[i]:=upcase(s[i]);
  67. result :=s;
  68. end;
  69. //---------------------------------
  70.  
  71. //-----Ekstrak Kunci-----------
  72. function EkstrakKunci(e_key:string;var pjg:integer) : string;
  73. var i,j:integer;
  74. Hasil : string;
  75. isi_key : char;
  76. begin
  77. for i:=1 to pjg do
  78. begin
  79. isi_key:=e_key[i];
  80. if not (isi_key=#32) then
  81. for j:=i+1 to pjg do
  82. if e_key[j]=isi_key then
  83. e_key[j]:=#32;
  84. end;
  85. for i:=1 to pjg do
  86. begin
  87. isi_key:=e_key[i];
  88. if not (isi_key=#32) then
  89. Hasil:=Concat(hasil,isi_key);
  90. end;
  91. Result:=Hasil;
  92. end;
  93. //------------------------------------
  94.  
  95. //--------Generate Susunan Huruf-----------
  96. function GenerateHuruf(Ekstrak : string):string;
  97. var i : integer;
  98. found : boolean;
  99. isi_key : char;
  100. begin
  101. pjg_key:=length(Ekstrak);
  102. for isi_key:='A' to 'Z' do
  103. begin
  104. found:=false;
  105. i:=0;
  106. while not found and (i<pjg_key) do
  107. begin
  108. i:=i+1;
  109. if Ekstrak[i]=isi_key then found:=true;
  110. end;
  111. if not found then
  112. Ekstrak:=Concat(Ekstrak,isi_key);
  113. end;
  114. Result:=Ekstrak;
  115. end;
  116. //------------------------------------------
  117.  
  118. //-----generate tabel Kunci (Baris)-------------------
  119. procedure TabelKunciBaris(List : string;var huruf_hilang:string);
  120. var a,i,j : integer;
  121. begin
  122. a:=1;
  123. for i := 0 to 4 do
  124. for j := 0 to 4 do
  125. begin
  126. if List[a] <> huruf_hilang then
  127. Form1.Grid.Cells[j,i]:=List[a]
  128. else
  129. begin
  130. Form1.Grid.Cells[j,i]:=List[a+1];
  131. a:=a+1;
  132. end;
  133. a:=a+1;
  134. end;
  135. end;
  136. //----------------------------------------
  137.  
  138. //-----generate tabel Kunci (Kolom)-------------------
  139. procedure TabelKunciKolom(List : string;var huruf_hilang:string);
  140. var a,i,j : integer;
  141. begin
  142. a:=1;
  143. for i := 0 to 4 do
  144. for j := 0 to 4 do
  145. begin
  146. if List[a] <> huruf_hilang then
  147. Form1.Grid.Cells[i,j]:=List[a]
  148. else
  149. begin
  150. Form1.Grid.Cells[i,j]:=List[a+1];
  151. a:=a+1;
  152. end;
  153. a:=a+1;
  154. end;
  155. end;
  156. //----------------------------------------
  157.  
  158. //-----fungsi enkrip setiap 2 huruf---------------------
  159. function Enkrip(Huruf1:string;var Huruf2:string):string;
  160. var i,j : integer;
  161. begin
  162. with Form1.Grid do
  163. begin
  164. for i := 0 to 4 do
  165. for j := 0 to 4 do
  166. begin
  167. if (Cells[i,j]) = (Huruf1) then
  168. begin
  169. kol1:=i;
  170. bar1:=j;
  171. end;
  172. if (Cells[i,j]) = (Huruf2) then
  173. begin
  174. kol2:=i;
  175. bar2:=j;
  176. end;
  177. end;
  178.  
  179. if (kol1 <> kol2) or (bar1 <> bar2) then
  180. Enkrip:=concat(Cells[kol2,bar1],Cells[kol1,bar2]);
  181.  
  182. if (kol1 = kol2) then
  183. begin
  184. bar1:=bar1+geser;
  185. bar2:=bar2+geser;
  186. if(bar1 > 4) then
  187. bar1:=bar1-5;
  188. if(bar2 > 4) then
  189. bar2:=bar2-5;
  190.  
  191. Enkrip:=concat(Cells[kol2,bar1],Cells[kol1,bar2]);
  192. end;
  193.  
  194. if (bar1 = bar2) then
  195. begin
  196. kol1:=kol1+geser;
  197. kol2:=kol2+geser;
  198. if(kol1 > 4) then
  199. kol1:=kol1-5;
  200. if(kol2 > 4) then
  201. kol2:=kol2-5;
  202.  
  203. Enkrip:=concat(Cells[kol1,bar2],Cells[kol2,bar1]);
  204. end;
  205. end;
  206. end;
  207. //-----------------------------------------------------------
  208.  
  209. //-----Enkrip Kata-------------------------------------------
  210. function Enkripsi(plainteks:string;var pjg : integer;var dummy:string):string;
  211. var a,k : integer;
  212. Hasil : string;
  213. cipher : array[1..100] of string;
  214. begin
  215. a:=0;
  216. k:=1;
  217. while k <= pjg do
  218. begin
  219. if(plainteks[k] <> plainteks[k+1]) then
  220. begin
  221. a:=a+1;
  222. X1:=plainteks[k];
  223. X2:=plainteks[k+1];
  224. if(plainteks[k+1] = '') then
  225. X2:=dummy;
  226. cipher[a]:=Enkrip(X1,X2);
  227. end;
  228. if(plainteks[k] = plainteks[k+1]) then
  229. begin
  230. a:=a+1;
  231. X1:=plainteks[k];
  232. X2:=dummy;
  233. cipher[a]:=Enkrip(X1,X2);
  234. k:=k-1;
  235. end;
  236. k:=k+2;
  237. Hasil:=concat(Hasil,cipher[a]);
  238. end;
  239. Result:=Hasil;
  240. end;
  241. //---------------------------------------------
  242.  
  243. //-----fungsi dekrip setiap 2 huruf---------------------
  244. function Dekrip(Huruf1:string;var Huruf2:string):string;
  245. var i,j : integer;
  246. begin
  247. with Form1.Grid do
  248. begin
  249. for i := 0 to 4 do
  250. for j := 0 to 4 do
  251. begin
  252. if (Cells[i,j]) = (Huruf1) then
  253. begin
  254. kol1:=i;
  255. bar1:=j;
  256. end;
  257. if (Cells[i,j]) = (Huruf2) then
  258. begin
  259. kol2:=i;
  260. bar2:=j;
  261. end;
  262. end;
  263.  
  264. if (kol1 <> kol2) or (bar1 <> bar2) then
  265. Dekrip:=concat(Cells[kol2,bar1],Cells[kol1,bar2]);
  266.  
  267. if (kol1 = kol2) then
  268. begin
  269. bar1:=bar1-geser;
  270. bar2:=bar2-geser;
  271. if(bar1 < 0) then
  272. bar1:=bar1+5;
  273. if(bar2 < 0) then
  274. bar2:=bar2+5;
  275.  
  276. Dekrip:=concat(Cells[kol2,bar1],Cells[kol1,bar2]);
  277. end;
  278.  
  279. if (bar1 = bar2) then
  280. begin
  281. kol1:=kol1-geser;
  282. kol2:=kol2-geser;
  283. if(kol1 < 0) then
  284. kol1:=kol1+5;
  285. if(kol2 < 0) then
  286. kol2:=kol2+5;
  287.  
  288. Dekrip:=concat(Cells[kol1,bar2],Cells[kol2,bar1]);
  289. end;
  290. end;
  291. end;
  292. //-----------------------------------------------------------
  293.  
  294. //-----Dekripsi Kata-------------------------------------------
  295. function Dekripsi(cipherteks:string;var pjg : integer;var dummy:string):string;
  296. var a,k : integer;
  297. Hasil,Eks_Hasil : string;
  298. plain : array[1..100] of string;
  299. begin
  300. a:=0;
  301. k:=1;
  302. while k <= pjg do
  303. begin
  304. a:=a+1;
  305. X1:=cipherteks[k];
  306. X2:=cipherteks[k+1];
  307.  
  308. plain[a]:=Dekrip(X1,X2);
  309.  
  310. k:=k+2;
  311. Hasil:=concat(Hasil,plain[a]);
  312. end;
  313.  
  314. pjg:=length(Hasil);
  315. for k:= 1 to pjg do
  316. begin
  317. if Hasil[k] <> dummy then
  318. Eks_Hasil:=concat(Eks_Hasil,Hasil[k]);
  319. end;
  320. Result:=Eks_Hasil;
  321. end;
  322. //---------------------------------------------
  323.  
  324. procedure TForm1.BitBtn1Click(Sender: TObject);
  325. begin
  326. About.AboutBox.Show;
  327. end;
  328.  
  329. procedure TForm1.Button1Click(Sender: TObject);
  330. begin
  331. c:=cb_hlg.Text;
  332. geser:=strtoint(cb_geser.Text);
  333. dum_c:=cb_dummy.Text;
  334.  
  335.  
  336. PlainT:=upper(edit1.Text);
  337. pjg_plain:=length(PlainT);
  338. Key:=upper(edit2.Text);
  339. pjg_key:=length(Key);
  340.  
  341. Ekstrak:=EkstrakKunci(Key,pjg_key);
  342.  
  343. List_Huruf:=GenerateHuruf(Ekstrak);
  344.  
  345. if cb_tabel.Text = 'Kolom' then
  346. TabelKunciKolom(List_Huruf,c);
  347. if cb_tabel.Text = 'Baris' then
  348. TabelKunciBaris(List_Huruf,c);
  349.  
  350. HasilEnkrip:=Enkripsi(PlainT,pjg_plain,dum_c);
  351.  
  352. Edit3.Clear;
  353. Edit3.Font.Color:=clblue;
  354. Edit3.Text:=HasilEnkrip;
  355. end;
  356.  
  357. procedure TForm1.Button2Click(Sender: TObject);
  358. begin
  359. CipherT:=edit4.Text;
  360. Pjg_cip:=length(CipherT);
  361. Key:=upper(edit2.Text);
  362. pjg_key:=length(Key);
  363.  
  364. c:=cb_hlg.Text;
  365. geser:=strtoint(cb_geser.Text);
  366. dum_c:=cb_dummy.Text;
  367.  
  368. Ekstrak:=EkstrakKunci(Key,pjg_key);
  369.  
  370. List_Huruf:=GenerateHuruf(Ekstrak);
  371.  
  372. if cb_tabel.Text = 'Kolom' then
  373. TabelKunciKolom(List_Huruf,c);
  374. if cb_tabel.Text = 'Baris' then
  375. TabelKunciBaris(List_Huruf,c);
  376.  
  377. HasilDekrip:=Dekripsi(CipherT,pjg_cip,dum_c);
  378.  
  379. Edit3.Clear;
  380. Edit3.Font.Color:=clgreen;
  381. Edit3.Text:=HasilDekrip;
  382. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement