public XmlElement DecryptSingleElementByKeyNumber(int encryptedKeyNumber)
        {
            EncryptedKey encryptedKey = new EncryptedKey();

            encryptedKey.LoadXml((XmlElement)this._encryptedKeyElements[encryptedKeyNumber]);
            ReferenceList      referenceList      = encryptedKey.ReferenceList;
            EncryptedReference encryptedReference = referenceList.Item(0);
            string             uri     = encryptedReference.Uri;
            KeyInfo            keyInfo = encryptedKey.KeyInfo;

            this._referenceList.Clear();
            ArrayList referenceElementList = new ArrayList();

            referenceElementList = this.FindXmlElementByURI(uri, this._tempdocument.ChildNodes[1]);
            XmlElement keyInfoElement = this._tempdocument.CreateElement("KeyInfo", SignedXml.XmlDsigNamespaceUrl);

            keyInfoElement.AppendChild(_tempdocument.ImportNode((XmlNode)encryptedKey.GetXml(), true));
            XmlElement encryptedDataElement   = (XmlElement)referenceElementList[0];
            RSACryptoServiceProvider provider = this._webService.RSACryptoServiceProvider;
            EncryptedXml             encXml   = new EncryptedXml(this._tempdocument);

            encXml.AddKeyNameMapping("Web Service Public Key", provider);
            EncryptedData data = new EncryptedData();

            data.LoadXml((XmlElement)encryptedDataElement);
            SymmetricAlgorithm algo = SymmetricAlgorithm.Create();

            algo.Key = encXml.DecryptEncryptedKey(encryptedKey);
            byte[] t = encXml.DecryptData(data, algo);
            encXml.ReplaceData(encryptedDataElement, t);
            this._tempdocument.GetElementsByTagName("wsse:Security")[0].RemoveChild(_tempdocument.GetElementsByTagName("xenc:EncryptedKey")[0]);
            XmlElement root = (XmlElement)this._decryptedDataList[encryptedKeyNumber];

            return((XmlElement)root);
        }
        public void DecryptEncryptedKey_Empty()
        {
            EncryptedXml ex = new EncryptedXml();
            EncryptedKey ek = new EncryptedKey();

            Assert.Null(ex.DecryptEncryptedKey(ek));
        }
        public void DecryptEncryptedKey_KeyInfoEncryptedKey()
        {
            XmlDocument doc = new XmlDocument();

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

            doc.LoadXml(xml);

            var random  = new SecureRandom();
            var keydata = new byte[256 / 8];

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

            keydata = new byte[128 / 8];
            random.NextBytes(keydata);
            var innerParam = new KeyParameter(keydata);

            keydata = new byte[192 / 8];
            random.NextBytes(keydata);
            var outerParam = new KeyParameter(keydata);

            EncryptedXml exml = new EncryptedXml(doc);

            exml.AddKeyNameMapping("aes", param);

            EncryptedKey ekey = new EncryptedKey();

            byte[] encKeyBytes = EncryptedXml.EncryptKey(outerParam.GetKey(), param);
            ekey.CipherData       = new CipherData(encKeyBytes);
            ekey.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
            ekey.Id      = "Key_ID";
            ekey.KeyInfo = new KeyInfo();
            ekey.KeyInfo.AddClause(new KeyInfoName("aes"));

            KeyInfo topLevelKeyInfo = new KeyInfo();

            topLevelKeyInfo.AddClause(new KeyInfoEncryptedKey(ekey));

            EncryptedKey ekeyTopLevel = new EncryptedKey();

            byte[] encTopKeyBytes = EncryptedXml.EncryptKey(innerParam.GetKey(), outerParam);
            ekeyTopLevel.CipherData       = new CipherData(encTopKeyBytes);
            ekeyTopLevel.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
            ekeyTopLevel.KeyInfo          = topLevelKeyInfo;

            doc.LoadXml(ekeyTopLevel.GetXml().OuterXml);

            byte[] decryptedKey = exml.DecryptEncryptedKey(ekeyTopLevel);
            Assert.Equal(innerParam.GetKey(), decryptedKey);

            EncryptedData eData = new EncryptedData();

            eData.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
            eData.KeyInfo          = topLevelKeyInfo;
            var decryptedAlg = exml.GetDecryptionKey(eData, null);

            Assert.Equal(outerParam.GetKey(), ((KeyParameter)decryptedAlg).GetKey());
        }
        private string DercryptSingleXmlElement(XmlElement encryptedKeyElement)
        {
            EncryptedKey encryptdKey = new EncryptedKey();

            encryptdKey.LoadXml(encryptedKeyElement);
            ReferenceList      referenceList      = encryptdKey.ReferenceList;
            EncryptedReference encryptedReference = referenceList.Item(0);
            string             uri     = encryptedReference.Uri;
            KeyInfo            keyInfo = encryptdKey.KeyInfo;

            this._referenceList.Clear();
            ArrayList  referenceElementList = this.FindXmlElementByURI(uri, this._inputDocument.ChildNodes[1]);
            XmlElement keyInfoElement       = this._inputDocument.CreateElement("KeyInfo", SignedXml.XmlDsigNamespaceUrl);

            keyInfoElement.AppendChild(encryptedKeyElement);
            if (referenceElementList.Count > 0)
            {
                XmlElement encryptedDataElement   = (XmlElement)referenceElementList[0];
                RSACryptoServiceProvider provider = this._webService.RSACryptoServiceProvider;
                XmlDocument doc  = new XmlDocument();
                XmlElement  root = doc.CreateElement("root");
                root.AppendChild(doc.ImportNode((XmlNode)encryptedKeyElement, true));
                root.AppendChild(doc.ImportNode(encryptedDataElement, true));
                doc.AppendChild(root);
                EncryptedXml encxml2 = new EncryptedXml(doc);
                EncryptedKey encKey2 = new EncryptedKey();
                encKey2.LoadXml((XmlElement)doc.GetElementsByTagName("xenc:EncryptedKey")[0]);
                EncryptedData encData2        = new EncryptedData();
                EncryptedData encDataElement2 = new EncryptedData();
                XmlElement    data2           = (XmlElement)doc.GetElementsByTagName("xenc:EncryptedData")[0];
                encDataElement2.LoadXml((XmlElement)doc.GetElementsByTagName("xenc:EncryptedData")[0]);
                encxml2.AddKeyNameMapping("Web Service Public Key", provider);
                SymmetricAlgorithm algo2 = SymmetricAlgorithm.Create();
                algo2.Key = encxml2.DecryptEncryptedKey(encKey2);
                byte[] t2 = encxml2.DecryptData(encDataElement2, algo2);
                encxml2.ReplaceData(data2, t2);
                doc.GetElementsByTagName("root")[0].RemoveChild(doc.GetElementsByTagName("xenc:EncryptedKey")[0]);
                this._tracer.appendDecryptedData(uri, doc.FirstChild.InnerXml);
                EncryptedXml encXml = new EncryptedXml(this._inputDocument);
                encXml.AddKeyNameMapping("Web Service Public Key", provider);
                EncryptedData data = new EncryptedData();
                data.LoadXml((XmlElement)encryptedDataElement);
                SymmetricAlgorithm algo = SymmetricAlgorithm.Create();
                algo.Key = encXml.DecryptEncryptedKey(encryptdKey);
                byte[] t = encXml.DecryptData(data, algo);
                encXml.ReplaceData(encryptedDataElement, t);
                this._encryptedDataList.Add(encryptedDataElement);
                this._decryptedDataList.Add(doc.GetElementsByTagName("root")[0]);
                this._encryptedKeyElements.Add(encryptedKeyElement);
                string decryptedXmlString;
                return(decryptedXmlString = Convert.ToBase64String(t));
            }
            return(string.Empty);
        }
        public void DecryptEncryptedKey_KeyInfoEncryptedKey()
        {
            XmlDocument doc = new XmlDocument();

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

            doc.LoadXml(xml);

            using (Aes aes = Aes.Create())
                using (Aes outerAes = Aes.Create())
                    using (Aes innerAes = Aes.Create())
                    {
                        outerAes.KeySize = 192;
                        innerAes.KeySize = 128;

                        EncryptedXml exml = new EncryptedXml(doc);
                        exml.AddKeyNameMapping("aes", aes);

                        EncryptedKey ekey        = new EncryptedKey();
                        byte[]       encKeyBytes = EncryptedXml.EncryptKey(outerAes.Key, aes);
                        ekey.CipherData       = new CipherData(encKeyBytes);
                        ekey.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
                        ekey.Id      = "Key_ID";
                        ekey.KeyInfo = new KeyInfo();
                        ekey.KeyInfo.AddClause(new KeyInfoName("aes"));

                        KeyInfo topLevelKeyInfo = new KeyInfo();
                        topLevelKeyInfo.AddClause(new KeyInfoEncryptedKey(ekey));

                        EncryptedKey ekeyTopLevel   = new EncryptedKey();
                        byte[]       encTopKeyBytes = EncryptedXml.EncryptKey(innerAes.Key, outerAes);
                        ekeyTopLevel.CipherData       = new CipherData(encTopKeyBytes);
                        ekeyTopLevel.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
                        ekeyTopLevel.KeyInfo          = topLevelKeyInfo;

                        doc.LoadXml(ekeyTopLevel.GetXml().OuterXml);

                        byte[] decryptedKey = exml.DecryptEncryptedKey(ekeyTopLevel);
                        Assert.Equal(innerAes.Key, decryptedKey);

                        EncryptedData eData = new EncryptedData();
                        eData.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
                        eData.KeyInfo          = topLevelKeyInfo;
                        SymmetricAlgorithm decryptedAlg = exml.GetDecryptionKey(eData, null);
                        Assert.Equal(outerAes.Key, decryptedAlg.Key);
                    }
        }
Exemple #6
0
        public void DecryptEncryptedKey_Null()
        {
            EncryptedXml ex = new EncryptedXml();

            ex.DecryptEncryptedKey(null);
        }
Exemple #7
0
        public void DecryptEncryptedKey_Null()
        {
            EncryptedXml ex = new EncryptedXml();

            Assert.Throws <ArgumentNullException>(() => ex.DecryptEncryptedKey(null));
        }