private MyBigInt CalculateE(MyBigInt m) { MyBigInt e = m - 1; while (true) { if (e.IsPrimeNumber() && (e < m) && (MyBigInt.ExtendedEuclid(m, e).Item1 == new MyBigInt(1))) { return(e); } e--; } }
public void Encrypt(MyBigInt p, MyBigInt q) { if (p.IsPrimeNumber() && q.IsPrimeNumber()) { var s = new StringBuilder(); try{ StreamReader sr = new StreamReader("in.txt"); while (!sr.EndOfStream) { s.Append(sr.ReadLine()); } sr.Close(); } catch { Console.WriteLine("Файл in.txt не найден"); return; } MyBigInt n = p * q; MyBigInt phi = (p - 1) * (q - 1); MyBigInt e = CalculateE(phi); MyBigInt d = (MyBigInt.Inverse(new MyBigInt(e), new MyBigInt(phi))); if (d == e) { d += phi; } List <string> result = Encode(s.ToString(), e, n); StreamWriter sw = new StreamWriter("out1.txt"); foreach (string item in result) { sw.WriteLine(item); } sw.Close(); PublicKey = new Tuple <MyBigInt, MyBigInt>(e, n); PrivateKey = new Tuple <MyBigInt, MyBigInt>(d, n); Console.WriteLine("Публичный ключ - ({0}, {1})", e, n); Console.WriteLine("Приватный ключ - ({0}, {1})", d, n); Process.Start("out1.txt"); } else { Console.WriteLine("p или q - не простые числа!"); } }