コード例 #1
0
ファイル: TestsMyBigInt.cs プロジェクト: 21xx13/LabRSA
        public void TestInverse()
        {
            var value1 = new MyBigInt("11");
            var value2 = new MyBigInt(15);
            var value3 = new MyBigInt(3);
            var value4 = new MyBigInt(26);

            Assert.AreEqual("11", MyBigInt.Inverse(value1, value2).ToString());
            Assert.AreEqual("3", MyBigInt.Inverse(value2, value1).ToString());
            Assert.AreEqual("9", MyBigInt.Inverse(value3, value4).ToString());
            Assert.AreEqual("2", MyBigInt.Inverse(value4, value3).ToString());
        }
コード例 #2
0
        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 - не простые числа!");
            }
        }