Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Numerics;
  6. using System.Security.Cryptography;
  7.  
  8. namespace L5
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //Job1();
  15. Job2();
  16. }
  17.  
  18. private static void Job1()
  19. {
  20. Console.WriteLine("Job1");
  21. int p = 0, q = 0;
  22. int e = 65537;
  23. bool ok = true;
  24.  
  25. while (ok)
  26. {
  27. p = (int)RandomIntegerBelow(100);
  28. ok = !IsPrime(p);
  29. }
  30. Console.WriteLine("P: "+p);
  31.  
  32. ok = true;
  33.  
  34. while (ok)
  35. {
  36. q = (int)RandomIntegerBelow(100);
  37. ok = !IsPrime(q);
  38. }
  39. Console.WriteLine("Q: "+q);
  40.  
  41. int n = (p - 1) * (q - 1);
  42. int d = 0;
  43. for (int i = 1; i < 10000; i++)
  44. {
  45. if ((e * i) % n == 0)
  46. {
  47. d = i;
  48. break;
  49. }
  50. }
  51.  
  52. Console.WriteLine("D: "+d);
  53. }
  54.  
  55. private static void Job2()
  56. {
  57. int p = 0, q = 0, e = 0, d = 0;
  58. bool ok = true;
  59.  
  60. while (ok)
  61. {
  62. p = (int)RandomIntegerBelow(100);
  63. ok = !IsPrime(p) || !IsPrime(2 * p + 1);
  64. }
  65. ok = true;
  66. Console.WriteLine("P: "+p);
  67.  
  68. while (ok)
  69. {
  70. q = (int)RandomIntegerBelow(100);
  71. ok = !IsPrime(q) || !IsPrime(2 * q + 1);
  72. }
  73. ok = true;
  74. Console.WriteLine("Q: "+q);
  75.  
  76. int n = (p - 1) * (q - 1);
  77.  
  78. while (ok)
  79. {
  80. e = (int)RandomIntegerBelow(100);
  81. if (e % 2 == 1 && GCD(e, n) == 1)
  82. {
  83. ok = false;
  84. }
  85. }
  86. Console.WriteLine("E: "+e);
  87.  
  88. for (int i = 1; i < n * e; i++)
  89. {
  90. if (e * i % n == 0)
  91. {
  92. d = i;
  93. Console.WriteLine("D: " + d);
  94. break;
  95. }
  96. }
  97. }
  98.  
  99. private static bool IsPrime(int number)
  100. {
  101. if ((number & 1) == 0)
  102. {
  103. return (number == 2);
  104. }
  105.  
  106. int limit = (int)Math.Sqrt(number);
  107.  
  108. for (int i = 3; i <= limit; i += 2)
  109. {
  110. if ((number % i) == 0)
  111. {
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. public static BigInteger RandomIntegerBelow(BigInteger N)
  118. {
  119. var rng = new RNGCryptoServiceProvider();
  120. byte[] bytes = N.ToByteArray();
  121. BigInteger R;
  122.  
  123. do
  124. {
  125. rng.GetBytes(bytes);
  126. bytes[bytes.Length - 1] &= (byte)0x7F;
  127. R = new BigInteger(bytes);
  128. } while (R >= N);
  129.  
  130. return R;
  131. }
  132.  
  133. private static int GCD(int a, int b)
  134. {
  135. while (a != 0 && b != 0)
  136. {
  137. if (a > b)
  138. a %= b;
  139. else
  140. b %= a;
  141. }
  142.  
  143. return a == 0 ? b : a;
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement