ademosh

Untitled

Mar 27th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. let testN=100;
  2. let roundCount=1000;
  3. let message="абракадабра";
  4. let m=15;
  5.  
  6. const alphaFactory= (symbol,code) => {
  7. return {
  8. symbol,
  9. code
  10. }
  11. }
  12. let alpha=[];
  13. alpha.push(alphaFactory('й','10'));
  14. alpha.push(alphaFactory('ц','10'));
  15. alpha.push(alphaFactory('у','10'));
  16. alpha.push(alphaFactory('к','10'));
  17. alpha.push(alphaFactory('е','10'));
  18. alpha.push(alphaFactory('н','10'));
  19. alpha.push(alphaFactory('г','10'));
  20. alpha.push(alphaFactory('ш','10'));
  21. alpha.push(alphaFactory('щ','10'));
  22. alpha.push(alphaFactory('з','11'));
  23. alpha.push(alphaFactory('х','111'));
  24. alpha.push(alphaFactory('ъ','112'));
  25. alpha.push(alphaFactory('ф','113'));
  26. alpha.push(alphaFactory('ы','114'));
  27. alpha.push(alphaFactory('в','115'));
  28. alpha.push(alphaFactory('а','116'));
  29. alpha.push(alphaFactory('п','117'));
  30. alpha.push(alphaFactory('р','118'));
  31. alpha.push(alphaFactory('о','119'));
  32. alpha.push(alphaFactory('л','120'));
  33. alpha.push(alphaFactory('д','121'));
  34. alpha.push(alphaFactory('ж','122'));
  35. alpha.push(alphaFactory('э','123'));
  36. alpha.push(alphaFactory('я','124'));
  37. alpha.push(alphaFactory('ч','125'));
  38. alpha.push(alphaFactory('с','126'));
  39. alpha.push(alphaFactory('м','127'));
  40. alpha.push(alphaFactory('и','128'));
  41. alpha.push(alphaFactory('т','129'));
  42. alpha.push(alphaFactory('ь','130'));
  43. alpha.push(alphaFactory('б','131'));
  44. alpha.push(alphaFactory('ю','132'));
  45. alpha.push(alphaFactory('ё','133'));
  46.  
  47.  
  48. //Необходимые функции
  49. const breakeNumber = number => {
  50. let buf=0;
  51. while (number%2===0) {
  52. buf++;
  53. number=number/2;
  54. }
  55. return [buf,number]; //buf - степень двойки s, t; number - t
  56. }
  57.  
  58. const checkSimple = (n,roundCount) => {
  59. let buf=n-1;
  60. let flag=0;
  61. let s=breakeNumber(buf)[0];
  62. let d=breakeNumber(buf)[1];
  63. for (let i=0;i<roundCount;++i) {
  64. let a=Math.floor((Math.random()*(n-4))+2);
  65. if (Math.pow(a,d)%n===1) {
  66. return true;
  67. }
  68. for (let j=0;j<s-1;++j) {
  69. let buf=Math.pow(a,(d*Math.pow(2,j)));
  70. if (buf%n===-1) return true;
  71. }
  72. }
  73. return false;
  74. }
  75.  
  76. const checkIsPervSqrt = (A,P) => {
  77. let buf=[];
  78. for (let i=1;i<P;++i){
  79. let bufNumb=Math.pow(A,i)%P;
  80. if (buf.includes(bufNumb)) { return false;}
  81. else {
  82. buf.push(bufNumb); }
  83. }
  84. return true;
  85. }
  86.  
  87. const findSqrt=P => {
  88. for (let i=1;i<P;++i) {
  89. if (checkIsPervSqrt(i,P)) {
  90. return i;
  91. }
  92. }
  93. }
  94.  
  95. const genRand = (min,max) => {
  96. return Math.floor(Math.random()*(max-min)+min);
  97. }
  98.  
  99.  
  100. const codeMessage = (message) => {
  101. let outputString=[];
  102. for (let i=0;i<message.length;++i) {
  103. let buf=message[i];
  104. console.log(buf);
  105. for (let j=0;j<alpha.length;++j) {
  106. if (alpha[j].symbol===buf) {
  107. outputString+=alpha[j].code;
  108. }
  109. }
  110. }
  111. return outputString;
  112. }
  113.  
  114.  
  115. //НОД
  116. const gcd=(a,b) => {
  117. if (b==0) return a;
  118. return gcd(b,a%b);
  119. }
  120.  
  121. const getK = (P) => {
  122. let buf=Math.floor(Math.random()*P);
  123. while (gcd(P,buf)!=1) {
  124. buf=Math.floor(Math.random()*P);
  125. }
  126. return buf;
  127. }
  128. //Основная часть
  129. //Кодировка символов
  130. console.log(codeMessage(message));
  131. console.log(alpha);
  132. //Генерируем большое просто число больше сотки с проверкой на простоту
  133. //Далее для него же находим первообразную в том же
  134.  
  135.  
  136. let A,P;
  137. while (typeof(A)==='undefined'||typeof(P)==='undefined') {
  138. P=genRand(80,200);
  139. if (checkSimple(P,100)) {
  140. A=findSqrt(P);
  141. }
  142. }
  143. console.log(`A=${A} P=${P}`);
  144. ///Первый пользователь
  145. let x=genRand(2,P/2);
  146. let y=Math.pow(A,x)%P;
  147. console.log(`m=${m} x= ${x} y=${y}`);
  148. //шифрование
  149. let k=getK(10);
  150. console.log(`k= ${k}`);
  151. let gamma=Math.pow(A,k)%P;
  152. let betta=m*Math.pow(y,k)%P;
  153. console.log(gamma);
  154. console.log(betta);
  155. console.log((betta*Math.pow(gamma,P-1-x))%P);
Add Comment
Please, Sign In to add comment