Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.35 KB | None | 0 0
  1. unit Unit5;
  2.  
  3. interface
  4.  
  5.   function Encrypt(str: string; const Coolkey: string):string;
  6.   function Decrypt(str, key: string): string;
  7.   function GetStr(var str: String): String;
  8.  
  9. implementation
  10.  
  11. function LowerCase(Str: string): string;
  12. var
  13.   i: integer;
  14. begin
  15.   Result := Str;
  16.   for i := 1 to length(Result) do
  17.   begin
  18.      if (Result[i] in ['А'..'Я']) then
  19.         Result[i] := chr(ord(Result[i]) + 32);
  20.      if Result[i] = 'Ё' then
  21.         Result[i] := 'ё';
  22.   end;
  23. end;
  24.  
  25. function Encrypt(str: string; const Coolkey: string):string;
  26. const
  27.    SizeAlphabet = 33;
  28.    Alphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
  29. var
  30.    i, j, Position, KeyCounter, Offset: integer;
  31.    temp, key: string;
  32. begin
  33.    str := GetStr(Str);
  34.    Key := Coolkey;
  35.    KeyCounter := 0;
  36.    for i := 1 to length(str) do
  37.    begin
  38.       inc(KeyCounter);
  39.       if KeyCounter > length(key) then
  40.       begin
  41.          KeyCounter := 1;
  42.          for  j := 1 to Length(Key) do
  43.             Key[j] := Alphabet[pos(Key[j], Alphabet) + 1];
  44.       end;
  45.       Position := pos(str[i], Alphabet) + pos(key[KeyCounter], Alphabet) - 1;
  46.       if Position > SizeAlphabet then
  47.          Position := Position - SizeAlphabet;
  48.       Result:=Result + Alphabet[Position];
  49.    end;
  50. end;
  51.  
  52.  
  53. function Decrypt(str, key: string): string;
  54. const
  55.    SizeAlphabet = 33;
  56.    Alphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
  57. var
  58.    i, j, Position, KeyCounter: integer;
  59.    Temp: string;
  60. begin
  61.    Str := GetStr(Str);
  62.    Str := LowerCase(str);
  63.    Key := LowerCase(key);
  64.    KeyCounter := 0;
  65.    for i := 1 to length(str) do
  66.    begin
  67.       inc(KeyCounter);
  68.       if KeyCounter > length(Key) then
  69.       begin
  70.          KeyCounter := 1;
  71.          for  j := 1 to Length(Key) do
  72.             Key[j] := Alphabet[pos(Key[j], Alphabet) + 1];
  73.       end;
  74.       Position := pos(Str[i], Alphabet) - pos(Key[KeyCounter], Alphabet) + 1;
  75.       if Position <= 0 then
  76.          Position := Position + SizeAlphabet;
  77.       Result := Result + Alphabet[Position];
  78.    end;
  79. end;
  80.  
  81. function GetStr(var str: String): String;
  82. var
  83.   i: Integer;
  84.   newStr: String;
  85. begin
  86.   for i := 1 to Length(str) do
  87.   begin
  88.     if (ord(str[i]) >= 1072) and (ord(str[i]) <= 1103) or (ord(str[i]) = 1105)then
  89.       newStr := newStr + str[i];
  90.     result := newStr;
  91.   end;
  92. end;
  93.  
  94. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement