Exemplo n.º 1
0
        /// <summary>
        /// 人为素数的密钥生成方法 如果参数不能通过素性检测则抛出异常
        /// </summary>
        /// <param name="p"></param>
        /// <param name="q"></param>
        /// <returns></returns>
        public static BigInteger[] GenKey(ulong p, ulong q)
        {
            PrimeGen pgen = new PrimeGen();

            if (!pgen.RabinMiller(p, 100))
            {
                throw new NotPrimeNumberException(p.ToString());
            }
            if (!pgen.RabinMiller(q, 100))
            {
                throw new NotPrimeNumberException(q.ToString());
            }
            BigInteger N = p * q;
            BigInteger r = (p - 1) * (q - 1);
            BigInteger e = pgen.Gen();

            e %= r;
            while (RSA_Math.SteinGCD(e, r) != 1)
            {
                e  = pgen.Gen();
                e %= r;
            }
            BigInteger d = RSA_Math.ExEuclid(e, r);

            return(new BigInteger[] { N, e, d });
        }
Exemplo n.º 2
0
        /// <summary>
        /// RSA密钥生成方法 除密钥外还返回了大素数p和q
        /// </summary>
        /// <returns>[n e d p q]</returns>
        public static BigInteger[] GenKeyAdd()
        {
            PrimeGen   pgen = new PrimeGen();
            BigInteger p    = pgen.Gen();
            BigInteger q    = pgen.Gen();
            BigInteger N    = p * q;
            BigInteger r    = (p - 1) * (q - 1);
            BigInteger e    = pgen.Gen();

            e %= r;
            while (RSA_Math.SteinGCD(e, r) != 1)
            {
                e  = pgen.Gen();
                e %= r;
            }
            BigInteger d = RSA_Math.ExEuclid(e, r);

            return(new BigInteger[] { N, e, d, p, q });
        }