예제 #1
0
        public (CurvePoint, BigInteger) CreateKeys()
        {
            var closeKey = Algos.GenerateRandom(1, Curve.n - 1);
            var openKey  = Curve.G.MulOnScalar(closeKey);

            return(openKey, closeKey);
        }
예제 #2
0
        public (ElGamalKey, ElGamalKey) GenerateKeys()
        {
            BigInteger prime, safePrime, generator;

            while (true)
            {
                prime = Algos.GenetateRandomPrime(
                    BigInteger.Pow(2, dimension / 4),
                    BigInteger.Pow(2, dimension - 1),
                    Algos.MillerRabin
                    );
                safePrime = 2 * prime + 1;
                if (Algos.MillerRabin(safePrime))
                {
                    break;
                }
            }
            while (true)
            {
                generator = Algos.GenetateRandomPrime(3, safePrime - 2, Algos.MillerRabin);
                if (BigInteger.ModPow(generator, 2, safePrime) != 1 && BigInteger.ModPow(generator, prime, safePrime) != 1)
                {
                    break;
                }
            }
            var closeKey = new ElGamalKey(Algos.GenerateRandom(2, safePrime - 2), generator, safePrime);
            var openKey  = new ElGamalKey(BigInteger.ModPow(generator, closeKey.Key, safePrime), generator, safePrime);

            return(openKey, closeKey);
        }
예제 #3
0
        public ElGamalSignature CreateSignature(ElGamalKey closeKey)
        {
            using var sHA256 = SHA256.Create();;
            using var file   = File.OpenRead(fileName);
            var hash = new BigInteger(sHA256.ComputeHash(file), true);
            var k    = Algos.GenerateRandomCoprime(closeKey.Prime - 1);
            var r    = BigInteger.ModPow(closeKey.Generator, k, closeKey.Prime);
            var s    = ((hash - closeKey.Key * r) * Algos.ReverseByMod(k, closeKey.Prime - 1)) % (closeKey.Prime - 1);

            return(new ElGamalSignature(r, s));
        }
예제 #4
0
 public static BigInteger GenerateRandomCoprime(BigInteger other)
 {
     for (var i = 0; i < 100; ++i)
     {
         var result = GenetateRandomPrime(2, other - 1, MillerRabin);
         if (Algos.EuclidExtended(other, result, out BigInteger temp1, out BigInteger temp2) == 1)
         {
             return(result);
         }
     }
     return(65537);
 }
예제 #5
0
        static public BigInteger Inverse(BigInteger n, BigInteger m)
        {
            if (n < 0)
            {
                return(m - Inverse(-n, m));
            }
            var gcd = Algos.EuclidExtended(n, m, out BigInteger x, out BigInteger y);

            if ((n * x + m * y) % m != gcd || gcd != 1)
            {
                throw new InvalidProgramException("Inverse don't exist");
            }
            return(x % m);
        }
예제 #6
0
        public Signature CreateSignature(BigInteger closeKey)
        {
            var z = GetHash();

            while (true)
            {
                var k = Algos.GenerateRandom(1, Curve.n - 1);
                var P = Curve.G.MulOnScalar(k);
                var r = P.x % Curve.n;
                if (r == 0)
                {
                    continue;
                }
                var s = (Inverse(k, Curve.n) * (z + r * closeKey)) % Curve.n;
                if (s == 0)
                {
                    continue;
                }
                return(new Signature(r, s));
            }
        }