Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Numerics;
- using System.Security.Cryptography;
- namespace L5
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Job1();
- Job2();
- }
- private static void Job1()
- {
- Console.WriteLine("Job1");
- int p = 0, q = 0;
- int e = 65537;
- bool ok = true;
- while (ok)
- {
- p = (int)RandomIntegerBelow(100);
- ok = !IsPrime(p);
- }
- Console.WriteLine("P: "+p);
- ok = true;
- while (ok)
- {
- q = (int)RandomIntegerBelow(100);
- ok = !IsPrime(q);
- }
- Console.WriteLine("Q: "+q);
- int n = (p - 1) * (q - 1);
- int d = 0;
- for (int i = 1; i < 10000; i++)
- {
- if ((e * i) % n == 0)
- {
- d = i;
- break;
- }
- }
- Console.WriteLine("D: "+d);
- }
- private static void Job2()
- {
- int p = 0, q = 0, e = 0, d = 0;
- bool ok = true;
- while (ok)
- {
- p = (int)RandomIntegerBelow(100);
- ok = !IsPrime(p) || !IsPrime(2 * p + 1);
- }
- ok = true;
- Console.WriteLine("P: "+p);
- while (ok)
- {
- q = (int)RandomIntegerBelow(100);
- ok = !IsPrime(q) || !IsPrime(2 * q + 1);
- }
- ok = true;
- Console.WriteLine("Q: "+q);
- int n = (p - 1) * (q - 1);
- while (ok)
- {
- e = (int)RandomIntegerBelow(100);
- if (e % 2 == 1 && GCD(e, n) == 1)
- {
- ok = false;
- }
- }
- Console.WriteLine("E: "+e);
- for (int i = 1; i < n * e; i++)
- {
- if (e * i % n == 0)
- {
- d = i;
- Console.WriteLine("D: " + d);
- break;
- }
- }
- }
- private static bool IsPrime(int number)
- {
- if ((number & 1) == 0)
- {
- return (number == 2);
- }
- int limit = (int)Math.Sqrt(number);
- for (int i = 3; i <= limit; i += 2)
- {
- if ((number % i) == 0)
- {
- return false;
- }
- }
- return true;
- }
- public static BigInteger RandomIntegerBelow(BigInteger N)
- {
- var rng = new RNGCryptoServiceProvider();
- byte[] bytes = N.ToByteArray();
- BigInteger R;
- do
- {
- rng.GetBytes(bytes);
- bytes[bytes.Length - 1] &= (byte)0x7F;
- R = new BigInteger(bytes);
- } while (R >= N);
- return R;
- }
- private static int GCD(int a, int b)
- {
- while (a != 0 && b != 0)
- {
- if (a > b)
- a %= b;
- else
- b %= a;
- }
- return a == 0 ? b : a;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement