Exemple #1
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");
        }
Exemple #2
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()));
        }
Exemple #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()));
        }
Exemple #4
0
        public string Decryption()
        {
            Console.WriteLine("Alice recieves the numbers and starting to work with it.");
            Console.WriteLine("Decrypted Word's Codes:");
            List <BigInt> wordNumbers = new List <BigInt>();
            BigInt        one         = new BigInt("1");

            for (int i = 0; i < word.Length; i++)
            {
                //wordNumbers.Add(BigInt.powByMod(wordNumbersEncrypted[i], d, mod));
                wordNumbers.Add(ChineseTheorem(BigInt.mod(BigInt.pow(wordNumbersEncrypted[i], BigInt.mod(d, BigInt.subtract(p, one))), p), BigInt.mod(BigInt.pow(wordNumbersEncrypted[i], BigInt.mod(d, BigInt.subtract(q, one))), q), p, q));

                //Console.Write("Chinese: " + ChineseTheorem(BigInt.mod(BigInt.pow(wordNumbersEncrypted[i], BigInt.mod(d, BigInt.subtract(p, one))), p), BigInt.mod(BigInt.pow(wordNumbersEncrypted[i], BigInt.mod(d, BigInt.subtract(q, one))), q), p, q).toString());
                Console.Write(wordNumbers[i].toString() + " ");
            }
            Console.WriteLine();
            string decryptedWord = "";

            for (int i = 0; i < word.Length; i++)
            {
                decryptedWord += (char)Int32.Parse(wordNumbers[i].toString());
            }
            Console.WriteLine("Decrypted Word: " + decryptedWord);
            Console.WriteLine("Sign: " + BigInt.powByMod(SignEncrypted, eSign, modSign).toString());
            Console.WriteLine("Hash: " + BigInt.mod(Hash(word, modSign), modSign).toString());
            Console.WriteLine("------------------------------------------------");
            return(decryptedWord);
        }
Exemple #5
0
        public BigInt Karmichael(BigInt p, BigInt q)
        {
            BigInt one = new BigInt("1");

            return(BigInt.LCM(BigInt.subtract(p, one), BigInt.subtract(q, one)));
        }
Exemple #6
0
        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;
            }
        }