fork download
  1. program main;
  2. const n=65;
  3. type A=array[0..n] of char;
  4. const alpha:A=('А','Б','В','Г','Д','Е','Ё','Ж','З','И',
  5. 'Й', 'К','Л','М','Н','О','П','Р','С','Т',
  6. 'У','Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ы','Ь',
  7. 'Э','Ю','Я', 'а','б','в','г','д','е','ё',
  8. 'ж','з','и','й','к','л','м','н','о','п',
  9. 'р','с','т','у','ф','х','ц','ч','ш','щ',
  10. 'ъ','ы', 'ь','э','ю','я');
  11.  
  12. {Поиск индекса символа в алфавите}
  13. function indexOf(c:char; arr:A):integer;
  14. var i:integer;
  15. index:integer;
  16. begin
  17. for i:=1 to n do
  18. if (c=arr[i]) then
  19. index:=i;
  20. indexOf:=index;
  21. end;
  22.  
  23. {Поиск наибольшего общего делителя}
  24. function gcd(a,b:integer):integer;
  25. begin
  26. while(a>0) and (b>0) do
  27. begin
  28. if a>b then
  29. a:=a mod b
  30. else
  31. b:=b mod a;
  32. end;
  33. gcd:=a+b;
  34. end;
  35. {Поиск обратного элемента в кольце по mod n}
  36. function reversed(m:integer):integer;
  37. var r:integer;
  38. i:integer;
  39. begin
  40. m:=m mod n;
  41. r:=-1;
  42. for i:=1 to n do
  43. if((i*m) mod n = 1) then
  44. begin
  45. r:=i;
  46. break;
  47. end;
  48. reversed:=r;
  49. end;
  50.  
  51. {Шифрование и дешифрование применяет стандартные формулы
  52.   аффинного шифра}
  53. function encrypt(txt:string; m,k:integer):string;
  54. var i:integer;
  55. c:char;
  56. index:integer;
  57. begin
  58. m:=m mod n;
  59. k:=k mod n;
  60. if (gcd(m,n)<>1) then
  61. begin
  62. encrypt:='для однозначного шифрования m должен быть взаимно прост с n';
  63. exit;
  64. end;
  65. for i:=1 to length(txt) do
  66. begin
  67. c:=txt[i];
  68. index:=indexOf(c,alpha);
  69. index:=(m*index+k) mod n;
  70. txt[i]:=alpha[index];
  71. end;
  72. encrypt:=txt;
  73. end;
  74.  
  75. function decrypt(txt:string; m,k:integer):string;
  76. var i:integer;
  77. c:char;
  78. index:integer;
  79. reversed_m:integer;
  80. begin
  81. k:=k mod n;
  82. reversed_m:=reversed(m);
  83. for i:=1 to length(txt) do
  84. begin
  85. c:=txt[i];
  86. index:=indexOf(c,alpha);
  87. index:=(reversed_m*(index-k) mod n);
  88. txt[i]:=alpha[index];
  89. end;
  90. decrypt:=txt;
  91. end;
  92. var m:integer;
  93. k:integer;
  94. txt:string;
  95. begin
  96. txt:='Шуршалка';
  97. {Параметры ключа (m,k)}
  98. m:=11;
  99. k:=4;
  100. writeln('Шифрование');
  101. writeln('***>',encrypt(txt,m,k));
  102. writeln('Дешифрование');
  103. writeln('***>',decrypt(txt,m,k));
  104. end.
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Free Pascal Compiler version 2.2.0 [2009/11/16] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling prog.pas
prog.pas(4,22) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(4,27) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(4,32) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(4,37) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(4,42) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(4,47) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(4,52) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(4,57) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(4,62) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(4,67) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(5,22) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(5,28) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(5,33) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(5,38) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(5,43) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(5,48) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(5,53) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(5,58) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(5,63) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(5,68) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(6,22) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(6,27) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(6,32) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(6,37) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(6,42) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(6,47) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(6,52) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(6,57) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(6,62) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(6,67) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(7,22) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(7,27) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(7,32) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(7,38) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(7,43) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(7,48) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(7,53) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(7,58) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(7,63) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(7,68) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,22) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,27) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,32) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,37) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,42) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,47) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,52) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,57) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,62) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,67) Error: Incompatible types: got "Constant String" expected "Char"
prog.pas(8,67) Fatal: There were 50 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)
stdout
Standard output is empty