Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
- type
- TForm1 = class(TForm)
- memoEncrypt: TMemo;
- memoDecrypt: TMemo;
- Edit1: TEdit;
- Button1: TButton;
- Button2: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- type
- ChArr = array of char;
- function ArrToStr(TempArr: ChArr; Str: string): string;
- var
- i, j: Integer;
- TempStr: string;
- begin
- SetLength(TempArr, Length(Str));
- for i := 0 to Length(Str) - 1 do
- TempStr := TempStr + TempArr[i];
- ArrToStr := TempStr;
- end;
- function NumerateStr(Str: string; Key: Integer): string;
- var
- i, j: Integer;
- NewArr: ChArr;
- NewStr: string;
- begin
- SetLength(NewArr, Length(Str));
- i := 1;
- while (i <= Length(Str)) do
- begin
- for j := 1 to Key do
- begin
- NewArr[i - 1] := chr(j);
- inc(i);
- end;
- for j := Key - 1 downto 2 do
- begin
- NewArr[i - 1] := chr(j);
- inc(i);
- end;
- end;
- NumerateStr := ArrToStr(NewArr, Str);
- end;
- function Encrypt(Str: string; SourceStr: string; Key: integer): string;
- var
- i, j, m: Integer;
- NewStr: string;
- begin
- i := 1;
- j := 1;
- m := 1;
- NewStr := SourceStr;
- while (j <= Key) do
- begin
- while (i <= Length(Str)) do
- begin
- if (Str[i] = chr(j)) then
- begin
- NewStr[m] := SourceStr[i];
- inc(m);
- end;
- inc(i);
- end;
- i := 1;
- inc(j);
- end;
- Encrypt := NewStr;
- end;
- function Decrypt(NumStr: string; SourceStr: string; Key: integer): string;
- var
- i, j, m: Integer;
- begin
- i := 1;
- j := 1;
- m := 1;
- while (j <= Key) do
- begin
- while (i <= Length(SourceStr)) do
- begin
- if (NumStr[i] = chr(j)) then
- begin
- NumStr[i] := SourceStr[m];
- inc(m);
- end;
- inc(i);
- end;
- i := 1;
- inc(j);
- end;
- Decrypt := NumStr;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- var
- SourceStr, NumStr, NewStr: string;
- begin
- SourceStr := memoEncrypt.Text;
- NumStr := NumerateStr(SourceStr, strtoint(edit1.Text));
- NewStr := Encrypt(NumStr, SourceStr, strtoint(edit1.Text));
- memoDecrypt.Text := NewStr;
- end;
- procedure TForm1.Button2Click(Sender: TObject);
- var
- SourceStr, NumStr: string;
- begin
- SourceStr := memoDecrypt.Text;
- NumStr := NumerateStr(SourceStr, strtoint(edit1.Text));
- NumStr := Decrypt(NumStr, SourceStr, strtoint(edit1.Text));
- memoDecrypt.Text := NumStr;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement