Esempio n. 1
0
        /// <summary>
        /// Decrypts all EncryptedData elements of the XML document that were specified
        /// during initialization of the System.Security.Cryptography.Xml.EncryptedXml class.
        /// </summary>
        /// <param name="document">The xml document containing the element to decrypt.</param>
        /// <param name="algorithm">The symmetric alogorithm used to decrypt the element.</param>
        /// <param name="keyName">The name to map to keyObject.</param>
        public static void Decrypt(XmlDocument document, SymmetricAlgorithm algorithm, string keyName)
        {
            // Check the arguments.
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            if (string.IsNullOrEmpty(keyName))
            {
                throw new ArgumentNullException("keyName");
            }

            if (document == null)
            {
                throw new ArgumentNullException("algorithm");
            }

            // Create a new EncryptedXml object.
            EncryptedXml exml = new EncryptedXml(document);

            // Add a key-name mapping.
            // This method can only decrypt documents
            // that present the specified key name.
            exml.AddKeyNameMapping(keyName, algorithm);

            // Decrypt the element.
            exml.DecryptDocument();
        }
Esempio n. 2
0
        static void decipher(string filename, XmlDocument xmlDoc, List <RSA> Keys)
        {
            // Create a new EncryptedXml object.
            EncryptedXml exml = new EncryptedXml(xmlDoc);
            // Add a key-name mapping.
            // This method can only decrypt documents
            // that present the specified key name.
            int keyid = 0;

            foreach (RSA Alg in Keys)
            {
                try
                {
                    exml.ClearKeyNameMappings();
                    Trace.WriteLine("Trying to decrypt with keyid " + keyid++);
                    exml.AddKeyNameMapping("rsaKey", Alg);
                    // Decrypt the element.
                    exml.DecryptDocument();
                    return;
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("When decoding the document - trying next key: " + ex.Message);
                }
            }
            Trace.WriteLine("The program tried to use " + keyid + " keys");
            throw new PingCastleDataException(filename, "Unable to find a key in the configuration which can decrypt the document");
        }
        public static void Decrypt(XmlDocument Doc, AsymmetricAlgorithm privateKey)
        {
            EncryptedXml exml = new EncryptedXml(Doc);

            exml.AddKeyNameMapping("encKey", privateKey);
            exml.DecryptDocument();
        }
Esempio n. 4
0
        public static void Decrypt(XmlDocument Doc, RSA Alg, string KeyName)
        {
            try
            {
                // Check the arguments.   
                if (Doc == null)
                    throw new ArgumentNullException("Doc");
                if (Alg == null)
                    throw new ArgumentNullException("Alg");
                if (KeyName == null)
                    throw new ArgumentNullException("KeyName");

                // Create a new EncryptedXml object.
                EncryptedXml exml = new EncryptedXml(Doc);

                // Add a key-name mapping. 
                // This method can only decrypt documents 
                // that present the specified key name.
                exml.AddKeyNameMapping(KeyName, Alg);

                // Decrypt the element.
                exml.DecryptDocument();
            }
            catch (Exception ex)
            {
                AutoClosingMessageBox.Show("Sorry!! License is not Valid.");
            }
        }
Esempio n. 5
0
        public static void Decrypt(XmlDocument Doc, RSA Alg, string KeyName)
        {
            // Verificare argumente
            if (Doc == null)
            {
                throw new ArgumentNullException("Doc");
            }
            if (Alg == null)
            {
                throw new ArgumentNullException("Alg");
            }
            if (KeyName == null)
            {
                throw new ArgumentNullException("KeyName");
            }

            // Se creeaza un nou obiect de tip EncryptedXml.
            EncryptedXml exml = new EncryptedXml(Doc);

            // Se adauga o mapare numeCheie - algoritm
            exml.AddKeyNameMapping(KeyName, Alg);

            // Se decripteaza
            exml.DecryptDocument();
        }
Esempio n. 6
0
        /// <summary>
        /// Método para descriptografia
        /// </summary>
        /// <param name="doc">arquivo XML</param>
        /// <param name="alg">RSA</param>
        /// <param name="keyName">chave</param>
        public static void Decrypt(XmlDocument doc, RSA alg, string keyName)
        {
            // Check the arguments.
            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }
            if (alg == null)
            {
                throw new ArgumentNullException("alg");
            }
            if (keyName == null)
            {
                throw new ArgumentNullException("keyName");
            }

            // Create a new EncryptedXml object.
            EncryptedXml exml = new EncryptedXml(doc);

            // Add a key-name mapping.
            // This method can only decrypt documents
            // that present the specified key name.
            exml.AddKeyNameMapping(keyName, alg);

            // Decrypt the element.
            exml.DecryptDocument();
        }
        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);

            EncryptedXml exml = new EncryptedXml();

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

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

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

            Assert.Equal(xml, doc.OuterXml);
        }
Esempio n. 8
0
//<SNIPPET1>
    public static void Decrypt(XmlDocument Doc, RSA Alg, string KeyName)
    {
        // Check the arguments.
        if (Doc == null)
        {
            throw new ArgumentNullException("Doc");
        }
        if (Alg == null)
        {
            throw new ArgumentNullException("Alg");
        }
        if (KeyName == null)
        {
            throw new ArgumentNullException("KeyName");
        }

        // Create a new EncryptedXml object.
        EncryptedXml exml = new EncryptedXml(Doc);

        // Add a key-name mapping.
        // This method can only decrypt documents
        // that present the specified key name.
        exml.AddKeyNameMapping(KeyName, Alg);

        while (Doc.GetElementsByTagName("EncryptedData", EncryptedXml.XmlEncNamespaceUrl).Count > 0)
        {
            // Decrypt the element.
            exml.DecryptDocument();
        }
    }
        /// <summary>
        /// Decrypts the specified XML element.
        /// </summary>
        /// <param name="encryptedElement">An encrypted XML element.</param>
        /// <returns>The decrypted form of <paramref name="encryptedElement"/>.</returns>
        /// <remarks>
        public XElement Decrypt(XElement encryptedElement)
        {
            if (encryptedElement == null)
            {
                throw new ArgumentNullException(nameof(encryptedElement));
            }

            // <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
            //   ...
            // </EncryptedData>

            // 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", encryptedElement).CreateReader());
            var elementToDecrypt = (XmlElement)xmlDocument.DocumentElement.FirstChild;

            // Perform the decryption and update the document in-place.
            var encryptedXml = new EncryptedXml(xmlDocument);

            _decryptor.PerformPreDecryptionSetup(encryptedXml);
            encryptedXml.DecryptDocument();

            // Strip the <root /> element back off and convert the XmlDocument to an XElement.
            return(XElement.Load(xmlDocument.DocumentElement.FirstChild.CreateNavigator().ReadSubtree()));
        }
Esempio n. 10
0
        private static void Decrypt(XmlDocument doc, RSA rsaKey, string keyName)
        {
            var encrypted = new EncryptedXml(doc);

            encrypted.AddKeyNameMapping(keyName, rsaKey);
            encrypted.DecryptDocument();
        }
Esempio n. 11
0
        public static void Decrypt(XmlDocument Doc, RSA Alg, string KeyName)
        {
            //////////////////////////////////////////////////////////////////////
            // Check the arguments
            //

            if (Doc == null)
            {
                throw new ArgumentNullException("Doc");
            }
            if (Alg == null)
            {
                throw new ArgumentNullException("Alg");
            }
            if (KeyName == null)
            {
                throw new ArgumentNullException("KeyName");
            }


            //////////////////////////////////////////////////////////////////////
            // Create a new EncryptedXml object to decrypt data
            //

            EncryptedXml exml = new EncryptedXml(Doc);

            // Add a key-name mapping.
            // This method can only decrypt documents
            // that present the specified key name.
            exml.AddKeyNameMapping(KeyName, Alg);
            // Decrypt the element.
            exml.DecryptDocument();
        }
        /// <summary>
        /// Decrypts the specified XML element.
        /// </summary>
        /// <param name="encryptedElement">
        /// An encrypted XML element.
        /// </param>
        /// <returns>
        /// The decrypted form of <paramref name="encryptedElement" />.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="encryptedElement" /> is <see langword="null" />.
        /// </exception>
        public XElement Decrypt(XElement encryptedElement)
        {
            if (encryptedElement == null)
            {
                throw new ArgumentNullException(nameof(encryptedElement));
            }

            this.Logger.LogDebug("Decrypting XML with certificate {0}.", this._keyName);

            // Create a faux XML document from the XElement so we can use EncryptedXml.
            var xmlDocument = encryptedElement.ToXmlDocumentWithRootNode();

            // Do the actual decryption. Algorithm based on MSDN docs:
            // https://msdn.microsoft.com/en-us/library/ms229746(v=vs.110).aspx
            var encryptedXml = new EncryptedXml(xmlDocument);

            encryptedXml.AddKeyNameMapping(this._keyName, this._keyProvider);

            try
            {
                encryptedXml.DecryptDocument();
            }
            catch (CryptographicException ex) when(ex.Message.IndexOf("bad key", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                // If you get a CryptographicException with the message "Bad Key"
                // in it here, it means the certificate used to encrypt wasn't generated
                // with "-sky Exchange" in makecert.exe so the encrypt/decrypt functionality
                // isn't enabled for it.
                this.Logger.LogError("Bad key exception was encountered. Did you generate the certificate with '-sky Exchange' to enable encryption/decryption?");
                throw;
            }

            return(xmlDocument.ElementToProcess().ToXElement());
        }
Esempio n. 13
0
        private static void Decrypt(XmlDocument doc, SymmetricAlgorithm key, string keyName)
        {
            var encrypted = new EncryptedXml(doc);

            encrypted.AddKeyNameMapping(keyName, key);
            encrypted.DecryptDocument();
        }
Esempio n. 14
0
        public bool Decrypt(string filePath)
        {
            bool error;

            if (filePath == null)
            {
                throw new ArgumentNullException("Doc");
            }
            else
            {
                XmlDoc.Load(filePath);
            }

            if (File.Exists(GetKeyFile()))
            {
                loadKey();

                // this will execute successfully even if the file was not encrypted
                EncryptedXml exml = new EncryptedXml(XmlDoc);
                exml.AddKeyNameMapping(KeyName, Alg);
                exml.DecryptDocument();

                // If you do not save the file then the encryption is not saved
                XmlDoc.Save(filePath);
                error = false;
            }
            else
            {
                error = true;
            }
            return(error);
        }
Esempio n. 15
0
    public static void Decrypt(XmlDocument Doc, SymmetricAlgorithm Alg, string KeyName)
    {
        // Check the arguments.
        if (Doc == null)
        {
            throw new ArgumentNullException("Doc");
        }
        if (Alg == null)
        {
            throw new ArgumentNullException("Alg");
        }
        if (KeyName == null)
        {
            throw new ArgumentNullException("KeyName");
        }

        // Create a new EncryptedXml object.
        EncryptedXml exml = new EncryptedXml(Doc);

        // Add the key name mapping.
        exml.AddKeyNameMapping(KeyName, Alg);

        // Decrypt the XML document.
        exml.DecryptDocument();
    }
        public void Decrypt(X509Certificate2 certificate, bool useFakeKeyName)
        {
            if (useFakeKeyName)
            {
                KeyInfoName keyInfoName = new KeyInfoName(kkKey);

                XmlNodeList encryptedKeyNodes = docValue.GetElementsByTagName("EncryptedKey");
                XmlNode     encryptedKeyNode  = encryptedKeyNodes.Item(0);
                XmlNode     referenceNode     = encryptedKeyNode.FirstChild;

                XmlElement infoElement    = docValue.CreateElement(null, "KeyInfo", "http://www.w3.org/2000/09/xmldsig#");
                XmlElement keyNameElement = docValue.CreateElement(null, "KeyName", "http://www.w3.org/2000/09/xmldsig#");
                keyNameElement.InnerText = kkKey;
                infoElement.AppendChild(keyNameElement);

                encryptedKeyNode.InsertAfter(infoElement, referenceNode);

                encryptedKeyNode.ParentNode.AppendChild(encryptedKeyNode);
            }

            EncryptedXml exml = new EncryptedXml(docValue);

            RSACryptoServiceProvider privateKeyProvider = (RSACryptoServiceProvider)certificate.PrivateKey;

            exml.AddKeyNameMapping(kkKey, privateKeyProvider);

            exml.DecryptDocument();
        }
Esempio n. 17
0
        public static XmlDocument Decrypt(XmlDocument Doc, string KeyName)
        {
            CspParameters cspParams = new CspParameters();

            cspParams.KeyContainerName = "XML_ENC_RSA_KEY";
            RSA Alg = new RSACryptoServiceProvider(cspParams);

            // Check the arguments.
            if (Doc == null)
            {
                throw new ArgumentNullException("Doc");
            }
            if (KeyName == null)
            {
                throw new ArgumentNullException("KeyName");
            }

            // Create a new EncryptedXml object.
            EncryptedXml exml = new EncryptedXml(Doc);

            // Add a key-name mapping.
            // This method can only decrypt documents
            // that present the specified key name.
            exml.AddKeyNameMapping(KeyName, Alg);

            // Decrypt the element.
            exml.DecryptDocument();

            return(Doc);
        }
Esempio n. 18
0
        public static void Decrypt(XmlDocument doc, RsaKeyParameters rsaKey, string keyName)
        {
            var encrypted = new EncryptedXml(doc);

            encrypted.AddKeyNameMapping(keyName, rsaKey);
            encrypted.DecryptDocument();
        }
Esempio n. 19
0
        public static SmtpAddress Decrypt(XmlElement encryptedSharingKey, SymmetricSecurityKey symmetricSecurityKey)
        {
            XmlDocument xmlDocument = new SafeXmlDocument();

            try
            {
                xmlDocument.AppendChild(xmlDocument.ImportNode(encryptedSharingKey, true));
            }
            catch (XmlException)
            {
                SharingKeyHandler.Tracer.TraceError <string>(0L, "Unable to import XML element of sharing key: {0}", encryptedSharingKey.OuterXml);
                return(SmtpAddress.Empty);
            }
            EncryptedXml encryptedXml = new EncryptedXml(xmlDocument);

            encryptedXml.AddKeyNameMapping("key", symmetricSecurityKey.GetSymmetricAlgorithm("http://www.w3.org/2001/04/xmlenc#tripledes-cbc"));
            try
            {
                encryptedXml.DecryptDocument();
            }
            catch (CryptographicException)
            {
                SharingKeyHandler.Tracer.TraceError <string>(0L, "Unable to decrypt XML element sharing key: {0}", encryptedSharingKey.OuterXml);
                return(SmtpAddress.Empty);
            }
            return(new SmtpAddress(xmlDocument.DocumentElement.InnerText));
        }
Esempio n. 20
0
        private static void Decrypt(SymmetricAlgorithm key, XmlDocument xmlDoc)
        {
            var encryptedXml = new EncryptedXml(xmlDoc);

            encryptedXml.AddKeyNameMapping("MyKey", key);

            encryptedXml.DecryptDocument();
        }
        /// <summary>
        /// Decrypt data with X509 certificate
        /// </summary>
        /// <param name="encryptedNode"></param>
        /// <returns></returns>
        public override System.Xml.XmlNode Decrypt(System.Xml.XmlNode encryptedNode)
        {
            XmlDocument  doc  = encryptedNode.OwnerDocument;
            EncryptedXml eXml = new EncryptedXml(doc);

            eXml.DecryptDocument();
            return(doc.DocumentElement);
        }
Esempio n. 22
0
        public XDocument Decrypt()
        {
            EncryptedXml exml = new EncryptedXml(xmlDoc);

            exml.AddKeyNameMapping(KeyName, rsaKey);
            exml.DecryptDocument();
            return(xmlDoc.ToXDocument());
        }
    // Performs provider initialization.
    #region Public Methods and Operators

    public override XmlNode Decrypt(XmlNode encryptedNode)
    {
        // Load config section to encrypt into xmlDocument instance
        XmlDocument  doc  = encryptedNode.OwnerDocument;
        EncryptedXml eXml = new EncryptedXml(doc);

        eXml.DecryptDocument();
        return(doc.DocumentElement);
    }
Esempio n. 24
0
        public static void Decrypt(XmlDocument Doc)
        {
            if (Doc == null)
            {
                throw new ArgumentNullException("Doc");
            }
            EncryptedXml exml = new EncryptedXml(Doc);

            exml.DecryptDocument();
        }
        protected virtual XmlDocument DecryptKeyBlobXml(string encryptedXmlString)
        {
            XmlDocument xmlDocument = new SafeXmlDocument();

            xmlDocument.LoadXml(encryptedXmlString);
            EncryptedXml encryptedXml = new EncryptedXml(xmlDocument);

            encryptedXml.DecryptDocument();
            this.ThrowIfKeyBlobXmlFailsSchemaValidation(xmlDocument);
            return(xmlDocument);
        }
Esempio n. 26
0
        /// <summary>
        /// 解密xml文档
        /// </summary>
        /// <param name="Doc">被解密的xml文档</param>
        /// <param name="Alg">不对称算法对象</param>
        /// <param name="KeyName">解密需要的RSA密钥名称</param>
        private void Decrypt(XmlDocument Doc, RSACryptoServiceProvider Alg, string KeyName)
        {
            //创建 EncryptedXml 对象以对文档进行解密
            EncryptedXml exml = new EncryptedXml(Doc);

            //添加密钥/名称映射,以将RSA密钥与要解密的文档中的元素关
            //联起来。用于密钥的名称必须与加密文档时使用的密钥名称相同
            exml.AddKeyNameMapping(KeyName, Alg);
            //解密元素
            exml.DecryptDocument();
        }
        public override XmlNode Decrypt(XmlNode encryptedNode)
        {
            // Load config section to encrypt into xmlDocument instance
            XmlDocument  doc  = encryptedNode.OwnerDocument;
            EncryptedXml eXml = new EncryptedXml(doc);

            // Add a key-name mapping. This method can only decrypt documents that present the specified key name.
            eXml.AddKeyNameMapping(this.keyName, this.rsaKey);

            eXml.DecryptDocument();
            return(doc.DocumentElement);
        }
Esempio n. 28
0
        public static XmlElement Decrypt(XmlElement xmlElement, SymmetricSecurityKey symmetricSecurityKey)
        {
            XmlDocument xmlDocument = new SafeXmlDocument();
            XmlNode     newChild    = xmlDocument.ImportNode(xmlElement, true);

            xmlDocument.AppendChild(newChild);
            EncryptedXml encryptedXml = new EncryptedXml(xmlDocument);

            encryptedXml.AddKeyNameMapping("key", symmetricSecurityKey.GetSymmetricAlgorithm("http://www.w3.org/2001/04/xmlenc#tripledes-cbc"));
            encryptedXml.DecryptDocument();
            return(xmlDocument.DocumentElement);
        }
Esempio n. 29
0
        protected virtual XmlReader DecryptDocumentAndCreateXmlReader(XmlDocument document)
        {
            // Perform the actual decryption step, updating the XmlDocument in-place.
            EncryptedXml encryptedXml = _encryptedXmlFactory?.Invoke(document) ?? new EncryptedXml(document);

            encryptedXml.DecryptDocument();

            // Finally, return the new XmlReader from the updated XmlDocument.
            // Error messages based on this XmlReader won't show line numbers,
            // but that's fine since we transformed the document anyway.
            return(document.CreateNavigator() !.ReadSubtree());
        }
Esempio n. 30
0
        public string Decrypt(string encryptedContent, RSA rsaKey)
        {
            var xmlEncrypteDocument = new XmlDocument();

            xmlEncrypteDocument.LoadXml(encryptedContent);

            var encryptedXml = new EncryptedXml(xmlEncrypteDocument);

            encryptedXml.AddKeyNameMapping(KeyName, rsaKey);
            encryptedXml.DecryptDocument();

            return(xmlEncrypteDocument.OuterXml);
        }