コード例 #1
0
        public static void Decrypt(XmlDocument doc, RsaKeyParameters rsaKey, string keyName)
        {
            var encrypted = new XmlDecryption(doc);

            encrypted.AddKeyNameMapping(keyName, rsaKey);
            encrypted.DecryptDocument();
        }
コード例 #2
0
        public void Encrypt_DecryptDocument_AES()
        {
            XmlDocument doc = new XmlDocument();

            doc.PreserveWhitespace = true;
            string xml = "<root>  <child>sample</child>   </root>";

            doc.LoadXml(xml);

            var aes     = CipherUtilities.GetCipher("AES/CBC/ZEROBYTEPADDING");
            var random  = new SecureRandom();
            var ivdata  = new byte[128 / 8];
            var keydata = new byte[256 / 8];

            random.NextBytes(ivdata);
            random.NextBytes(keydata);
            var param = new ParametersWithIV(new KeyParameter(keydata), ivdata);

            XmlEncryption exml = new XmlEncryption();

            exml.AddKeyNameMapping("aes", param);
            EncryptedData ed = exml.Encrypt(doc.DocumentElement, "aes");

            doc.LoadXml(ed.GetXml().OuterXml);
            XmlDecryption exmlDecryptor = new XmlDecryption(doc);

            exmlDecryptor.AddKeyNameMapping("aes", param);
            exmlDecryptor.DecryptDocument();

            Assert.Equal(xml, doc.OuterXml);
        }
コード例 #3
0
        public static void DecryptWithCertificate_NotInStore()
        {
            const string SecretMessage = "Grilled cheese is tasty";

            XmlDocument document = new XmlDocument();

            document.LoadXml($"<data><secret>{SecretMessage}</secret></data>");
            XmlElement toEncrypt = (XmlElement)document.DocumentElement.FirstChild;

            var           cert             = TestHelpers.GetSampleX509Certificate();
            XmlEncryption encryptor        = new XmlEncryption(document);
            EncryptedData encryptedElement = encryptor.Encrypt(toEncrypt, cert.Item1);

            XmlDecryption.ReplaceElement(toEncrypt, encryptedElement, false);

            XmlDocument document2 = new XmlDocument();

            document2.LoadXml(document.OuterXml);

            XmlDecryption decryptor = new XmlDecryption(document2);

            Assert.Throws <System.Security.Cryptography.CryptographicException>(() => decryptor.DecryptDocument());
            Assert.DoesNotContain(SecretMessage, document2.OuterXml);
        }