Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Globalization;
  7. using System.Numerics;
  8. using System.Collections;
  9. using System.IO;
  10.  
  11. namespace Generator
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. BigInteger p = new BigInteger(5000903);
  18. BigInteger q = new BigInteger(4900789);
  19. BigInteger n = p * q;
  20.  
  21. BigInteger m = (p - 1) * (q - 1); // nazywane rowniez d
  22. BigInteger e; // powinna byc wzglednie pierwsza do d=(p-1)(q-1) - ale pomijamy
  23.  
  24. // losowanie e
  25. do
  26. {
  27. e = RandomIntegerBelow(m);
  28. }
  29. while (e < 1);
  30.  
  31. // ziarno, x0
  32. BigInteger x0 = RandomIntegerBelow(n); // ziarno
  33.  
  34. String generated = "";
  35.  
  36. BigInteger x = x0;
  37. const int range = 20000;
  38. for (int i = 0; i < range; ++i)
  39. {
  40. BigInteger xi = x;
  41. xi = BigInteger.ModPow(xi, e, n);
  42.  
  43. generated += xi & 1;
  44.  
  45. x = xi;
  46. }
  47.  
  48. using (StreamWriter outputFile = new StreamWriter("generated.txt"))
  49. {
  50. outputFile.Write(generated);
  51. }
  52.  
  53. // Testy
  54. testPojedynczychBitow(generated);
  55. testDlugiejSerii(generated);
  56. testPokerowy(generated);
  57. testSerii(generated);
  58.  
  59. }
  60.  
  61. public static BigInteger RandomIntegerBelow(BigInteger N)
  62. {
  63. byte[] bytes = N.ToByteArray();
  64. BigInteger R;
  65.  
  66. do
  67. {
  68. (new Random()).NextBytes(bytes);
  69. bytes[bytes.Length - 1] &= (byte)0x7F; //force sign bit to positive
  70. R = new BigInteger(bytes);
  71. } while (R >= N);
  72.  
  73. return R;
  74. }
  75.  
  76. //
  77.  
  78. public static void testPojedynczychBitow(String klucz)
  79. {
  80. int wystapienia = 0;
  81.  
  82. foreach(char znak in klucz)
  83. {
  84. if (znak == '0')
  85. wystapienia++;
  86. }
  87.  
  88. if (wystapienia > 9725 && wystapienia < 10275)
  89. Console.WriteLine("[Test pojedynczych bitow] Pozytywny");
  90. else
  91. Console.WriteLine("[Test pojedynczych bitow] Negatywny");
  92. }
  93.  
  94. public static void testDlugiejSerii(String klucz)
  95. {
  96. int zera = 0;
  97. int jedynki = 0;
  98.  
  99.  
  100. foreach (char znak in klucz)
  101. {
  102. if (znak == '0')
  103. {
  104. zera++;
  105. jedynki = 0;
  106. }
  107. else
  108. {
  109. zera = 0;
  110. jedynki++;
  111. }
  112. }
  113.  
  114. if (zera < 26 && jedynki < 26)
  115. Console.WriteLine("[Test dlugiej serii] Pozytywny");
  116. else
  117. Console.WriteLine("[Test dlugiej serii] Negatywny");
  118. }
  119.  
  120. public static void testPokerowy(String klucz)
  121. {
  122. int blok = 4;
  123. var ciagi = new Dictionary<String, int>();
  124.  
  125. for (int i = 0; i < klucz.Length; i += blok)
  126. {
  127. String ciag = "";
  128.  
  129. for(int j = 0; j < blok; ++j)
  130. {
  131. ciag += klucz[i + j];
  132. }
  133.  
  134. try
  135. {
  136. ciagi[ciag]++;
  137. }
  138. catch (KeyNotFoundException)
  139. {
  140. ciagi[ciag] = 1;
  141. }
  142. }
  143.  
  144. Console.WriteLine("[Test Pokerowy " + blok + " segmentowy] Wyniki:");
  145. /*
  146. foreach (var ciag in ciagi)
  147. {
  148. Console.WriteLine(ciag.Key + ": " + ciag.Value);
  149. }
  150. */
  151.  
  152. double suma = 0;
  153. foreach (var ciag in ciagi)
  154. {
  155. suma += Math.Pow(ciag.Value, 2);
  156. }
  157.  
  158. double x = 16.0 / 5000.0 * suma - 5000.0;
  159.  
  160. Console.WriteLine("Wynik testu pokerowego: " + x);
  161. if (x > 2.16 && x < 46.17)
  162. {
  163. Console.WriteLine("Test pozytywny");
  164. }
  165. else
  166. {
  167. Console.WriteLine("Test negatywny");
  168. }
  169. }
  170.  
  171. public static void testSerii(String klucz)
  172. {
  173. var serie = new Dictionary<int, int>();
  174.  
  175. int zera = 0;
  176.  
  177. foreach (char znak in klucz)
  178. {
  179. if (znak == '0')
  180. {
  181. zera++;
  182. }
  183. else if(zera > 0)
  184. {
  185. try
  186. {
  187. serie[zera]++;
  188. }
  189. catch (KeyNotFoundException)
  190. {
  191. serie[zera] = 1;
  192. }
  193.  
  194. zera = 0;
  195. }
  196. }
  197.  
  198. Console.WriteLine("[Test Serii] Wyniki:");
  199. foreach (var seria in serie)
  200. {
  201. Console.Write(seria.Key + ": " + seria.Value + " ");
  202.  
  203. if (seriaPrzedzialy(seria.Key, seria.Value))
  204. Console.Write("Pozytywny");
  205. else
  206. Console.Write("Negatywny");
  207.  
  208. Console.WriteLine();
  209. }
  210. }
  211.  
  212. public static bool seriaPrzedzialy(int i, int j)
  213. {
  214. switch (i)
  215. {
  216. case 1:
  217. return j > 2315 && j < 2685;
  218. case 2:
  219. return j > 1114 && j < 1386;
  220. case 3:
  221. return j > 527 && j < 723;
  222. case 4:
  223. return j > 240 && j < 384;
  224. case 5:
  225. return j > 103 && j < 209;
  226. default:
  227. return j > 103 && j < 209;
  228. }
  229. }
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement