Пример #1
0
 public void Encrypt(string xmlFileName)
 {
     TripleDESCryptoServiceProvider encryptionKey = new TripleDESCryptoServiceProvider();
     XmlDocument xmlDoc = new XmlDocument();
     xmlDoc.Load(xmlFileName);
     encryptionKey.Key = UTF8Encoding.UTF8.GetBytes("");  // your salt value
     XmlElement orderElem = xmlDoc.SelectSingleNode("Settings") as XmlElement;
     EncryptedXml encXml = new EncryptedXml(xmlDoc);
     byte[] encryptedOrder = encXml.EncryptData(orderElem, encryptionKey, false);
     EncryptedData encryptedData = new EncryptedData();
     encryptedData.Type = EncryptedXml.XmlEncElementUrl;
     encryptedData.EncryptionMethod = new
     EncryptionMethod(EncryptedXml.XmlEncTripleDESUrl);
     encryptedData.CipherData = new CipherData();
     encryptedData.CipherData.CipherValue = encryptedOrder;
     EncryptedXml.ReplaceElement(orderElem, encryptedData, false);
     xmlDoc.Save(xmlFileName);
 }
Пример #2
0
        public void DecryptEncryptedKey_Null()
        {
            EncryptedXml ex = new EncryptedXml();

            ex.DecryptEncryptedKey(null);
        }
Пример #3
0
        /// <summary>
        /// Entschlüsselung einer verschlüsselten XML Struktur
        /// </summary>
        /// <param name="Doc"></param>
        /// <param name="keyStore"></param>
        /// <param name="keyStorePasswort"></param>
        private static void DecryptXML(XmlDocument Doc, Pkcs12Store keyStore, string keyStorePasswort)
        {
            System.Security.Cryptography.X509Certificates.X509Certificate2 cert =
                new System.Security.Cryptography.X509Certificates.X509Certificate2(CertHelper.ConvertPkcs12ToByteArray(keyStore, keyStorePasswort), keyStorePasswort);


            //Heranziehen des privaten Schlüssels:

            RSACryptoServiceProvider privateKeyProvider = null;

            privateKeyProvider = (RSACryptoServiceProvider)(cert.PrivateKey);


            //Kompatibilität Java Anfang -->

            XmlElement keyNameNode = null;
            string     keyName     = "rsaKeyName";

            foreach (string keyNameName in GetNodeKeyNameCollection())
            {
                keyNameNode = Doc.GetElementsByTagName(keyNameName)[0] as XmlElement;
                if (keyNameNode != null)
                {
                    break;
                }
            }

            if (keyNameNode == null)
            {
                XmlElement encryptedKey = null;
                foreach (string encryptionKeyName in GetNodeEncryptionKeyNameCollection())
                {
                    encryptedKey = Doc.GetElementsByTagName(encryptionKeyName)[0] as XmlElement;
                    if (encryptedKey != null)
                    {
                        break;
                    }
                }

                if (encryptedKey != null)
                {
                    XmlElement elementKeyMethod = null;

                    foreach (string elementKeyMethodName in GetNodeEncryptionMethodCollection())
                    {
                        elementKeyMethod = encryptedKey.GetElementsByTagName(elementKeyMethodName)[0] as XmlElement;
                        if (elementKeyMethod != null)
                        {
                            break;
                        }
                    }

                    if (elementKeyMethod != null)
                    {
                        XmlElement elementKeyInfo = Doc.CreateElement("KeyInfo", SignedXml.XmlDsigNamespaceUrl);
                        XmlElement elementKeyName = Doc.CreateElement("KeyName", SignedXml.XmlDsigNamespaceUrl);
                        elementKeyName.InnerText = keyName;
                        elementKeyInfo.AppendChild(elementKeyName);
                        encryptedKey.InsertAfter(elementKeyInfo, elementKeyMethod);
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(keyNameNode.InnerText))
                {
                    keyName = keyNameNode.InnerText;
                }
                else
                {
                    keyName = "";
                }
            }
            // Kompatibilität Java Ende <--

            //Entschlüsselung:
            EncryptedXml exml = new EncryptedXml(Doc);

            exml.AddKeyNameMapping(keyName, privateKeyProvider);
            exml.DecryptDocument();
        }
Пример #4
0
        public void DecryptData_SymmetricAlgorithmNull()
        {
            EncryptedXml ex = new EncryptedXml();

            ex.DecryptData(new EncryptedData(), null);
        }
Пример #5
0
        public void EncryptData_SymmetricAlgorithmNull()
        {
            EncryptedXml ex = new EncryptedXml();

            ex.EncryptData(new byte[16], null);
        }
Пример #6
0
        public void GetDecryptionIV_EncryptedDataNull()
        {
            EncryptedXml ex = new EncryptedXml();

            ex.GetDecryptionIV(null, EncryptedXml.XmlEncAES128Url);
        }
Пример #7
0
 public void EncryptKey_KeyNull()
 {
     EncryptedXml.EncryptKey(null, Rijndael.Create());
 }
Пример #8
0
        public void DecryptEncryptedKey_Null()
        {
            EncryptedXml ex = new EncryptedXml();

            Assert.Throws <ArgumentNullException>(() => ex.DecryptEncryptedKey(null));
        }
        /// <summary>
        /// An example on how to decrypt an encrypted assertion.
        /// </summary>
        /// <param name="file">The file.</param>
        public static void DecryptAssertion(string file)
        {
            var doc = new XmlDocument();

            doc.Load(file);
            var encryptedDataElement = GetElement(Schema.XEnc.EncryptedData.ElementName, Saml20Constants.Xenc, doc);

            var encryptedData = new EncryptedData();

            encryptedData.LoadXml(encryptedDataElement);

            var nodelist = doc.GetElementsByTagName(Schema.XmlDSig.KeyInfo.ElementName, Saml20Constants.Xmldsig);

            Assert.That(nodelist.Count > 0);

            var key = new KeyInfo();

            key.LoadXml((XmlElement)nodelist[0]);

            // Review: Is it possible to figure out which certificate to load based on the Token?

            /*
             * Comment:
             * It would be possible to provide a key/certificate identifier in the EncryptedKey element, which contains the "recipient" attribute.
             * The implementation (Safewhere.Tokens.Saml20.Saml20EncryptedAssertion) currently just expects an appropriate asymmetric key to be provided,
             * and is not not concerned about its origin.
             * If the need arises, we can easily extend the Saml20EncryptedAssertion class with a property that allows extraction key info, eg. the "recipient"
             * attribute.
             */
            var cert = new X509Certificate2(@"Certificates\sts_dev_certificate.pfx", "test1234");

            // ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref18/html/T_System_Security_Cryptography_Xml_KeyInfoClause_DerivedTypes.htm
            // Look through the list of KeyInfo elements to find the encrypted key.
            SymmetricAlgorithm symmetricKey = null;

            foreach (KeyInfoClause keyInfoClause in key)
            {
                if (keyInfoClause is KeyInfoEncryptedKey)
                {
                    var keyInfoEncryptedKey = (KeyInfoEncryptedKey)keyInfoClause;
                    var encryptedKey        = keyInfoEncryptedKey.EncryptedKey;
                    symmetricKey = new RijndaelManaged
                    {
                        Key = EncryptedXml.DecryptKey(encryptedKey.CipherData.CipherValue, (RSA)cert.PrivateKey, false)
                    };
                }
            }

            // Explode if we didn't manage to find a viable key.
            Assert.IsNotNull(symmetricKey);
            var encryptedXml = new EncryptedXml();
            var plaintext    = encryptedXml.DecryptData(encryptedData, symmetricKey);

            var assertion = new XmlDocument();

            assertion.Load(new StringReader(System.Text.Encoding.UTF8.GetString(plaintext)));

            // A very simple test to ensure that there is indeed an assertion in the plaintext.
            Assert.AreEqual(Assertion.ElementName, assertion.DocumentElement.LocalName);
            Assert.AreEqual(Saml20Constants.Assertion, assertion.DocumentElement.NamespaceURI);

            // At this point, assertion will contain a decrypted assertion.
        }
Пример #10
0
 public void EncryptKey_RSA_RSANull()
 {
     Assert.Throws <ArgumentNullException>(() => EncryptedXml.EncryptKey(new byte[16], null, false));
 }
Пример #11
0
        public void EncryptData_SymmetricAlgorithmNull()
        {
            EncryptedXml ex = new EncryptedXml();

            Assert.Throws <ArgumentNullException>(() => ex.EncryptData(new byte[16], null));
        }
Пример #12
0
 public void EncryptKey_SymmetricAlgorithmNull()
 {
     Assert.Throws <ArgumentNullException>(() => EncryptedXml.EncryptKey(new byte[16], null));
 }
Пример #13
0
        public void GetDecryptionIV_EncryptedDataNull()
        {
            EncryptedXml ex = new EncryptedXml();

            Assert.Throws <ArgumentNullException>(() => ex.GetDecryptionIV(null, EncryptedXml.XmlEncAES128Url));
        }
Пример #14
0
        public void GetIdElement_StringNull()
        {
            EncryptedXml ex = new EncryptedXml();

            Assert.Throws <ArgumentNullException>(() => ex.GetIdElement(new XmlDocument(), null));
        }
Пример #15
0
 public void ReplaceElement_XmlElementNull()
 {
     Assert.Throws <ArgumentNullException>(() => EncryptedXml.ReplaceElement(null, new EncryptedData(), true));
 }
Пример #16
0
        public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, string EncryptionElementID, RSA Alg, string KeyName)
        {
            if (Doc == null)
            {
                throw new ArgumentNullException("Doc");
            }
            if (ElementToEncrypt == null)
            {
                throw new ArgumentNullException("ElementToEncrypt");
            }
            if (EncryptionElementID == null)
            {
                throw new ArgumentNullException("EncryptionElementID");
            }
            if (Alg == null)
            {
                throw new ArgumentNullException("Alg");
            }
            if (KeyName == null)
            {
                throw new ArgumentNullException("KeyName");
            }

            XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;

            if (elementToEncrypt == null)
            {
                throw new XmlException("The specified element was not found");
            }
            Aes sessionKey = null;

            try
            {
                EncryptedXml  eXml      = new EncryptedXml();
                EncryptedData edElement = new EncryptedData();
                EncryptedKey  ek        = new EncryptedKey();
                DataReference dRef      = new DataReference();
                KeyInfoName   kin       = new KeyInfoName();

                sessionKey = Aes.Create();

                byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false);

                edElement.Type             = EncryptedXml.XmlEncElementUrl;
                edElement.Id               = EncryptionElementID;
                edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);

                byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, Alg, false);

                ek.CipherData       = new CipherData(encryptedKey);
                ek.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url);
                dRef.Uri            = "#" + EncryptionElementID;
                ek.AddReference(dRef);
                edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));
                kin.Value = KeyName;
                ek.KeyInfo.AddClause(kin);
                edElement.CipherData.CipherValue = encryptedElement;

                EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (sessionKey != null)
                {
                    sessionKey.Clear();
                }
            }
        }
Пример #17
0
        public void GetIdElement_WhenElementNameMustBeNonColonizedAndItContainsColon_ReturnsNull()
        {
            var sut = new EncryptedXml();

            Assert.That(sut.GetIdElement(new XmlDocument(), "t:test"), Is.Null);
        }
Пример #18
0
    public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, RSA Alg, string KeyName)
    {
        // Check the arguments.
        if (Doc == null)
        {
            throw new ArgumentNullException("Doc");
        }
        if (ElementToEncrypt == null)
        {
            throw new ArgumentNullException("ElementToEncrypt");
        }
        if (Alg == null)
        {
            throw new ArgumentNullException("Alg");
        }

        ////////////////////////////////////////////////
        // Find the specified element in the XmlDocument
        // object and create a new XmlElemnt object.
        ////////////////////////////////////////////////

        XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;

        // Throw an XmlException if the element was not found.
        if (elementToEncrypt == null)
        {
            throw new XmlException("The specified element was not found");
        }

        //////////////////////////////////////////////////
        // Create a new instance of the EncryptedXml class
        // and use it to encrypt the XmlElement with the
        // a new random symmetric key.
        //////////////////////////////////////////////////

        // Create a 256 bit Rijndael key.
        RijndaelManaged sessionKey = new RijndaelManaged();

        sessionKey.KeySize = 256;

        EncryptedXml eXml = new EncryptedXml();

        byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false);

        ////////////////////////////////////////////////
        // Construct an EncryptedData object and populate
        // it with the desired encryption information.
        ////////////////////////////////////////////////


        EncryptedData edElement = new EncryptedData();

        edElement.Type = EncryptedXml.XmlEncElementUrl;

        // Create an EncryptionMethod element so that the
        // receiver knows which algorithm to use for decryption.

        edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);

        // Encrypt the session key and add it to an EncryptedKey element.
        EncryptedKey ek = new EncryptedKey();

        byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, Alg, false);

        ek.CipherData = new CipherData(encryptedKey);

        ek.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url);

        // Set the KeyInfo element to specify the
        // name of the RSA key.

        // Create a new KeyInfo element.
        edElement.KeyInfo = new KeyInfo();

        // Create a new KeyInfoName element.
        KeyInfoName kin = new KeyInfoName();

        // Specify a name for the key.
        kin.Value = KeyName;

        // Add the KeyInfoName element to the
        // EncryptedKey object.
        ek.KeyInfo.AddClause(kin);

        // Add the encrypted key to the
        // EncryptedData object.

        edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));

        // Add the encrypted element data to the
        // EncryptedData object.
        edElement.CipherData.CipherValue = encryptedElement;

        ////////////////////////////////////////////////////
        // Replace the element from the original XmlDocument
        // object with the EncryptedData element.
        ////////////////////////////////////////////////////

        EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
    }
Пример #19
0
        public void GetDecryptionIV_StringNull()
        {
            EncryptedXml ex = new EncryptedXml();

            Assert.IsNull(ex.GetDecryptionIV(new EncryptedData(), null));
        }
Пример #20
0
        /// <summary>
        /// 加密 XML 文件
        /// </summary>
        /// <param name="Doc"></param>
        /// <param name="ElementToEncrypt"></param>
        /// <param name="EncryptionElementID"></param>
        /// <param name="Alg"></param>
        /// <param name="KeyName"></param>
        public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, string EncryptionElementID, RSA Alg, string KeyName)
        {
            // Check the arguments.
            if (Doc == null)
            {
                throw new ArgumentNullException("Doc");
            }
            if (ElementToEncrypt == null)
            {
                throw new ArgumentNullException("ElementToEncrypt");
            }
            if (EncryptionElementID == null)
            {
                throw new ArgumentNullException("EncryptionElementID");
            }
            if (Alg == null)
            {
                throw new ArgumentNullException("Alg");
            }
            if (KeyName == null)
            {
                throw new ArgumentNullException("KeyName");
            }

            ////////////////////////////////////////////////
            // Find the specified element in the XmlDocument
            // object and create a new XmlElemnt object.
            ////////////////////////////////////////////////
            XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;

            // Throw an XmlException if the element was not found.
            if (elementToEncrypt == null)
            {
                throw new XmlException("The specified element was not found");
            }
            RijndaelManaged sessionKey = null;

            try
            {
                //////////////////////////////////////////////////
                // Create a new instance of the EncryptedXml class
                // and use it to encrypt the XmlElement with the
                // a new random symmetric key.
                //////////////////////////////////////////////////

                // Create a 256 bit Rijndael key.
                sessionKey         = new RijndaelManaged();
                sessionKey.KeySize = 256;

                EncryptedXml eXml = new EncryptedXml();

                byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false);
                ////////////////////////////////////////////////
                // Construct an EncryptedData object and populate
                // it with the desired encryption information.
                ////////////////////////////////////////////////

                EncryptedData edElement = new EncryptedData();
                edElement.Type = EncryptedXml.XmlEncElementUrl;
                edElement.Id   = EncryptionElementID;
                // Create an EncryptionMethod element so that the
                // receiver knows which algorithm to use for decryption.

                edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
                // Encrypt the session key and add it to an EncryptedKey element.
                EncryptedKey ek = new EncryptedKey();

                byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, Alg, false);

                ek.CipherData = new CipherData(encryptedKey);

                ek.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url);

                // Create a new DataReference element
                // for the KeyInfo element.  This optional
                // element specifies which EncryptedData
                // uses this key.  An XML document can have
                // multiple EncryptedData elements that use
                // different keys.
                DataReference dRef = new DataReference();

                // Specify the EncryptedData URI.
                dRef.Uri = "#" + EncryptionElementID;

                // Add the DataReference to the EncryptedKey.
                ek.AddReference(dRef);
                // Add the encrypted key to the
                // EncryptedData object.

                edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));
                // Set the KeyInfo element to specify the
                // name of the RSA key.

                // Create a new KeyInfo element.
                edElement.KeyInfo = new KeyInfo();

                // Create a new KeyInfoName element.
                KeyInfoName kin = new KeyInfoName();

                // Specify a name for the key.
                kin.Value = KeyName;

                // Add the KeyInfoName element to the
                // EncryptedKey object.
                ek.KeyInfo.AddClause(kin);


                edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));

                // Add the encrypted element data to the
                // EncryptedData object.
                edElement.CipherData.CipherValue = encryptedElement;
                ////////////////////////////////////////////////////
                // Replace the element from the original XmlDocument
                // object with the EncryptedData element.
                ////////////////////////////////////////////////////
                EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
            }
            catch (Exception e)
            {
                // re-throw the exception.
                throw e;
            }
            finally
            {
                if (sessionKey != null)
                {
                    sessionKey.Clear();
                }
            }
        }
Пример #21
0
 public void EncryptKey_SymmetricAlgorithmNull()
 {
     EncryptedXml.EncryptKey(new byte [16], null);
 }
Пример #22
0
        public override XmlNode Encrypt(XmlNode node)
        {
            XmlDocument  xmlDocument;
            EncryptedXml exml;

            byte[]                   rgbOutput;
            EncryptedData            ed;
            KeyInfoName              kin;
            EncryptedKey             ek;
            KeyInfoEncryptedKey      kek;
            XmlElement               inputElement;
            RSACryptoServiceProvider rsa = GetCryptoServiceProvider(false, false);

            // Encrypt the node with the new key
            xmlDocument = new XmlDocument();
            xmlDocument.PreserveWhitespace = true;
            xmlDocument.LoadXml("<foo>" + node.OuterXml + "</foo>");
            exml         = new EncryptedXml(xmlDocument);
            inputElement = xmlDocument.DocumentElement;

            using (SymmetricAlgorithm symAlg = GetSymAlgorithmProvider()) {
                rgbOutput           = exml.EncryptData(inputElement, symAlg, true);
                ed                  = new EncryptedData();
                ed.Type             = EncryptedXml.XmlEncElementUrl;
                ed.EncryptionMethod = GetSymEncryptionMethod();
                ed.KeyInfo          = new KeyInfo();

                ek = new EncryptedKey();
                ek.EncryptionMethod       = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url);
                ek.KeyInfo                = new KeyInfo();
                ek.CipherData             = new CipherData();
                ek.CipherData.CipherValue = EncryptedXml.EncryptKey(symAlg.Key, rsa, UseOAEP);
            }

            kin       = new KeyInfoName();
            kin.Value = _KeyName;
            ek.KeyInfo.AddClause(kin);
            kek = new KeyInfoEncryptedKey(ek);
            ed.KeyInfo.AddClause(kek);
            ed.CipherData             = new CipherData();
            ed.CipherData.CipherValue = rgbOutput;
            EncryptedXml.ReplaceElement(inputElement, ed, true);

            rsa.Clear();

            // Get node from the document
            foreach (XmlNode node2 in xmlDocument.ChildNodes)
            {
                if (node2.NodeType == XmlNodeType.Element)
                {
                    foreach (XmlNode node3 in node2.ChildNodes) // node2 is the "foo" node
                    {
                        if (node3.NodeType == XmlNodeType.Element)
                        {
                            return(node3); // node3 is the "EncryptedData" node
                        }
                    }
                }
            }
            return(null);
        }
Пример #23
0
        public void EncryptData_DataNull()
        {
            EncryptedXml ex = new EncryptedXml();

            ex.EncryptData(null, Rijndael.Create());
        }
Пример #24
0
        public void ReplaceData_XmlElementNull()
        {
            EncryptedXml ex = new EncryptedXml();

            ex.ReplaceData(null, new byte[0]);
        }
Пример #25
0
        public void EncryptData_XmlElementNull()
        {
            EncryptedXml ex = new EncryptedXml();

            ex.EncryptData(null, Rijndael.Create(), true);
        }
Пример #26
0
 public void ReplaceElement_XmlElementNull()
 {
     EncryptedXml.ReplaceElement(null, new EncryptedData(), true);
 }
Пример #27
0
    public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, SymmetricAlgorithm Alg)
    {
        // Check the arguments.
        if (Doc == null)
        {
            throw new ArgumentNullException("Doc");
        }
        if (ElementToEncrypt == null)
        {
            throw new ArgumentNullException("ElementToEncrypt");
        }
        if (Alg == null)
        {
            throw new ArgumentNullException("Alg");
        }

        ////////////////////////////////////////////////
        // Find the specified element in the XmlDocument
        // object and create a new XmlElemnt object.
        ////////////////////////////////////////////////

        XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;

        // Throw an XmlException if the element was not found.
        if (elementToEncrypt == null)
        {
            throw new XmlException("The specified element was not found");
        }

        //////////////////////////////////////////////////
        // Create a new instance of the EncryptedXml class
        // and use it to encrypt the XmlElement with the
        // symmetric key.
        //////////////////////////////////////////////////

        EncryptedXml eXml = new EncryptedXml();

        byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, Alg, false);

        ////////////////////////////////////////////////
        // Construct an EncryptedData object and populate
        // it with the desired encryption information.
        ////////////////////////////////////////////////


        EncryptedData edElement = new EncryptedData();

        edElement.Type = EncryptedXml.XmlEncElementUrl;

        // Create an EncryptionMethod element so that the
        // receiver knows which algorithm to use for decryption.
        // Determine what kind of algorithm is being used and
        // supply the appropriate URL to the EncryptionMethod element.

        string encryptionMethod = null;

        if (Alg is TripleDES)
        {
            encryptionMethod = EncryptedXml.XmlEncTripleDESUrl;
        }
        else if (Alg is DES)
        {
            encryptionMethod = EncryptedXml.XmlEncDESUrl;
        }
        else if (Alg is Rijndael)
        {
            switch (Alg.KeySize)
            {
            case 128:
                encryptionMethod = EncryptedXml.XmlEncAES128Url;
                break;

            case 192:
                encryptionMethod = EncryptedXml.XmlEncAES192Url;
                break;

            case 256:
                encryptionMethod = EncryptedXml.XmlEncAES256Url;
                break;
            }
        }
        else
        {
            // Throw an exception if the transform is not in the previous categories
            throw new CryptographicException("The specified algorithm is not supported for XML Encryption.");
        }

        edElement.EncryptionMethod = new EncryptionMethod(encryptionMethod);

        // Add the encrypted element data to the
        // EncryptedData object.
        edElement.CipherData.CipherValue = encryptedElement;

        ////////////////////////////////////////////////////
        // Replace the element from the original XmlDocument
        // object with the EncryptedData element.
        ////////////////////////////////////////////////////

        EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
    }
Пример #28
0
        public void ReplaceElement_EncryptedDataNull()
        {
            XmlDocument doc = new XmlDocument();

            EncryptedXml.ReplaceElement(doc.DocumentElement, null, false);
        }
Пример #29
0
 public void DecryptKey_NotSupportedAlgorithm()
 {
     Assert.Throws <CryptographicException>(() => EncryptedXml.DecryptKey(new byte[16], new NotSupportedSymmetricAlgorithm()));
 }
Пример #30
0
        public void GetIdElement_XmlDocumentNull()
        {
            EncryptedXml ex = new EncryptedXml();

            Assert.IsNull(ex.GetIdElement(null, "value"));
        }
Пример #31
0
 public void Decrypt(string xmlFileName)
 {
     TripleDESCryptoServiceProvider encryptionKey = new TripleDESCryptoServiceProvider();
     encryptionKey.Key = UTF8Encoding.UTF8.GetBytes("");  // your salt value
     XmlDocument document = new XmlDocument();
     document.Load(xmlFileName);
     XmlElement encOrderElem = document.GetElementsByTagName("EncryptedData")[0] as XmlElement;
     EncryptedData encData = new EncryptedData();
     encData.LoadXml(encOrderElem);
     EncryptedXml encryptedXml = new EncryptedXml();
     byte[] decryptedOrder = encryptedXml.DecryptData(encData, encryptionKey);
     encryptedXml.ReplaceData(encOrderElem, decryptedOrder);
     document.Save(xmlFileName);
 }
Пример #32
0
        public void ReplaceData_XmlElementNull()
        {
            EncryptedXml ex = new EncryptedXml();

            Assert.Throws <ArgumentNullException>(() => ex.ReplaceData(null, new byte[0]));
        }