Esempio n. 1
0
        private XElement EncryptElement(XElement plaintextElement)
        {
            // EncryptedXml works with XmlDocument, not XLinq. When we perform the conversion
            // we'll wrap the incoming element in a dummy <root /> element since encrypted XML
            // doesn't handle encrypting the root element all that well.
            var xmlDocument = new XmlDocument();

            xmlDocument.Load(new XElement("root", plaintextElement).CreateReader());
            var elementToEncrypt = (XmlElement)xmlDocument.DocumentElement.FirstChild;

            // Perform the encryption and update the document in-place.
            var encryptedXml  = new EncryptedXml(xmlDocument);
            var encryptedData = _encryptor.PerformEncryption(encryptedXml, elementToEncrypt);

            EncryptedXml.ReplaceElement(elementToEncrypt, encryptedData, content: false);

            // Strip the <root /> element back off and convert the XmlDocument to an XElement.
            return(XElement.Load(xmlDocument.DocumentElement.FirstChild.CreateNavigator().ReadSubtree()));
        }