예제 #1
0
        public void RunTest()
        {
            // Done by sender
            //BigInteger p = 2357;
            BigInteger p = this.GenerateP(32);
            //BigInteger alpha = 2;
            BigInteger alpha = Helpers.GetRandomBigInteger(1, p - 1);
            //BigInteger a = 1751;
            BigInteger        a          = Helpers.GetRandomBigInteger(1, p - 2);
            BigInteger        alphaToA   = Helpers.SquareAndMultiply(alpha, a, p);
            ElGamalPublicKey  publicKey  = new ElGamalPublicKey(p, alpha, alphaToA);
            ElGamalPrivateKey privateKey = new ElGamalPrivateKey(a);

            // Done by receiver
            int message = 2035;
            //BigInteger k = 1520; // Random number 1 <= k <= p-2
            BigInteger k     = Helpers.GetRandomBigInteger(1, p - 2); // Random number 1 <= k <= p-2
            BigInteger gamma = Helpers.SquareAndMultiply(alpha, k, p);
            BigInteger delta = (message * Helpers.SquareAndMultiply(publicKey.AlphaToA, k, p)) % p;
            Tuple <BigInteger, BigInteger> cipherText = new Tuple <BigInteger, BigInteger>(gamma, delta);

            // Done by receiver
            BigInteger gammaToTheNegativeA = Helpers.SquareAndMultiply(gamma, (p - 1 - a), p);

            BigInteger plainText = gammaToTheNegativeA * delta % p;

            Console.WriteLine("plainText: " + message);
            Console.WriteLine("cipherText: (" + gamma + "," + delta + ")");
            Console.WriteLine("plainText: " + plainText);
        }
예제 #2
0
        public void KeyGeneration(int keySize)
        {
            this.p        = this.GenerateP(keySize);
            this.alpha    = Helpers.GetRandomBigInteger(1, p - 1);
            this.a        = Helpers.GetRandomBigInteger(1, p - 2);
            this.alphaToA = Helpers.SquareAndMultiply(alpha, a, p);

            this.PublicKey  = new ElGamalPublicKey(p, alpha, alphaToA);
            this.PrivateKey = new ElGamalPrivateKey(a);
        }