Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. unit MainUnit;
  2.  
  3. interface
  4.  
  5. uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  8. ElgamalUnit, Vcl.Menus;
  9.  
  10. type
  11. TMainForm = class(TForm)
  12. lbPrimeRoots: TListBox;
  13. lbAmount: TLabel;
  14. edP: TEdit;
  15. edX: TEdit;
  16. edK: TEdit;
  17. btEncrypt: TButton;
  18. OPenFile: TOpenDialog;
  19. MainMenu: TMainMenu;
  20. N1: TMenuItem;
  21. mnOpen: TMenuItem;
  22. mnSaveAs: TMenuItem;
  23. SaveFile: TSaveDialog;
  24. btDecipher: TButton;
  25. lbP: TLabel;
  26. lbX: TLabel;
  27. lbK: TLabel;
  28. lbShowRoots: TButton;
  29. mmCipher: TMemo;
  30. lbAmountText: TLabel;
  31. procedure FormCreate(Sender: TObject);
  32. procedure mnOpenClick(Sender: TObject);
  33. procedure btEncryptClick(Sender: TObject);
  34. procedure mnSaveAsClick(Sender: TObject);
  35. procedure btDecipherClick(Sender: TObject);
  36. procedure lbShowRootsClick(Sender: TObject);
  37. private
  38. const
  39. TempFileName = 'Temp\Temp';
  40. var
  41. SourceFileName: string;
  42. OutputFileName: string;
  43. ElgamalCipher: TElgamalCipher;
  44.  
  45. procedure FillPrimeRoots(PrimeNumber: Integer);
  46. procedure ShowError(ErrorType: Integer);
  47. function RemoveAnnesSymbols(const InputStr: string): string;
  48. public
  49. procedure FillMemo(const NumbersArr: IntBuff; Len: Integer); overload;
  50. procedure FillMemo(const NumbersArr: ByteBuff; Len: Integer); overload;
  51. end;
  52.  
  53. var
  54. MainForm: TMainForm;
  55.  
  56. implementation
  57.  
  58. {$R *.dfm}
  59.  
  60. { TMainForm }
  61.  
  62. procedure TMainForm.FormCreate(Sender: TObject);
  63. begin
  64. ElgamalCipher := TElgamalCipher.Create;
  65. mmCipher.Clear;
  66. end;
  67.  
  68. procedure TMainForm.lbShowRootsClick(Sender: TObject);
  69. var
  70. PStr: string;
  71. P: Integer;
  72. begin
  73. PStr := RemoveAnnesSymbols(edP.Text);
  74. if Length(PStr) <> 0 then
  75. begin
  76. try
  77. P := StrToInt(PStr);
  78. except
  79. Exit;
  80. end;
  81. if ElgamalCipher.IsPrime(P) then
  82. FillPrimeRoots(P)
  83. else
  84. ShowError(1);
  85. end;
  86. end;
  87.  
  88. procedure TMainForm.btEncryptClick(Sender: TObject);
  89. const
  90. NoFileMsg = 'Не выбран исходный файл.';
  91. NoFileCaption = 'Не выбран файл';
  92. SuccessText = 'Успешно зашифровано.';
  93. SuccessCaption = 'Зашифровано';
  94. var
  95. ErrorIndex: Integer;
  96. PStr, XStr, KStr: string;
  97. P, X, K: Integer;
  98. begin
  99. if FileExists(SourceFileName) then
  100. begin
  101. PStr := RemoveAnnesSymbols(edP.Text);
  102. XStr := RemoveAnnesSymbols(edX.Text);
  103. KStr := RemoveAnnesSymbols(edK.Text);
  104. if (Length(pStr) = 0) or (Length(XStr) = 0) or (Length(KStr) = 0) then
  105. Exit;
  106. try
  107. P := StrToInt(PStr);
  108. X := StrToInt(XStr);
  109. K := StrToInt(KStr);
  110. except
  111. Exit;
  112. end;
  113. ErrorIndex := ElgamalCipher.CheckNumbers(P, X, K);
  114. if ErrorIndex <> 0 then
  115. begin
  116. ShowError(errorIndex);
  117. Exit;
  118. end;
  119. if lbPrimeRoots.ItemIndex = -1 then
  120. Exit;
  121. OutputFileName := TempFileName + ExtractFileExt(SourceFileName);
  122. mmCipher.Clear;
  123. ElgamalCipher.EncryptFile(SourceFileName, OutputFileName,
  124. P, StrToInt(lbPrimeRoots.Items[lbPrimeRoots.ItemIndex]),
  125. ElgamalCipher.GetOpenKey(StrToInt(lbPrimeRoots.Items[lbPrimeRoots.ItemIndex]),
  126. X, P), K);
  127. MessageBox(Handle, SuccessText, SuccessCaption, MB_OK + MB_ICONINFORMATION);
  128. end
  129. else
  130. MessageBox(Handle, NoFileMsg, NoFileCaption, MB_OK + MB_ICONINFORMATION);
  131. end;
  132.  
  133. procedure TMainForm.btDecipherClick(Sender: TObject);
  134. const
  135. NoFileMsg = 'Не выбран исходный файл.';
  136. NoFileCaption = 'Не выбран файл';
  137. SuccessText = 'Успешно расшифровано.';
  138. SuccessCaption = 'Расшифровано';
  139. var
  140. XStr, PStr: string;
  141. X, P, ErrorIndex: Integer;
  142. begin
  143. if FileExists(SourceFileName) then
  144. begin
  145. PStr := RemoveAnnesSymbols(edP.Text);
  146. XStr := RemoveAnnesSymbols(edX.Text);
  147. if (Length(pStr) = 0) or (Length(XStr) = 0) then
  148. Exit;
  149. try
  150. P := StrToInt(PStr);
  151. X := StrToInt(XStr);
  152. except
  153. Exit;
  154. end;
  155. ErrorIndex := ElgamalCipher.CheckNumbers(P, X, 0);
  156. if (ErrorIndex <> 0) and (ErrorIndex <> 3) then
  157. begin
  158. ShowError(errorIndex);
  159. Exit;
  160. end;
  161. OutputFileName := TempFileName + ExtractFileExt(SourceFileName);
  162. mmCipher.Clear;
  163. ElgamalCipher.DecryptFile(SourceFileName, OutputFileName, X, P);
  164. MessageBox(Handle, SuccessText, SuccessCaption, MB_OK + MB_ICONINFORMATION);
  165. end
  166. else
  167. MessageBox(Handle, NoFileMsg, NoFileCaption, MB_OK + MB_ICONINFORMATION);
  168. end;
  169.  
  170. procedure TMainForm.FillMemo(const NumbersArr: ByteBuff; Len: Integer);
  171. const
  172. MaxAmount = 79;
  173. var
  174. i: Integer;
  175. begin
  176. Dec(Len);
  177. if Len > MaxAmount then
  178. len := MaxAmount;
  179. for i := 0 to Len do
  180. mmCipher.Text := mmCipher.Text + ' ' + IntToStr(NumbersArr[i]);
  181. end;
  182.  
  183. procedure TMainForm.FillMemo(const NumbersArr: IntBuff; Len: Integer);
  184. const
  185. MaxAmount = 79;
  186. var
  187. i: Integer;
  188. begin
  189. Dec(Len);
  190. if Len > MaxAmount then
  191. len := MaxAmount;
  192. for i := 0 to Len do
  193. mmCipher.Text := mmCipher.Text + ' ' + IntToStr(NumbersArr[i]);
  194. end;
  195.  
  196. procedure TMainForm.FillPrimeRoots(PrimeNumber: Integer);
  197. var
  198. PrimeRoots: TIntArray;
  199. i, Len: Integer;
  200. begin
  201. lbPrimeRoots.Clear;
  202. PrimeRoots := ElgamalCipher.GetPrimeRoots(PrimeNumber);
  203. lbAmount.Caption := IntToStr(Length(PrimeRoots));
  204. Len := Length(PrimeRoots) - 1;
  205. if Len > 2000 then
  206. Len := 2000;
  207. for i := 0 to Len do
  208. lbPrimeRoots.Items.Add(IntToStr(PrimeRoots[i]));
  209. end;
  210.  
  211. procedure TMainForm.ShowError(ErrorType: Integer);
  212. const
  213. ErrorCaption = 'Ошибка';
  214. NotSimpleP = 'Число p должно быть простым и превышать 255.';
  215. WrongX = 'Число x должно принадлежать интервалу (1; p - 1).';
  216. WrongK = 'Число k должно быть взаимно простым с p - 1 и принадлежать интервалу (1; p - 1).';
  217. begin
  218. case ErrorType of
  219. 1: MessageBox(Handle, NotSimpleP, ErrorCaption, MB_OK + MB_ICONERROR);
  220. 2: MessageBox(Handle, WrongX, ErrorCaption, MB_OK + MB_ICONERROR);
  221. 3: MessageBox(Handle, WrongK, ErrorCaption, MB_OK + MB_ICONERROR);
  222. end;
  223. end;
  224.  
  225. procedure TMainForm.mnOpenClick(Sender: TObject);
  226. begin
  227. if OpenFile.Execute then
  228. begin
  229. SourceFileName := OpenFile.FileName;
  230. if FileExists(SourceFileName) then
  231. begin
  232. SourceFileName := SourceFileName;
  233. // lbCurrFileName.Caption := SourceFileName;
  234. // ClearGrid;
  235. end
  236. else
  237. SourceFileName := '';
  238. end;
  239. end;
  240.  
  241. procedure TMainForm.mnSaveAsClick(Sender: TObject);
  242. begin
  243. if FileExists(SourceFileName) and FileExists(OutputFileName) then
  244. if SaveFile.Execute then
  245. begin
  246. if ExtractFileExt(SaveFile.FileName) = ExtractFileExt(SourceFileName) then
  247. CopyFile(PChar(OutputFileName), PChar(SaveFile.FileName), false)
  248. else
  249. CopyFile(PChar(OutputFileName), PChar(SaveFile.FileName + ExtractFileExt(SourceFileName)), false);
  250. end;
  251. end;
  252.  
  253. function TMainForm.RemoveAnnesSymbols(const InputStr: string): string;
  254. const
  255. ValidSymbols = ['0'..'9'];
  256. var
  257. i: Integer;
  258. begin
  259. Result := '';
  260. for i := 1 to Length(InputStr) do
  261. if InputStr[i] in ValidSymbols then
  262. Result := Result + InputStr[i];
  263. end;
  264.  
  265. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement