示例#1
0
        /// <summary>
        /// Rabin-Miller素性检测
        /// </summary>
        /// <param name="biN">被检数</param>
        /// <param name="iK">测试轮数 默认50</param>
        /// <returns>true:biN为素数,false:biN不是素数</returns>
        public bool RabinMiller(BigInteger biN, int iK = 50)
        {
            if ((biN & 1) == 0)
            {
                return(false);
            }
            BigInteger s = 0, i = 1;
            BigInteger t = biN - 1;

            while ((t & 1) == 0)
            {
                t >>= 1;
                ++s;
            }

            while (iK-- != 0)
            {
                RNGCryptoServiceProvider csp = new RNGCryptoServiceProvider();
                byte[] baCsp = new byte[8];
                csp.GetNonZeroBytes(baCsp);
                BigInteger b = BitConverter.ToUInt64(baCsp, 0) % (biN - 2) + 2;
                BigInteger y = RSA_Math.RepeatMod(b, t, biN);
                if (y == 1)
                {
                    return(true);
                }
                while (y != biN - 1)
                {
                    if (i == s)
                    {
                        return(false);
                    }
                    y = RSA_Math.RepeatMod(y, 2, biN);
                    ++i;
                }
            }
            return(true);
        }
示例#2
0
 /// <summary>
 /// RSA解密基本方法
 /// </summary>
 /// <param name="biCipher"></param>
 /// <param name="d"></param>
 /// <param name="N"></param>
 /// <returns></returns>
 public static BigInteger Decrypt(BigInteger biCipher, BigInteger d, BigInteger N)
 {
     return(RSA_Math.RepeatMod(biCipher, d, N));
 }
示例#3
0
 /// <summary>
 /// RSA加密基本方法
 /// </summary>
 /// <param name="biPlain"></param>
 /// <param name="e"></param>
 /// <param name="N"></param>
 /// <returns></returns>
 public static BigInteger Encrypt(BigInteger biPlain, BigInteger e, BigInteger N)
 {
     return(RSA_Math.RepeatMod(biPlain, e, N));
 }