Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Console.WriteLine("Zadanie 1"); Console.WriteLine();
- Stopwatch stopwatch = Stopwatch.StartNew();
- //Zadanie 1
- int bity = 2048;
- Console.WriteLine("Bezpieczeństwo: " + bity + " bitów");Console.WriteLine();
- Random rnd = new SecureRandom();
- BigInteger p = BigInteger.ProbablePrime(bity/2, rnd);
- BigInteger q = BigInteger.ProbablePrime(bity/2, rnd);
- p = p.Abs();
- q = q.Abs();
- BigInteger n = p.Multiply(q);
- p = p.Subtract(BigInteger.One);
- q = q.Subtract(BigInteger.One);
- BigInteger phi = p.Multiply(q);
- BigInteger publiczny;
- do publiczny = BigInteger.ProbablePrime(phi.BitLength, rnd);
- while (publiczny.CompareTo(BigInteger.One) <= 0 || publiczny.CompareTo(phi) >= 0 || !publiczny.Gcd(phi).Equals(BigInteger.One));
- BigInteger prywatny = publiczny.ModInverse(phi);
- Console.WriteLine();
- Console.WriteLine("klucz publiczny: " + publiczny.ToString()); Console.WriteLine();
- Console.WriteLine("klucz prywatny: " + prywatny.ToString()); Console.WriteLine();
- string tekst = "Daje Ci tekst do zaszyfrowania za pomoca RSA";
- Console.WriteLine("Tekst szyfrowany: " + tekst);
- byte[] textbajty = Encoding.ASCII.GetBytes(tekst); Console.WriteLine();
- BigInteger textBig = new BigInteger(textbajty);
- BigInteger encrypt = textBig.ModPow(publiczny, n);
- BigInteger decrypt = encrypt.ModPow(prywatny, n);
- byte[] deszyfr = decrypt.ToByteArray();
- string tekstd = Encoding.ASCII.GetString(deszyfr);
- Console.WriteLine("tekst odszyfrowany: " + tekstd); Console.WriteLine();
- BigInteger sign = textBig.ModPow(prywatny, n);
- BigInteger check = sign.ModPow(publiczny, n);
- if (textBig.CompareTo(check) == 0)
- {
- Console.WriteLine("Podpis sie zgadza");
- }
- else Console.WriteLine("Something is no yes");
- Console.WriteLine();
- stopwatch.Stop();
- Console.WriteLine(stopwatch.Elapsed); Console.WriteLine();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement