Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit MainUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
- ElgamalUnit, Vcl.Menus;
- type
- TMainForm = class(TForm)
- lbPrimeRoots: TListBox;
- lbAmount: TLabel;
- edP: TEdit;
- edX: TEdit;
- edK: TEdit;
- btEncrypt: TButton;
- OPenFile: TOpenDialog;
- MainMenu: TMainMenu;
- N1: TMenuItem;
- mnOpen: TMenuItem;
- mnSaveAs: TMenuItem;
- SaveFile: TSaveDialog;
- btDecipher: TButton;
- lbP: TLabel;
- lbX: TLabel;
- lbK: TLabel;
- lbShowRoots: TButton;
- mmCipher: TMemo;
- lbAmountText: TLabel;
- procedure FormCreate(Sender: TObject);
- procedure mnOpenClick(Sender: TObject);
- procedure btEncryptClick(Sender: TObject);
- procedure mnSaveAsClick(Sender: TObject);
- procedure btDecipherClick(Sender: TObject);
- procedure lbShowRootsClick(Sender: TObject);
- private
- const
- TempFileName = 'Temp\Temp';
- var
- SourceFileName: string;
- OutputFileName: string;
- ElgamalCipher: TElgamalCipher;
- procedure FillPrimeRoots(PrimeNumber: Integer);
- procedure ShowError(ErrorType: Integer);
- function RemoveAnnesSymbols(const InputStr: string): string;
- public
- procedure FillMemo(const NumbersArr: IntBuff; Len: Integer); overload;
- procedure FillMemo(const NumbersArr: ByteBuff; Len: Integer); overload;
- end;
- var
- MainForm: TMainForm;
- implementation
- {$R *.dfm}
- { TMainForm }
- procedure TMainForm.FormCreate(Sender: TObject);
- begin
- ElgamalCipher := TElgamalCipher.Create;
- mmCipher.Clear;
- end;
- procedure TMainForm.lbShowRootsClick(Sender: TObject);
- var
- PStr: string;
- P: Integer;
- begin
- PStr := RemoveAnnesSymbols(edP.Text);
- if Length(PStr) <> 0 then
- begin
- try
- P := StrToInt(PStr);
- except
- Exit;
- end;
- if ElgamalCipher.IsPrime(P) then
- FillPrimeRoots(P)
- else
- ShowError(1);
- end;
- end;
- procedure TMainForm.btEncryptClick(Sender: TObject);
- const
- NoFileMsg = 'Не выбран исходный файл.';
- NoFileCaption = 'Не выбран файл';
- SuccessText = 'Успешно зашифровано.';
- SuccessCaption = 'Зашифровано';
- var
- ErrorIndex: Integer;
- PStr, XStr, KStr: string;
- P, X, K: Integer;
- begin
- if FileExists(SourceFileName) then
- begin
- PStr := RemoveAnnesSymbols(edP.Text);
- XStr := RemoveAnnesSymbols(edX.Text);
- KStr := RemoveAnnesSymbols(edK.Text);
- if (Length(pStr) = 0) or (Length(XStr) = 0) or (Length(KStr) = 0) then
- Exit;
- try
- P := StrToInt(PStr);
- X := StrToInt(XStr);
- K := StrToInt(KStr);
- except
- Exit;
- end;
- ErrorIndex := ElgamalCipher.CheckNumbers(P, X, K);
- if ErrorIndex <> 0 then
- begin
- ShowError(errorIndex);
- Exit;
- end;
- if lbPrimeRoots.ItemIndex = -1 then
- Exit;
- OutputFileName := TempFileName + ExtractFileExt(SourceFileName);
- mmCipher.Clear;
- ElgamalCipher.EncryptFile(SourceFileName, OutputFileName,
- P, StrToInt(lbPrimeRoots.Items[lbPrimeRoots.ItemIndex]),
- ElgamalCipher.GetOpenKey(StrToInt(lbPrimeRoots.Items[lbPrimeRoots.ItemIndex]),
- X, P), K);
- MessageBox(Handle, SuccessText, SuccessCaption, MB_OK + MB_ICONINFORMATION);
- end
- else
- MessageBox(Handle, NoFileMsg, NoFileCaption, MB_OK + MB_ICONINFORMATION);
- end;
- procedure TMainForm.btDecipherClick(Sender: TObject);
- const
- NoFileMsg = 'Не выбран исходный файл.';
- NoFileCaption = 'Не выбран файл';
- SuccessText = 'Успешно расшифровано.';
- SuccessCaption = 'Расшифровано';
- var
- XStr, PStr: string;
- X, P, ErrorIndex: Integer;
- begin
- if FileExists(SourceFileName) then
- begin
- PStr := RemoveAnnesSymbols(edP.Text);
- XStr := RemoveAnnesSymbols(edX.Text);
- if (Length(pStr) = 0) or (Length(XStr) = 0) then
- Exit;
- try
- P := StrToInt(PStr);
- X := StrToInt(XStr);
- except
- Exit;
- end;
- ErrorIndex := ElgamalCipher.CheckNumbers(P, X, 0);
- if (ErrorIndex <> 0) and (ErrorIndex <> 3) then
- begin
- ShowError(errorIndex);
- Exit;
- end;
- OutputFileName := TempFileName + ExtractFileExt(SourceFileName);
- mmCipher.Clear;
- ElgamalCipher.DecryptFile(SourceFileName, OutputFileName, X, P);
- MessageBox(Handle, SuccessText, SuccessCaption, MB_OK + MB_ICONINFORMATION);
- end
- else
- MessageBox(Handle, NoFileMsg, NoFileCaption, MB_OK + MB_ICONINFORMATION);
- end;
- procedure TMainForm.FillMemo(const NumbersArr: ByteBuff; Len: Integer);
- const
- MaxAmount = 79;
- var
- i: Integer;
- begin
- Dec(Len);
- if Len > MaxAmount then
- len := MaxAmount;
- for i := 0 to Len do
- mmCipher.Text := mmCipher.Text + ' ' + IntToStr(NumbersArr[i]);
- end;
- procedure TMainForm.FillMemo(const NumbersArr: IntBuff; Len: Integer);
- const
- MaxAmount = 79;
- var
- i: Integer;
- begin
- Dec(Len);
- if Len > MaxAmount then
- len := MaxAmount;
- for i := 0 to Len do
- mmCipher.Text := mmCipher.Text + ' ' + IntToStr(NumbersArr[i]);
- end;
- procedure TMainForm.FillPrimeRoots(PrimeNumber: Integer);
- var
- PrimeRoots: TIntArray;
- i, Len: Integer;
- begin
- lbPrimeRoots.Clear;
- PrimeRoots := ElgamalCipher.GetPrimeRoots(PrimeNumber);
- lbAmount.Caption := IntToStr(Length(PrimeRoots));
- Len := Length(PrimeRoots) - 1;
- if Len > 2000 then
- Len := 2000;
- for i := 0 to Len do
- lbPrimeRoots.Items.Add(IntToStr(PrimeRoots[i]));
- end;
- procedure TMainForm.ShowError(ErrorType: Integer);
- const
- ErrorCaption = 'Ошибка';
- NotSimpleP = 'Число p должно быть простым и превышать 255.';
- WrongX = 'Число x должно принадлежать интервалу (1; p - 1).';
- WrongK = 'Число k должно быть взаимно простым с p - 1 и принадлежать интервалу (1; p - 1).';
- begin
- case ErrorType of
- 1: MessageBox(Handle, NotSimpleP, ErrorCaption, MB_OK + MB_ICONERROR);
- 2: MessageBox(Handle, WrongX, ErrorCaption, MB_OK + MB_ICONERROR);
- 3: MessageBox(Handle, WrongK, ErrorCaption, MB_OK + MB_ICONERROR);
- end;
- end;
- procedure TMainForm.mnOpenClick(Sender: TObject);
- begin
- if OpenFile.Execute then
- begin
- SourceFileName := OpenFile.FileName;
- if FileExists(SourceFileName) then
- begin
- SourceFileName := SourceFileName;
- // lbCurrFileName.Caption := SourceFileName;
- // ClearGrid;
- end
- else
- SourceFileName := '';
- end;
- end;
- procedure TMainForm.mnSaveAsClick(Sender: TObject);
- begin
- if FileExists(SourceFileName) and FileExists(OutputFileName) then
- if SaveFile.Execute then
- begin
- if ExtractFileExt(SaveFile.FileName) = ExtractFileExt(SourceFileName) then
- CopyFile(PChar(OutputFileName), PChar(SaveFile.FileName), false)
- else
- CopyFile(PChar(OutputFileName), PChar(SaveFile.FileName + ExtractFileExt(SourceFileName)), false);
- end;
- end;
- function TMainForm.RemoveAnnesSymbols(const InputStr: string): string;
- const
- ValidSymbols = ['0'..'9'];
- var
- i: Integer;
- begin
- Result := '';
- for i := 1 to Length(InputStr) do
- if InputStr[i] in ValidSymbols then
- Result := Result + InputStr[i];
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement