Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
        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("------------------------------------------------");
        }
Exemplo n.º 3
0
        public static BigInt powByModFast(BigInt x, BigInt y, BigInt N)
        {
            if (y.isZero())
            {
                return(new BigInt("1"));
            }
            string binary = Convert.ToString(Int32.Parse(y.toString()), 2);

            binary = new string(binary.ToCharArray().Reverse().ToArray());
            List <BigInt> temp = new List <BigInt>();
            BigInt        last = x;

            temp.Add(last);
            for (int i = 1; i < binary.Length; i++)
            {
                last = mod((multiply(last, last)), N);
                temp.Add(last);
            }
            BigInt res = new BigInt("1");

            for (int i = 0; i < binary.Length; i++)
            {
                if (binary[i] == '1')
                {
                    res = multiply(res, temp[i]);
                }
            }
            res = mod(res, N);
            return(res);
        }
Exemplo n.º 4
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");
        }
Exemplo n.º 5
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()));
        }
Exemplo n.º 6
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()));
        }
Exemplo n.º 7
0
        public static bool Vn(BigInt n, BigInt D)
        {
            BigInt P = new BigInt("1");
            BigInt Q = divide(subtract(P, D), new BigInt("4"));

            Console.WriteLine("Q:" + Q.toString());
            BigInt U        = new BigInt("1");
            BigInt V        = new BigInt("1");
            BigInt k        = new BigInt("1");
            BigInt one      = new BigInt("1");
            BigInt two      = new BigInt("2");
            BigInt nPlusOne = add(one, n);
            string bin      = nPlusOne.ToBinary();

            Console.WriteLine("n+1 bin: " + bin);
            BigInt U_temp = new BigInt(U.toString());
            BigInt V_temp = new BigInt(V.toString());
            int    i      = 0;

            while (i < bin.Length)
            {
                //if ((bin[i] == '1') && (i == 0))
                //{
                //    U = U2k(U_temp, V_temp);
                //    V = V2k(V_temp, Q, k);
                //}
                if ((bin[i] == '1') && (i != 0))
                {
                    U      = U2k(U_temp, V_temp);
                    V      = V2k(V_temp, Q, k);
                    U_temp = new BigInt(U.toString());
                    V_temp = new BigInt(V.toString());
                    //Console.WriteLine("V:" + V.toString());
                    //Console.WriteLine("U" + U.toString());
                    k = multiply(two, k);
                    U = U2kPlusOne(U_temp, V_temp, P);
                    V = V2kPlusOne(U_temp, V_temp, P, D);
                    k = add(k, one);
                }
                ;
                if ((bin[i] == '0'))
                {
                    U = U2k(U_temp, V_temp);
                    V = V2k(V_temp, Q, k);
                    k = multiply(two, k);
                }
                ;
                //Console.WriteLine("V:" + V.toString());
                //Console.WriteLine("U" + U.toString());
                i++;
                U_temp = new BigInt(U.toString());
                V_temp = new BigInt(V.toString());
            }
            Console.WriteLine("k: " + k.toString());
            Console.WriteLine("Vn+1 mod n: " + mod(V, n).toString());
            BigInt compare = multiply(two, Q);

            while (less(compare, new BigInt("0")))
            {
                compare = add(compare, n);
            }
            Console.WriteLine("Result:");
            return(equal(mod(V, n), compare));
        }