コード例 #1
0
ファイル: RSA.cs プロジェクト: YehorOstapchuk/CryptoLab
        public void SignInitialize(int max, int powMin, int powMax)
        {
            Console.WriteLine("------------------------------------------------");
            max    = (int)Math.Pow(2, max);
            powMin = (int)Math.Pow(2, powMin);
            powMax = (int)Math.Pow(2, powMax);
            do
            {
                pSign = BigInt.PrimeGenerate(powMin, powMax);
                Console.WriteLine("P Sign: " + pSign.toString());
                qSign   = BigInt.PrimeGenerate(powMin, powMax);
                modSign = BigInt.multiply(pSign, qSign);
            }while ((BigInt.less(modSign, new BigInt(max.ToString()))) || (BigInt.equal(pSign, qSign)));

            Console.WriteLine("P Sign: " + pSign.toString());
            Console.WriteLine("Q Sign: " + qSign.toString());
            Console.WriteLine("mod Sign: " + modSign.toString());

            ASign = Karmichael(pSign, qSign);
            Console.WriteLine("A Sign: " + ASign.toString());
            do
            {
                do
                {
                    eSign = BigInt.PrimeGenerate(0, Int32.Parse(ASign.toString()));
                }while ((BigInt.mod(ASign, eSign)).isZero());
                BigInt temp;
                BigInt.ExtendedGCD(ASign, eSign, out temp, out dSign);
            }while (BigInt.less(dSign, new BigInt("0")));
            Console.WriteLine("E Sign: " + eSign.toString());
            Console.WriteLine("D Sign: " + dSign.toString());
            Console.WriteLine("------------------------------------------------");
        }
コード例 #2
0
ファイル: RSA.cs プロジェクト: YehorOstapchuk/CryptoLab
        public BigInt ChineseTheorem(BigInt r1, BigInt r2, BigInt mod1, BigInt mod2)
        {
            // Console.WriteLine("r1:" + r1.toString());
            BigInt M   = BigInt.multiply(mod1, mod2);
            BigInt m1  = BigInt.divide(M, mod1);
            BigInt m2  = BigInt.divide(M, mod2);
            BigInt y1  = new BigInt("1");
            BigInt y2  = new BigInt("1");
            BigInt one = new BigInt("1");

            for (BigInt i = one; BigInt.less(i, mod1); i = BigInt.add(i, one))
            {
                if (BigInt.equal(BigInt.multiplyByMod(m1, i, mod1), one))
                {
                    y1 = i; break;
                }
            }

            for (BigInt i = one; BigInt.less(i, mod2); i = BigInt.add(i, one))
            {
                if (BigInt.equal(BigInt.multiplyByMod(m2, i, mod2), one))
                {
                    y2 = i; break;
                }
            }

            return(BigInt.mod(BigInt.add(BigInt.multiply(m1, BigInt.multiply(r1, y1)), BigInt.multiply(m2, BigInt.multiply(r2, y2))), mod));
        }
コード例 #3
0
ファイル: RSA.cs プロジェクト: YehorOstapchuk/CryptoLab
        public RSA(int max, int powMin, int powMax)
        {
            SignInitialize(max, powMin, powMax);
            max    = (int)Math.Pow(2, max);
            powMin = (int)Math.Pow(2, powMin);
            powMax = (int)Math.Pow(2, powMax);
            do
            {
                p   = BigInt.PrimeGenerate(powMin, powMax);
                q   = BigInt.PrimeGenerate(powMin, powMax);
                mod = BigInt.multiply(p, q);
            }while ((BigInt.less(mod, new BigInt(max.ToString()))) || (BigInt.equal(p, q)));

            Console.WriteLine("P: " + p.toString());
            Console.WriteLine("Q: " + q.toString());
            Console.WriteLine("mod: " + mod.toString());

            A = Karmichael(p, q);
            Console.WriteLine("A: " + A.toString());
            do
            {
                do
                {
                    e = BigInt.PrimeGenerate(0, Int32.Parse(A.toString()));
                }while ((BigInt.mod(A, e)).isZero());
                BigInt temp;
                BigInt.ExtendedGCD(A, e, out temp, out d);
            }while (BigInt.less(d, new BigInt("0")));
            Console.WriteLine("E: " + e.toString());
            Console.WriteLine("D: " + d.toString());
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: YehorOstapchuk/CryptoLab
        static void calculate()
        {
            BigInt a, b, m;
            int    n;

            switch (choice)
            {
            case 1:
                Console.WriteLine("1 - +");
                a = getNumber("First ");
                // BigInt.Billie(a);
                // Console.WriteLine((BigInt.Billie(a).toString()));
                b = getNumber("Second ");
                Console.WriteLine(a.toString() + "+" + b.toString() + "=" + BigInt.add(a, b).toString());
                break;

            case 2:
                Console.WriteLine("2 - -");
                a = getNumber("First ");
                b = getNumber("Second ");
                Console.WriteLine(a.toString() + "-" + b.toString() + "=" + BigInt.subtract(a, b).toString());
                break;

            case 3:
                Console.WriteLine("3 - *");
                a = getNumber("First ");
                b = getNumber("Second ");
                Console.WriteLine(a.toString() + "*" + b.toString() + "=" + BigInt.multiply(a, b).toString());
                break;

            case 4:
                Console.WriteLine("4 - /");
                a = getNumber("First ");
                b = getNumber("Second ");
                Console.WriteLine(a.toString() + "/" + b.toString() + "=" + BigInt.divide(a, b).toString());
                Console.WriteLine("Ostacha: " + BigInt.subtract(a, BigInt.multiply(b, BigInt.divide(a, b))).toString());
                break;

            case 5:
                Console.WriteLine("5 - ^");
                a = getNumber("First ");
                b = getNumber("Second ");
                Console.WriteLine(a.toString() + "^" + b.toString() + "=" + BigInt.pow(a, b).toString());
                break;

            case 6:
                Console.WriteLine("6 - [sqrt(x)]");
                a = getNumber("Number >0 ");
                Console.WriteLine("sqrt =" + a.sqrt().toString());
                break;

            case 7:
                Console.WriteLine("7 - <");
                a = getNumber("First ");
                b = getNumber("Second ");
                Console.WriteLine(a.toString() + "<" + b.toString() + "=" + BigInt.less(a, b));
                break;

            case 8:
                Console.WriteLine("8 - >");
                a = getNumber("First ");
                b = getNumber("Second ");
                Console.WriteLine(a.toString() + ">" + b.toString() + "=" + BigInt.greater(a, b));
                break;

            case 9:
                Console.WriteLine("9 - ==");
                a = getNumber("First ");
                b = getNumber("Second ");
                Console.WriteLine(a.toString() + "==" + b.toString() + "=" + BigInt.equal(a, b));
                break;

            case 10:
                Console.WriteLine("10 - + (mod)");
                a = getNumber("First ");
                b = getNumber("Second ");
                m = getNumber("mod ");
                Console.WriteLine(a.toString() + "+" + b.toString() + "mod" + m.toString() + "=" + BigInt.addMod(a, b, m).toString());
                break;

            case 11:
                Console.WriteLine("11 - - (mod)");
                a = getNumber("First ");
                b = getNumber("Second ");
                m = getNumber("mod ");
                Console.WriteLine(a.toString() + "-" + b.toString() + "mod" + m.toString() + "=" + BigInt.subtractMod(a, b, m).toString());
                break;

            case 12:
                Console.WriteLine("12 - * (mod)");
                a = getNumber("First ");
                b = getNumber("Second ");
                m = getNumber("mod ");
                Console.WriteLine(a.toString() + "*" + b.toString() + "mod" + m.toString() + "=" + BigInt.multiplyByMod(a, b, m).toString());
                break;

            case 13:
                Console.WriteLine("13 - /(mod)");
                a = getNumber("First ");
                b = getNumber("Second ");
                m = getNumber("mod ");
                Console.WriteLine(a.toString() + "/" + b.toString() + "mod" + m.toString() + "=" + BigInt.divideMod(a, b, m).toString());
                Console.WriteLine("Ostacha: " + BigInt.subtractMod(a, BigInt.multiply(b, BigInt.divide(a, b)), m).toString());
                break;

            case 14:
                Console.WriteLine("14 - ^ (mod)");
                a = getNumber("First ");
                b = getNumber("Second ");
                m = getNumber("mod ");
                Console.WriteLine(a.toString() + "^" + b.toString() + "mod" + m.toString() + "=" + BigInt.powByMod(a, b, m).toString());
                break;

            case 15:
                Console.WriteLine("15 - aiX=bi(mod pi)");
                n = getUInt("Count of equations");
                BigInt[] A = new BigInt[n];
                BigInt[] B = new BigInt[n];
                BigInt[] M = new BigInt[n];
                for (int i = 0; i < n; i++)
                {
                    A[i] = getNumber("A[" + i + "]");
                    B[i] = getNumber("B[" + i + "]");
                    M[i] = getNumber("P[" + i + "]");
                }
                for (int i = 0; i < n; i++)
                {
                    Console.WriteLine(A[i].toString() + "x =" + B[i].toString() + "mod" + M[i].toString());
                }
                (BigInt, BigInt)res = BigInt.solve(A, B, M, n);
                Console.WriteLine("x=" + res.Item1.toString() + "mod" + res.Item2.toString());
                break;

            case 16:
                Console.WriteLine("16 - ^ (mod) fast");
                a = getNumber("First ");
                b = getNumber("Second ");
                m = getNumber("mod ");
                Console.WriteLine(a.toString() + "^" + b.toString() + "mod" + m.toString() + "=" + BigInt.powByModFast(a, b, m).toString());
                break;

            case 17:
                Console.WriteLine("17 - Miller Rabin");
                a = getNumber("Number ");
                Console.WriteLine(BigInt.MillerRabin(a));
                break;

            case 18:
                //Console.WriteLine("18 - Billie");
                //Console.WriteLine("Enter the word");
                //string word = Console.ReadLine();
                //RSA test = new RSA(8, 1, 6);
                //test.Encryption(word);
                //Console.WriteLine(test.Decryption());
                break;

            case 19:
                Console.WriteLine("Enter the word");
                string word          = Console.ReadLine();
                long   ellapledTicks = DateTime.Now.Ticks;
                RSA    test          = new RSA(8, 32, 64);
                test.Encryption(word);
                ellapledTicks = DateTime.Now.Ticks - ellapledTicks;

                TimeSpan elapsedSpan = new TimeSpan(ellapledTicks);

                Console.WriteLine(test.Decryption());
                Console.WriteLine("Time: " + elapsedSpan.TotalMilliseconds);
                break;

            case 20:
                Console.WriteLine("19 - exit");
                end = true;
                break;
            }
        }