Пример #1
0
        // Нахождение числа d.
        public static BigInteger GenerateD(BigInteger e, BigInteger phi)
        {
            BigInteger d = new BigInteger();

            for (BigInteger i = 1; i < phi; i = BigInteger.Add(i, 1))
            {
                d = BigInteger.DivRem(BigInteger.Add(BigInteger.Multiply(i, phi), 1), e, out BigInteger remainder);
                if (remainder == 0)
                {
                    break;
                }
            }
            return(d);
        }
Пример #2
0
        private static BigInteger Gyorshatvanyoz(BigInteger alap, BigInteger kitevo, BigInteger modulus)
        {
            BigInteger congr    = alap;
            int        max      = (int)Math.Ceiling(BigInteger.Log(kitevo, 2)); //az adott számot hány biten lehet eltárolni
            BigInteger solution = 1;

            for (int j = 0; j < max; ++j)
            {
                if (kitevo % 2 == 1)
                {
                    solution *= congr;
                    solution %= modulus;
                }
                kitevo /= 2;

                BigInteger.DivRem(BigInteger.Pow(congr, 2), modulus, out congr); //Ez visszatér az első és a második paraméter egész osztásával és a congr változóba belerakja a maradékot
            }

            return(solution);
        }
Пример #3
0
        /// <summary>
        /// 扩展欧几里得,求解ax+by=gcd(a,b)中的x与y
        /// </summary>
        /// <param name="a">a</param>
        /// <param name="b">b</param>
        /// <param name="x">x</param>
        /// <param name="y">y</param>
        /// <returns>gcd(a,b)</returns>
        public static BigInteger ExtendGCD(BigInteger a, BigInteger b, out BigInteger x, out BigInteger y)
        {
            if (b == 0)
            {
                x = 1; y = 0; return(a);
            }
            BigInteger x0 = 1, y0 = 0, x1 = 0, y1 = 1, x2, y2;
            BigInteger r, p;

            p = BigInteger.DivRem(a, b, out r);
            while (r > 0)
            {
                x2 = x0 - x1 * p;
                y2 = y0 - y1 * p;
                x0 = x1; x1 = x2; y0 = y1; y1 = y2;
                a  = b; b = r; p = BigInteger.DivRem(a, b, out r);
            }
            x = x1;
            y = y1;
            return(b);
        }