Exemple #1
0
        /// <summary>
        /// Gets BigNum <paramref name="b"/>.
        /// Generates p and q, prime numbers.
        /// While b is not inverse number to p-1 and q-1, p and q re-generates.
        /// After finding good p and q, calculate a that is inverse of b mod ((p-1)(q-1)).
        /// n is calculated as p * q.
        /// Returns by reference the BigNum a, p, q, and n.
        /// </summary>
        /// <param name="b"></param>
        /// <param name="a"></param>
        /// <param name="q"></param>
        /// <param name="p"></param>
        /// <param name="nByRef"></param>
        public static void GenRSA(BigNum b, BigNum a, BigNum q, BigNum p, BigNum nByRef)
        {
            bool   isBInverseOfPMinus1AndQMinus1 = false;
            BigNum n = new BigNum();

            while (!isBInverseOfPMinus1AndQMinus1)
            {
                p.Set(GenPrime());
                q.Set(GenPrime());

                BigNum pMinus1 = new BigNum(p);
                pMinus1.SubNum(GlobalVariables.one);
                BigNum qMinus1 = new BigNum(q);
                qMinus1.SubNum(GlobalVariables.one);

                BigNum fiOfN = new BigNum(pMinus1);
                fiOfN.MultNum(qMinus1);
                BigNum gcd = new BigNum();
                ExtendedGCD(b, fiOfN, new BigNum(), new BigNum(), gcd);
                if (gcd.CompNumWithoutPrint(GlobalVariables.one))
                {
                    isBInverseOfPMinus1AndQMinus1 = true;
                    a.Set(Inverse(b, fiOfN));
                    if (a.IsNegative)
                    {
                        a.AddNum(fiOfN);
                    }
                    n = new BigNum(p);
                    n.MultNum(q);
                }
            }

            nByRef.Set(n);
        }
Exemple #2
0
        /// <summary>
        /// K.
        /// Gets two BigNum type numbers a and m and returns by reference (d, x, y), also BigNum type numbers.
        /// d represents GCD(m,a). (Greatest common divider).
        /// Note: If d = 1, it means that x is the opposite number of m(mod a) and
        ///                               y is the opposite number of a(mod m).
        /// Proofe for Note: 1 = [m*x + a*y](mod m) ==> 1 = a*y.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="m"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="d"></param>
        public static void ExtendedGCD(BigNum a, BigNum m, BigNum x, BigNum y, BigNum d)
        {
            BigNum r0 = new BigNum(m);
            BigNum x0 = new BigNum(GlobalVariables.one);
            BigNum y0 = new BigNum(GlobalVariables.zero);

            BigNum r1 = new BigNum(a);
            BigNum x1 = new BigNum(GlobalVariables.zero);
            BigNum y1 = new BigNum(GlobalVariables.one);

            BigNum r = new BigNum(GlobalVariables.one);    // Just to be able to enter the while loop.
            BigNum q = new BigNum();

            DivNum(r0, r1, q, r);

            while (!r.CompNumWithoutPrint(GlobalVariables.zero))
            {
                // x = x0 - qx1.
                BigNum qx1 = new BigNum(q);
                qx1.MultNum(x1);
                x.Set(x0);
                x.SubNum(qx1);

                // y = y0 - qy1.
                BigNum qy1 = new BigNum(q);
                qy1.MultNum(y1);
                y.Set(y0);
                y.SubNum(qy1);

                r0.Set(r1);
                x0.Set(x1);
                y0.Set(y1);

                r1.Set(r);
                x1.Set(x);
                y1.Set(y);

                DivNum(r0, r1, q, r);
            }

            d.Set(r1);
            x.Set(x1);
            y.Set(y1);
        }
Exemple #3
0
        public static void G()
        {
            Console.WriteLine("(G) Using MultNum method.");
            Console.WriteLine("Multiply 7 by 3. Equals 21 (10101 in base 2)");
            BigNum bigNumG1 = new BigNum(7L);
            BigNum bigNumG2 = new BigNum(3L);

            bigNumG1.MultNum(bigNumG2);
            Console.WriteLine(bigNumG1.WriteNum());

            Console.WriteLine("Multiply 113 by 73. Equals 8249 (10000000111001 in base 2)");
            BigNum bigNumG3 = new BigNum(113L);
            BigNum bigNumG4 = new BigNum(73L);

            bigNumG3.MultNum(bigNumG4);
            Console.WriteLine(bigNumG3.WriteNum());

            Console.WriteLine();
        }