コード例 #1
0
        public void Generate_ab()
        {
            byte[] bytes = new byte[KeySize / 8];
            BigInteger _a = new BigInteger(bytes);

            RandomNumberGenerator rng = RandomNumberGenerator.Create();
            while (!_a.IsProbablePrime(KeySize) || _a >= P)
            {
                rng.GetBytes(bytes);

                byte[] temp = new byte[bytes.Length + 1];
                Array.Copy(bytes, temp, bytes.Length);

                _a = new BigInteger(temp);
            }

            bytes = new byte[KeySize / 8];
            BigInteger _b = new BigInteger(bytes);
            while (!_b.IsProbablePrime(KeySize) || _b >= P)
            {
                rng.GetBytes(bytes);

                byte[] temp = new byte[bytes.Length + 1];
                Array.Copy(bytes, temp, bytes.Length);

                _b = new BigInteger(temp);
            }

            a = _a;
            b = _b;
        }
コード例 #2
0
        public void GeneratePG()
        {
            byte[] bytes = new byte[KeySize / 8];
            BigInteger p = new BigInteger(bytes);

            RandomNumberGenerator rng = RandomNumberGenerator.Create();
            while (!p.IsProbablePrime(KeySize))
            {
                rng.GetBytes(bytes);

                byte[] temp = new byte[bytes.Length + 1];
                Array.Copy(bytes, temp, bytes.Length);

                p = new BigInteger(temp);
            }

            bytes = new byte[KeySize / 8];
            BigInteger g = new BigInteger(bytes);
            while (!g.IsProbablePrime(KeySize) || g >= p)
            {
                rng.GetBytes(bytes);

                byte[] temp = new byte[bytes.Length + 1];
                Array.Copy(bytes, temp, bytes.Length);

                g = new BigInteger(temp);
            }

            P = p;
            G = g;
        }
コード例 #3
0
        public static BigInteger GenPrimeBigInteger(byte length)
        {
            RandomNumberGenerator rng = RandomNumberGenerator.Create();
            byte[] bytes = new byte[length];
            BigInteger a;
            int i = 0;
            do
            {
                i++;
                rng.GetBytes(bytes);
                a = new BigInteger(bytes);

            } while (!a.IsProbablePrime(1));
            return a;
        }
コード例 #4
0
 public async Task<IHttpActionResult> GetRandomPrime()
 {
     var rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();
     var bytes = new byte[16];
     var result = await Task.Run(() =>
     {
         rnd.GetNonZeroBytes(bytes);
         var res = new BigInteger(bytes);
         while (!res.IsProbablePrime(10))
         {
             rnd.GetNonZeroBytes(bytes);
             res = new BigInteger(bytes);
         }
         return res;
     });
     Debug.WriteLine(result);
     return Json(result.ToString());
 }