Ejemplo n.º 1
0
        public void Encrypt_Decrypt_RoundTrips()
        {
            // Arrange
            var symmetricAlgorithm = TripleDES.Create();

            symmetricAlgorithm.GenerateKey();

            var mockInternalEncryptor = new Mock <IInternalCertificateXmlEncryptor>();

            mockInternalEncryptor.Setup(o => o.PerformEncryption(It.IsAny <EncryptedXml>(), It.IsAny <XmlElement>()))
            .Returns <EncryptedXml, XmlElement>((encryptedXml, element) =>
            {
                encryptedXml.AddKeyNameMapping("theKey", symmetricAlgorithm);     // use symmetric encryption
                return(encryptedXml.Encrypt(element, "theKey"));
            });

            var mockInternalDecryptor = new Mock <IInternalEncryptedXmlDecryptor>();

            mockInternalDecryptor.Setup(o => o.PerformPreDecryptionSetup(It.IsAny <EncryptedXml>()))
            .Callback <EncryptedXml>(encryptedXml =>
            {
                encryptedXml.AddKeyNameMapping("theKey", symmetricAlgorithm);     // use symmetric encryption
            });

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IInternalEncryptedXmlDecryptor>(mockInternalDecryptor.Object);

            var services  = serviceCollection.BuildServiceProvider();
            var encryptor = new CertificateXmlEncryptor(NullLoggerFactory.Instance, mockInternalEncryptor.Object);
            var decryptor = new EncryptedXmlDecryptor(services);

            var originalXml = XElement.Parse(@"<mySecret value='265ee4ea-ade2-43b1-b706-09b259e58b6b' />");

            // Act & assert - run through encryptor and make sure we get back <EncryptedData> element
            var encryptedXmlInfo = encryptor.Encrypt(originalXml);

            Assert.Equal(typeof(EncryptedXmlDecryptor), encryptedXmlInfo.DecryptorType);
            Assert.Equal(XName.Get("EncryptedData", "http://www.w3.org/2001/04/xmlenc#"), encryptedXmlInfo.EncryptedElement.Name);
            Assert.Equal("http://www.w3.org/2001/04/xmlenc#Element", (string)encryptedXmlInfo.EncryptedElement.Attribute("Type"));
            Assert.DoesNotContain("265ee4ea-ade2-43b1-b706-09b259e58b6b", encryptedXmlInfo.EncryptedElement.ToString(), StringComparison.OrdinalIgnoreCase);

            // Act & assert - run through decryptor and make sure we get back the original value
            var roundTrippedElement = decryptor.Decrypt(encryptedXmlInfo.EncryptedElement);

            XmlAssert.Equal(originalXml, roundTrippedElement);
        }
        public void Encrypt_Decrypt_RoundTrips()
        {
            // Arrange
            var symmetricAlgorithm = new TripleDESCryptoServiceProvider();
            symmetricAlgorithm.GenerateKey();

            var serviceCollection = new ServiceCollection();
            var mockInternalEncryptor = new Mock<IInternalCertificateXmlEncryptor>();
            mockInternalEncryptor.Setup(o => o.PerformEncryption(It.IsAny<EncryptedXml>(), It.IsAny<XmlElement>()))
                .Returns<EncryptedXml, XmlElement>((encryptedXml, element) =>
                {
                    encryptedXml.AddKeyNameMapping("theKey", symmetricAlgorithm); // use symmetric encryption
                    return encryptedXml.Encrypt(element, "theKey");
                });
            serviceCollection.AddSingleton<IInternalCertificateXmlEncryptor>(mockInternalEncryptor.Object);

            var mockInternalDecryptor = new Mock<IInternalEncryptedXmlDecryptor>();
            mockInternalDecryptor.Setup(o => o.PerformPreDecryptionSetup(It.IsAny<EncryptedXml>()))
                .Callback<EncryptedXml>(encryptedXml =>
                {
                    encryptedXml.AddKeyNameMapping("theKey", symmetricAlgorithm); // use symmetric encryption
                });
            serviceCollection.AddSingleton<IInternalEncryptedXmlDecryptor>(mockInternalDecryptor.Object);

            var services = serviceCollection.BuildServiceProvider();
            var encryptor = new CertificateXmlEncryptor(services);
            var decryptor = new EncryptedXmlDecryptor(services);

            var originalXml = XElement.Parse(@"<mySecret value='265ee4ea-ade2-43b1-b706-09b259e58b6b' />");

            // Act & assert - run through encryptor and make sure we get back <EncryptedData> element
            var encryptedXmlInfo = encryptor.Encrypt(originalXml);
            Assert.Equal(typeof(EncryptedXmlDecryptor), encryptedXmlInfo.DecryptorType);
            Assert.Equal(XName.Get("EncryptedData", "http://www.w3.org/2001/04/xmlenc#"), encryptedXmlInfo.EncryptedElement.Name);
            Assert.Equal("http://www.w3.org/2001/04/xmlenc#Element", (string)encryptedXmlInfo.EncryptedElement.Attribute("Type"));
            Assert.DoesNotContain("265ee4ea-ade2-43b1-b706-09b259e58b6b", encryptedXmlInfo.EncryptedElement.ToString(), StringComparison.OrdinalIgnoreCase);

            // Act & assert - run through decryptor and make sure we get back the original value
            var roundTrippedElement = decryptor.Decrypt(encryptedXmlInfo.EncryptedElement);
            XmlAssert.Equal(originalXml, roundTrippedElement);
        }