Ejemplo n.º 1
0
        private static void SimulateDiffieHellmanExchange(BigInteger p, BigInteger g)
        {
            Console.WriteLine("Alice starts exchange");

            BigInteger xA = CryptoTools.GenerateDiffieHellmanPrivateKey(g, p);

            Console.WriteLine("Alice generated private key xA = {0}", xA);
            BigInteger yA = CryptoTools.GenerateDiffieHellmanPublicKey(g, xA, p);

            Console.WriteLine("Alice generated public key yA = {0} and sent it to Bob", yA);

            BigInteger xB = CryptoTools.GenerateDiffieHellmanPrivateKey(g, p);

            Console.WriteLine("Bob generated private key xB = {0}", xB);
            BigInteger yB = CryptoTools.GenerateDiffieHellmanPublicKey(g, xB, p);

            Console.WriteLine("Bob generated public key yB = {0} and sent it to Alice", yB);

            BigInteger zAB = CryptoTools.ModuloPower(yB, xA, p);

            Console.WriteLine($"Alice calculated zAB = {zAB} - common private key");

            BigInteger zBA = CryptoTools.ModuloPower(yA, xB, p);

            Console.WriteLine($"Bob calculated zBA = {zBA} - common private key");
        }