Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. Console.WriteLine("Zadanie 1"); Console.WriteLine();
  2.             Stopwatch stopwatch = Stopwatch.StartNew();
  3.             //Zadanie 1
  4.             int bity = 2048;
  5.             Console.WriteLine("Bezpieczeństwo: " + bity + " bitów");Console.WriteLine();
  6.  
  7.             Random rnd = new SecureRandom();
  8.  
  9.             BigInteger p = BigInteger.ProbablePrime(bity/2, rnd);
  10.             BigInteger q = BigInteger.ProbablePrime(bity/2, rnd);
  11.             p = p.Abs();
  12.             q = q.Abs();
  13.  
  14.             BigInteger n = p.Multiply(q);
  15.  
  16.             p = p.Subtract(BigInteger.One);
  17.             q = q.Subtract(BigInteger.One);
  18.  
  19.             BigInteger phi = p.Multiply(q);
  20.            
  21.             BigInteger publiczny;
  22.  
  23.            
  24.             do publiczny = BigInteger.ProbablePrime(phi.BitLength, rnd);
  25.             while (publiczny.CompareTo(BigInteger.One) <= 0 || publiczny.CompareTo(phi) >= 0 || !publiczny.Gcd(phi).Equals(BigInteger.One));
  26.             BigInteger prywatny = publiczny.ModInverse(phi);
  27.  
  28.             Console.WriteLine();
  29.  
  30.             Console.WriteLine("klucz publiczny: " + publiczny.ToString()); Console.WriteLine();
  31.             Console.WriteLine("klucz prywatny: " + prywatny.ToString()); Console.WriteLine();
  32.  
  33.             string tekst = "Daje Ci tekst do zaszyfrowania za pomoca RSA";
  34.             Console.WriteLine("Tekst szyfrowany: " + tekst);
  35.             byte[] textbajty = Encoding.ASCII.GetBytes(tekst); Console.WriteLine();
  36.             BigInteger textBig = new BigInteger(textbajty);
  37.  
  38.             BigInteger encrypt = textBig.ModPow(publiczny, n);
  39.  
  40.  
  41.             BigInteger decrypt = encrypt.ModPow(prywatny, n);
  42.  
  43.             byte[] deszyfr = decrypt.ToByteArray();
  44.  
  45.             string tekstd = Encoding.ASCII.GetString(deszyfr);
  46.             Console.WriteLine("tekst odszyfrowany: " + tekstd); Console.WriteLine();
  47.  
  48.             BigInteger sign = textBig.ModPow(prywatny, n);
  49.             BigInteger check = sign.ModPow(publiczny, n);
  50.  
  51.  
  52.             if (textBig.CompareTo(check) == 0)
  53.             {
  54.                 Console.WriteLine("Podpis sie zgadza");
  55.             }
  56.             else Console.WriteLine("Something is no yes");
  57.  
  58.             Console.WriteLine();
  59.  
  60.             stopwatch.Stop();
  61.  
  62.             Console.WriteLine(stopwatch.Elapsed); Console.WriteLine();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement