コード例 #1
0
        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));
                }
        }
コード例 #2
0
        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);
                }
        }
コード例 #3
0
        public override IKeyAgreement DiffieHellmanModp14(IExchangeKey privateKey)
        {
            if (privateKey != null)
            {
                return(BCryptDiffieHellman.Import(privateKey));
            }

            return(this.DiffieHellmanModp14());
        }
コード例 #4
0
        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);
                }
            }
        }