Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace MeyhodsPrepearing
- {
- class Program
- {
- static bool Prime(long n)
- {
- for (long i = 2; i <= Math.Sqrt(n); i++)
- if (n % i == 0) return false;
- return true;
- }
- static long GCD(long e, long totient)
- {
- long temp = 0;
- while (totient != 0)
- {
- temp = e % totient;
- e = totient;
- totient = temp;
- }
- return e;
- }
- static long BinPow(long a, long b)
- {
- if (b == 0) return 1;
- else
- {
- if (b % 2 == 1) return a * BinPow(a, b - 1);
- else return BinPow(a * a, b / 2);
- }
- }
- static void Main(string[] args)
- {
- int p = 0;
- Console.Write("First prime: ");
- do p = Convert.ToInt32(Console.ReadLine()); while (!Prime(p));
- int q = 0;
- Console.Write("Second number: ");
- do q = Convert.ToInt32(Console.ReadLine()); while (!Prime(q));
- long n = p * q;
- long fi = (p - 1) * (q - 1);
- long e = 2;
- Console.Write("E: ");
- long d = (1 + (2 * fi)) / e;
- Console.Write("Message: ");
- long msg1 = Convert.ToInt32(Console.ReadLine());
- long c = BinPow(msg1, e) % n;
- Console.Write("Encrypted: ");
- Console.WriteLine(c);
- long msg2 = BinPow(c, d) % n;
- Console.Write("Original: ");
- Console.WriteLine(msg2);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement