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 ManagedExportAgreeswithNativeImportGroup2()
        {
            DiffieHellmanKey managedExport;

            using (var alice = new ManagedDiffieHellmanOakley2())
                using (var bob = new ManagedDiffieHellmanOakley2())
                {
                    managedExport = alice.PrivateKey as DiffieHellmanKey;

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

                    AssertKeysAgree(alice, bob);
                }

            managedExport.Generator = Pad(managedExport.Generator, managedExport.KeyLength);

            using (var alice = BCryptDiffieHellman.Import(managedExport))
                using (var bob = new BCryptDiffieHellmanOakleyGroup2())
                {
                    alice.ImportPartnerKey(bob.PublicKey);
                    bob.ImportPartnerKey(alice.PublicKey);

                    AssertKeysAgree(alice, bob);
                }
        }
Exemple #3
0
        public override IKeyAgreement DiffieHellmanModp14(IExchangeKey privateKey)
        {
            if (privateKey != null)
            {
                return(BCryptDiffieHellman.Import(privateKey));
            }

            return(this.DiffieHellmanModp14());
        }
        public void ManagedExportMatchesNativeImport()
        {
            using (var alice = new ManagedDiffieHellmanOakley2())
            {
                var managedExportPrivate = alice.PrivateKey as DiffieHellmanKey;
                var managedExportPublic  = alice.PublicKey as DiffieHellmanKey;

                managedExportPrivate.Generator = Pad(managedExportPrivate.Generator, managedExportPrivate.KeyLength);
                managedExportPublic.Generator  = Pad(managedExportPublic.Generator, managedExportPublic.KeyLength);

                using (var bob = BCryptDiffieHellman.Import(managedExportPrivate))
                {
                    AssertKeysMatch(alice.PrivateKey, bob.PrivateKey);
                    AssertKeysMatch(alice.PublicKey, bob.PublicKey);

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

                    AssertKeysAgree(alice, bob);
                }
            }
        }