コード例 #1
0
        private BigInteger GeneratePrimeNumber()
        {
            BigInteger number;

            do
            {
                number = random.Next(100, 500);
            } while (!GCD.IsSimple(number));
            return(number);
        }
コード例 #2
0
        private BigInteger GeneratePrimeNumber()
        {
            BigInteger number = random.Next(4, 1000);

            while (!GCD.IsSimple(number))
            {
                number = random.Next(4, 1000);
            }
            return(number);
        }
コード例 #3
0
ファイル: RSA.cs プロジェクト: ValerieBrave/Cryptography_labs
        private BigInteger GenerateCoprimeNumber(BigInteger coprime)
        {
            BigInteger number = random.Next(2, (int)coprime - 1), nod = GCD.GetNOD(number, coprime);

            while (nod != 1)
            {
                number = random.Next(2, (int)coprime - 1);
                nod    = GCD.GetNOD(number, coprime);
            }
            return(number);
        }
コード例 #4
0
ファイル: RSA.cs プロジェクト: ValerieBrave/Cryptography_labs
 public RSA()
 {
     p  = GeneratePrimeNumber();
     q  = GeneratePrimeNumber();
     n  = p * q;
     fi = (p - 1) * (q - 1);
     e  = GenerateCoprimeNumber(fi);
     d  = GCD.ModInverse(e, fi);
     Console.WriteLine(new string('-', 40) + "Digital sign via RSA" + new string('-', 40));
     Console.WriteLine($"p = {p}, q = {q}, n = p * q = {p * q}, fi(n) = {fi}, e = {e}, d = {d}\n");
     Console.WriteLine($"PUBLIC KEY: (e, n) = ({e}, {n})");
     Console.WriteLine($"PRIVATE KEY: (d, n) = ({d}, {n})");
     Console.WriteLine();
 }
コード例 #5
0
 public Schnorr()
 {
     do
     {
         q = GeneratePrimeNumber();
     } while ((p - 1) % q != 0);
     do
     {
         g = random.Next(10000, 15000);
     } while (g == 1 || BigInteger.ModPow(g, (int)q, p) != 1);
     do
     {
         x = random.Next((int)q);
     } while (x >= q);
     y = GCD.ModInverse(BigInteger.ModPow(g, x, p), p);
     Console.WriteLine(new string('-', 40) + "Digital sign via Snorr" + new string('-', 40));
     Console.WriteLine($"p = {p}, q = {q}, g = {g}, x = {x}, y = {y}\n");
     Console.WriteLine($"PUBLIC KEY: (p, q, g, y) = ({p}, {q}, {g}, {y})");
     Console.WriteLine($"PRIVATE KEY: (x) = ({x})");
     Console.WriteLine();
 }
コード例 #6
0
 public BigInteger[,] CreateDigitalSign(string text)
 {
     using (MD5 md5Hash = MD5.Create())
     {
         hash = MD5Hash.GetMd5Hash(md5Hash, text);
     }
     byte[] bytes = Encoding.ASCII.GetBytes(hash);
     BigInteger[,] digitalSign = new BigInteger[bytes.Length, 2];
     for (int i = 0; i < bytes.Length; i++)
     {
         do
         {
             BigInteger k = random.Next(2, (int)p - 2);
             while (GCD.GetNOD(k, p - 1) != 1)
             {
                 k = random.Next(2, (int)p - 2);
             }
             digitalSign[i, 0] = BigInteger.ModPow(g, k, p);
             BigInteger temp = BigInteger.Multiply(BigInteger.Subtract(bytes[i], BigInteger.Multiply(x, digitalSign[i, 0])), GCD.ModInverse(k, p - 1));
             digitalSign[i, 1] = temp < 0 ? (p - 1) - BigInteger.ModPow(BigInteger.Negate(temp), 1, p - 1) : BigInteger.ModPow(temp, 1, p - 1);
         } while (digitalSign[i, 1] == 0);
     }
     return(digitalSign);
 }