Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit UnitMain;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Menus;
- type
- TForm_TI_1 = class(TForm)
- MainMenu: TMainMenu;
- LabelKey: TLabel;
- EditKey: TEdit;
- LabelPlaitext: TLabel;
- ButtonEncryption: TButton;
- RadioButtonDecimationMethod: TRadioButton;
- RadioButtonVigenereCipher: TRadioButton;
- LabelEncryptionType: TLabel;
- OpenDialog: TOpenDialog;
- SaveDialog: TSaveDialog;
- MemoPlainText: TMemo;
- Label1: TLabel;
- ButtonDecryption: TButton;
- MemoCiphertext: TMemo;
- ButFile: TMenuItem;
- ButOpenFile: TMenuItem;
- ButSaveFile: TMenuItem;
- ButInstruction: TMenuItem;
- procedure ButOpenFileClick(Sender: TObject);
- procedure EditKeyChange(Sender: TObject);
- procedure ButtonEncryptionClick(Sender: TObject);
- procedure ButtonDecryptionClick(Sender: TObject);
- procedure ButSaveFileClick(Sender: TObject);
- procedure ButInstructionClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form_TI_1: TForm_TI_1;
- implementation
- {$R *.dfm}
- type
- TLanguage = (TRus, TEng);
- function TextToNumbers(text: string; A: Integer): TArray<Integer>;
- var
- i: Integer;
- begin
- SetLength(Result, Length(text));
- for i := 1 to Length(text) do
- begin
- Result[i - 1] := Ord(UpCase(text[i])) - A;
- end;
- end;
- function TextToNumbersDecimation(text: string): TArray<Integer>;
- var
- i: Integer;
- temp: integer;
- begin
- SetLength(Result, Length(text));
- for i := 1 to Length(text) do
- begin
- temp := Ord(text[i]);
- if (temp = 1105) or (temp = 1025) then
- Result[i - 1] := 6
- else
- begin
- if ((temp > 1045) and (temp < 1072)) then
- Result[i - 1] := Ord(UpCase(text[i])) - 1040 + 1;
- if ((temp > 1077) and (temp < 1104)) then
- Result[i - 1] := Ord(UpCase(text[i])) - 1072 + 1;
- if (temp < 1046) then
- Result[i - 1] := Ord(UpCase(text[i])) - 1040;
- if (temp > 1071) and (temp < 1078) then
- Result[i - 1] := Ord(UpCase(text[i])) - 1072;
- end;
- end;
- end;
- function NumbersToText(numbers: TArray<Integer>; A, M: Integer): string;
- var
- i: Integer;
- begin
- Result := '';
- for i := 0 to Length(numbers) - 1 do
- begin
- Result := Result + Chr(((numbers[i] + M) mod M) + A);
- end;
- end;
- function NumbersToTextDecimation(numbers: TArray<Integer>; A, M: Integer): string;
- var
- i: Integer;
- begin
- Result := '';
- for i := 0 to Length(numbers) - 1 do
- begin
- if numbers[i] = 6 then
- Result := Result + 'Ё'
- else
- Result := Result + Chr(((numbers[i] + M) mod M) + A);
- end;
- end;
- function EncryptionVigenere(text, key: string): string;
- var
- numbers, keyNumbers: TArray<Integer>;
- encrypted: TArray<Integer>;
- i: Integer;
- begin
- numbers := TextToNumbersDecimation(text);
- keyNumbers := TextToNumbersDecimation(key);
- SetLength(encrypted, Length(text));
- for i := 0 to Length(text) - 1 do
- encrypted[i] := (numbers[i] + (Ord(key[i mod Length(key) + 1]) - 1040)) mod 33;
- Result := NumbersToTextDecimation(encrypted, 1040, 33);
- end;
- function DecryptionVigenere(encryptedText, key: string): string;
- var
- numbers, keyNumbers: TArray<Integer>;
- decrypted: TArray<Integer>;
- i: Integer;
- begin
- numbers := TextToNumbersDecimation(encryptedText);
- keyNumbers := TextToNumbersDecimation(key);
- SetLength(decrypted, Length(encryptedText));
- for i := 0 to Length(encryptedText) - 1 do
- decrypted[i] := (numbers[i] - (Ord(key[i mod Length(key) + 1]) - Ord('А'))) mod 33;
- Result := NumbersToTextDecimation(decrypted, Ord('А') + 848, 33);
- end;
- function EncryptionDecimationMethod(text: string; key: Integer): string;
- var
- numbers: TArray<Integer>;
- encryptedNumbers: TArray<Integer>;
- i: Integer;
- begin
- numbers := TextToNumbers(text, ord('A'));
- SetLength(encryptedNumbers, Length(numbers));
- for i := 0 to Length(numbers) - 1 do
- encryptedNumbers[i] := (numbers[i] * key) mod 26;
- Result := NumbersToText(encryptedNumbers, Ord('A'), 26);
- end;
- function ModInverse(a, m: Integer): Integer;
- var
- i: Integer;
- begin
- a := a mod m;
- for i := 1 to m do
- if (a * i) mod m = 1 then
- Exit(i);
- Result := -1;
- end;
- function DecryptionDecimationMethod(encryptedText: string; key: Integer): string;
- var
- numbers: TArray<Integer>;
- decryptedNumbers: TArray<Integer>;
- inverseKey: Integer;
- i: Integer;
- begin
- numbers := TextToNumbers(encryptedText, ord('A'));
- inverseKey := ModInverse(key, 26);
- if inverseKey = -1 then
- begin
- ShowMessage('Невозможно найти обратный ключ.');
- Exit('');
- end;
- SetLength(decryptedNumbers, Length(numbers));
- for i := 0 to Length(numbers) - 1 do
- decryptedNumbers[i] := (numbers[i] * inverseKey) mod 26;
- Result := NumbersToText(decryptedNumbers, Ord('A'), 26);
- end;
- procedure CheckAndCorrectText(var Text: TMemo; Lang: TLanguage);
- var
- I, J: Integer;
- Buf: String;
- A: Char;
- B: Integer;
- begin
- for I := 0 to Text.Lines.Count - 1 do
- begin
- Buf := '';
- for J := 1 to Length(Text.Lines[I]) do
- begin
- case (Lang) of
- TRus:
- begin
- A := Text.Lines[i][j];
- B := Ord(A);
- if (B = 1025) or (B = 1105) or ((B >= 1040) and (B <= 1103)) or (B = 32) then
- begin
- Buf := Buf + Text.Lines[I][J];
- end;
- end;
- TEng:
- begin
- if (Text.Lines[I][J] in ['A'..'Z','a'..'z', ' ']) then
- begin
- Buf := Buf + Text.Lines[I][J];
- end;
- end;
- end;
- end;
- Text.Lines[i] := (Buf);
- end;
- end;
- procedure TForm_TI_1.ButInstructionClick(Sender: TObject);
- begin
- ShowMessage('Метод децимаций: текст на английском, ключ - число. Алгоритм Виженера: текст на русском, ключ - слово.');
- end;
- procedure TForm_TI_1.ButOpenFileClick(Sender: TObject);
- var
- Path, InputStr, Error: String;
- F: TextFile;
- IsCorrect: Boolean;
- begin
- if (OpenDialog.Execute) then
- begin
- Path := OpenDialog.FileName;
- AssignFile(F, Path);
- try
- Reset(F);
- try
- while not(EoF(F)) do
- begin
- Readln(F, InputStr);
- if not(InputStr.IsEmpty) then
- begin
- MemoPlainText.Lines.Insert(MemoPlainText.Lines.Count, UTF8ToAnsi(InputStr));
- end;
- end;
- finally
- CloseFile(F);
- end;
- except
- IsCorrect := false;
- Error := 'Ошибка при считывании данных из файла. ';
- ShowMessage(Error);
- end;
- end;
- end;
- procedure TForm_TI_1.ButSaveFileClick(Sender: TObject);
- Var
- F: TextFile;
- Path: String;
- i: integer;
- begin
- If SaveDialog.Execute() Then
- Begin
- Path := SaveDialog.FileName;
- AssignFile(F, Path);
- Try
- Rewrite(F);
- Try
- for I := 0 to MemoCiphertext.Lines.Count - 1 do
- Writeln(F, MemoCiphertext.Lines[i]);
- Finally
- CloseFile(F);
- End;
- Except
- ShowMessage('Ошибка при записи!');
- End;
- End;
- end;
- procedure TForm_TI_1.ButtonDecryptionClick(Sender: TObject);
- var
- KeyDecimation, i: Integer;
- KeyVigenere, Temp: String;
- begin
- MemoCipherText.Clear;
- if (RadioButtonDecimationMethod.Checked) then
- begin
- CheckAndCorrectText(MemoPlaintext, TEng);
- try
- KeyDecimation := StrToInt(EditKey.Text);
- for I := 0 to MemoPlaintext.Lines.Count - 1 do
- begin
- Temp := DecryptionDecimationMethod(MemoPlaintext.Lines[i], KeyDecimation);
- MemoCiphertext.Lines.Insert(MemoCiphertext.Lines.Count, Temp);
- end;
- except
- ShowMessage('Ошибка! Проверьте значение ключа!');
- end;
- end
- else
- begin
- if (RadioButtonVigenereCipher.Checked) then
- begin
- CheckAndCorrectText(MemoPlaintext, TRus);
- KeyVigenere := EditKey.Text;
- for I := 0 to MemoPlaintext.Lines.Count - 1 do
- begin
- Temp := DecryptionVigenere(MemoPlaintext.Lines[i], KeyVigenere);
- MemoCiphertext.Lines.Insert(MemoCiphertext.Lines.Count, Temp);
- end;
- end
- else
- ShowMessage('Не выбран шифр!');
- end;
- end;
- procedure TForm_TI_1.ButtonEncryptionClick(Sender: TObject);
- var
- KeyDecimation, i: Integer;
- KeyVigenere, Temp, A: String;
- begin
- MemoCipherText.Clear;
- if (RadioButtonDecimationMethod.Checked) then
- begin
- CheckAndCorrectText(MemoPlaintext, TEng);
- try
- KeyDecimation := StrToInt(EditKey.Text);
- for I := 0 to MemoPlaintext.Lines.Count - 1 do
- begin
- Temp := EncryptionDecimationMethod((MemoPlaintext.Lines[i]), KeyDecimation);
- MemoCiphertext.Lines.Insert(MemoCiphertext.Lines.Count, Temp);
- end;
- except
- ShowMessage('Ошибка! Проверьте значение ключа!');
- end;
- end
- else
- begin
- if (RadioButtonVigenereCipher.Checked) then
- begin
- CheckAndCorrectText(MemoPlaintext, TRus);
- KeyVigenere := EditKey.Text;
- for I := 0 to MemoPlaintext.Lines.Count - 1 do
- begin
- A := MemoPlaintext.Lines[i];
- Temp := EncryptionVigenere(A, KeyVigenere);
- MemoCiphertext.Lines.Insert(MemoCiphertext.Lines.Count, Temp);
- end;
- end
- else
- ShowMessage('Не выбран шифр!');
- end;
- end;
- procedure TForm_TI_1.EditKeyChange(Sender: TObject);
- begin
- if (EditKey.Text <> '') then
- begin
- ButtonEncryption.Enabled := true;
- ButtonDecryption.Enabled := true;
- end
- else
- begin
- ButtonEncryption.Enabled := false;
- ButtonDecryption.Enabled := false;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement