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;
        }
        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;
        }
        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;
        }
 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());
 }