Ejemplo n.º 1
0
        internal static void SimulatePokerExchange(BigInteger p)
        {
            BigInteger cA, dA, cB, dB;

            GeneratePokerPrivateKeys(p, out cA, out dA);
            Console.WriteLine($"Alice's private keys are cA = {cA} and dA = {dA}");
            GeneratePokerPrivateKeys(p, out cB, out dB);
            Console.WriteLine($"Bob's private keys are cB = {cB} and dB = {dB}");

            BigInteger alpha, beta, gamma; //карты

            GenerateCardNumbers(p, out alpha, out beta, out gamma);
            Console.WriteLine($"Alice generated numbers for cards and told Bob that alpha is {alpha}, beta is {beta}, gamma is {gamma}");

            Console.WriteLine("START OF THE EXCHANGE");
            Triplet cards = new Triplet(new Card(alpha, "ALPHA"), new Card(beta, "BETA"), new Card(gamma, "GAMMA"));
            Triplet encryptedCardsForBob = cards.ModuloPower(cA, p);

            Console.WriteLine($"Alice encrypted card numbers: alpha to {encryptedCardsForBob.X}, beta to {encryptedCardsForBob.Y}, gamma to {encryptedCardsForBob.Z}...");
            encryptedCardsForBob.Mix();
            Console.WriteLine($"...And then mixed them: {encryptedCardsForBob.X}, {encryptedCardsForBob.Y}, {encryptedCardsForBob.Z} and sent to Bob");

            BigInteger cardAEncryptedNumber = encryptedCardsForBob.ChooseRandom3();

            Console.WriteLine($"Bob chose {cardAEncryptedNumber} and sent to Alice");

            BigInteger cardANumber = CryptoTools.ModuloPower(cardAEncryptedNumber, dA, p);

            Console.WriteLine($"Alice decrypted it; her card number is {cardANumber} and it's {cards.FindName(cardANumber)}!");

            encryptedCardsForBob.RemoveUsedCard();
            Triplet encryptedCardsForAlice = encryptedCardsForBob.ModuloPower(cB, p);

            encryptedCardsForAlice.Mix();
            Console.Write($"Bob encrypted card numbers, mixed them and sent to Alice: ");
            for (int i = 0; i < 3; i++)
            {
                if (encryptedCardsForAlice[i].Number != 0)
                {
                    Console.Write($"{encryptedCardsForAlice[i].Name} to {encryptedCardsForAlice[i].Number} ");
                }
            }
            Console.WriteLine();

            BigInteger cardBEncryptedNumber          = encryptedCardsForAlice.ChooseRandom2();
            BigInteger cardBPartiallyDecryptedNumber = CryptoTools.ModuloPower(cardBEncryptedNumber, dA, p);

            Console.WriteLine($"Alice chose {cardBEncryptedNumber}, found its power {cardBPartiallyDecryptedNumber} and sent to Bob");

            BigInteger cardBNumber = CryptoTools.ModuloPower(cardBPartiallyDecryptedNumber, dB, p);

            Console.WriteLine($"Bob decrypted it; his card number is {cardBNumber} and it's {cards.FindName(cardBNumber)}!");

            encryptedCardsForBob.RemoveUsedCard();
            for (int i = 0; i < 3; i++)
            {
                if (cards[i].Number != cardANumber && cards[i].Number != cardBNumber)
                {
                    Console.WriteLine($"The third card has number {cards[i].Number} and is {cards[i].Name} ");
                }
            }
        }