public void ImportPrivateKey()
        {
            IExchangeKey aliceKey = null;
            IExchangeKey bobKey   = null;

            byte[] firstPassDerivedKey = null;

            using (var alice = new BCryptDiffieHellmanOakleyGroup14())
                using (var bob = new BCryptDiffieHellmanOakleyGroup14())
                {
                    aliceKey = alice.PrivateKey;
                    bobKey   = bob.PrivateKey;

                    alice.ImportPartnerKey(bob.PublicKey);
                    bob.ImportPartnerKey(alice.PublicKey);

                    AssertKeysAgree(alice, bob);

                    firstPassDerivedKey = alice.GenerateAgreement().ToArray();
                }

            using (var alice = BCryptDiffieHellman.Import(aliceKey))
                using (var bob = BCryptDiffieHellman.Import(bobKey))
                {
                    alice.ImportPartnerKey(bob.PublicKey);
                    bob.ImportPartnerKey(alice.PublicKey);

                    var aliceDerived = alice.GenerateAgreement();

                    AssertKeysAgree(alice, bob);

                    Assert.IsTrue(aliceDerived.Span.SequenceEqual(firstPassDerivedKey));
                }
        }
        public void ManagedAgreesWithNativeGroup14()
        {
            using (var alice = new BCryptDiffieHellmanOakleyGroup14())
                using (var bob = new ManagedDiffieHellmanOakley14())
                {
                    alice.ImportPartnerKey(bob.PublicKey);
                    bob.ImportPartnerKey(alice.PublicKey);

                    AssertKeysAgree(alice, bob);
                }
        }
        public ReadOnlyMemory <byte> Modp14()
        {
            using (var alice = new BCryptDiffieHellmanOakleyGroup14())
                using (var bob = new BCryptDiffieHellmanOakleyGroup14())
                {
                    alice.ImportPartnerKey(bob.PublicKey);
                    bob.ImportPartnerKey(alice.PublicKey);

                    return(alice.GenerateAgreement());
                }
        }
        public void Oakley14_KeyAgreement()
        {
            using (var alice = new BCryptDiffieHellmanOakleyGroup14())
                using (var bob = new BCryptDiffieHellmanOakleyGroup14())
                {
                    Assert.IsFalse(bob.PublicKey.PublicComponent.Span.SequenceEqual(alice.PublicKey.PublicComponent.Span));

                    alice.ImportPartnerKey(GetRightPublicKey(alice.PublicKey, bob.PublicKey));
                    bob.ImportPartnerKey(GetRightPublicKey(bob.PublicKey, alice.PublicKey));

                    AssertKeysAgree(alice, bob);
                }
        }
        public void DHParametersAsBigInteger()
        {
            using (var eve = new BCryptDiffieHellmanOakleyGroup14())
            {
                var evePubKey = eve.PublicKey as DiffieHellmanKey;

                var eveMod = new BigInteger(evePubKey.Modulus.Span.ToArray());
                var eveGen = new BigInteger(evePubKey.Generator.Span.ToArray());
                var evePub = new BigInteger(evePubKey.PublicComponent.Span.ToArray());

                for (var i = 0; i < 100; i++)
                {
                    using (var alice = new BCryptDiffieHellmanOakleyGroup14())
                        using (var bob = new BCryptDiffieHellmanOakleyGroup14())
                        {
                            alice.ImportPartnerKey(bob.PublicKey);
                            bob.ImportPartnerKey(alice.PublicKey);

                            AssertKeysAgree(alice, bob);

                            var alicePubKey = alice.PublicKey as DiffieHellmanKey;
                            var bobPubKey   = bob.PublicKey as DiffieHellmanKey;

                            var aliceMod = new BigInteger(alicePubKey.Modulus.Span.ToArray());
                            var bobMod   = new BigInteger(bobPubKey.Modulus.Span.ToArray());

                            var alicePub = new BigInteger(alice.PublicKey.PublicComponent.Span.ToArray());
                            var bobPub   = new BigInteger(bob.PublicKey.PublicComponent.Span.ToArray());

                            Assert.AreEqual(aliceMod, bobMod);
                            Assert.AreNotEqual(alicePub, bobPub);

                            Assert.AreEqual(aliceMod, eveMod);
                        }
                }
            }
        }