コード例 #1
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));
        }
コード例 #2
0
        public static string MillerRabin(BigInt x)
        {
            BigInt xMinusOne = subtract(x, new BigInt("1"));
            BigInt two       = new BigInt("2");
            int    i         = 0;
            BigInt m         = xMinusOne;

            while (true)
            {
                if (BigInt.subtract(m, BigInt.multiply(two, BigInt.divide(m, two))).toString() != "0")
                {
                    break;
                }
                m = divide(m, two);
                i++;
            }
            Random r = new Random();
            BigInt a;

            if (xMinusOne.toString().Length >= 10)
            {
                a = new BigInt((r.Next(2, 1000000000)).ToString());
            }
            else
            {
                a = new BigInt((r.Next(2, Int32.Parse(subtract(xMinusOne, new BigInt("1")).toString()))).ToString());
            }
            BigInt b       = powByModFast(a, m, x);
            BigInt one     = new BigInt("1");
            BigInt counter = new BigInt("0");
            BigInt iBigInt = new BigInt(i.ToString());

            if (equal(b, one))
            {
                return("probably prime");
            }
            while (less(counter, iBigInt))
            {
                if (equal(b, xMinusOne))
                {
                    return("probably prime");
                }
                b       = powByModFast(b, two, x);
                counter = add(counter, one);
            }
            return("composite");
        }
コード例 #3
0
        public static string ToBiase64(this BigInt x)
        {
            BigInt num  = new BigInt(x.toString());
            BigInt zero = new BigInt("0");
            BigInt two  = new BigInt("64");
            string res  = "";
            BigInt temp;

            while (num.toString() != "0")
            {
                temp = BigInt.mod(num, two);
                num  = BigInt.subtract(num, temp);
                num  = BigInt.divide(num, two);
                res += base64Table[Int32.Parse(temp.toString())];
            }
            return(new string(res.ToCharArray().Reverse().ToArray()));
        }
コード例 #4
0
        public static string ToEight(this BigInt x)
        {
            BigInt num  = new BigInt(x.toString());
            BigInt zero = new BigInt("0");
            BigInt two  = new BigInt("8");
            string res  = "";
            BigInt temp;

            while (num.toString() != "0")
            {
                temp = BigInt.mod(num, two);
                num  = BigInt.subtract(num, temp);
                num  = BigInt.divide(num, two);
                res += temp.toString();
            }
            return(new string(res.ToCharArray().Reverse().ToArray()));
        }
コード例 #5
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;
            }
        }