public void ReplaceData_EncryptedDataNull() { XmlDecryption ex = new XmlDecryption(); XmlDocument doc = new XmlDocument(); doc.LoadXml("<root />"); Assert.Throws <ArgumentNullException>(() => ex.ReplaceData(doc.DocumentElement, null)); }
public void RsaDecryption(string resourceName) { XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; string originalXml; using (Stream stream = TestHelpers.LoadResourceStream(resourceName)) using (StreamReader streamReader = new StreamReader(stream)) { originalXml = streamReader.ReadToEnd(); doc.LoadXml(originalXml); } XmlDecryption encxml = new XmlDecryption(doc); var certificate = TestHelpers.GetSampleX509Certificate(); var rsaKey = certificate.Item2 as RsaKeyParameters; Assert.NotNull(rsaKey); XmlNamespaceManager nm = new XmlNamespaceManager(doc.NameTable); nm.AddNamespace("s", "http://www.w3.org/2003/05/soap-envelope"); nm.AddNamespace("o", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); nm.AddNamespace("e", XmlNameSpace.Url[NS.XmlEncNamespaceUrl]); XmlElement el = doc.SelectSingleNode("/s:Envelope/s:Header/o:Security/e:EncryptedKey", nm) as XmlElement; EncryptedKey ekey = new EncryptedKey(); ekey.LoadXml(el); var rsa = CipherUtilities.GetCipher("RSA//OAEPPADDING"); rsa.Init(false, rsaKey); byte[] key = rsa.DoFinal(ekey.CipherData.CipherValue); var aes = CipherUtilities.GetCipher("AES/CBC/PKCS7PADDING"); var random = new SecureRandom(); var ivdata = new byte[aes.GetBlockSize()]; random.NextBytes(ivdata); var param = new ParametersWithIV(new KeyParameter(key), ivdata); List <XmlElement> elements = new List <XmlElement>(); foreach (XmlElement encryptedDataElement in doc.SelectNodes("//e:EncryptedData", nm)) { elements.Add(encryptedDataElement); } foreach (XmlElement encryptedDataElement in elements) { EncryptedData edata = new EncryptedData(); edata.LoadXml(encryptedDataElement); encxml.ReplaceData(encryptedDataElement, encxml.DecryptData(edata, param)); } }
public void RoundtripSample1() { using (StringWriter sw = new StringWriter()) { { XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.LoadXml("<root> <child>sample</child> </root>"); XmlElement body = doc.DocumentElement; var aes = CipherUtilities.GetCipher("AES/CBC/ZEROBYTEPADDING"); var ivdata = Convert.FromBase64String("pBUM5P03rZ6AE4ZK5EyBrw=="); var keydata = Convert.FromBase64String("o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640="); var param = new ParametersWithIV(new KeyParameter(keydata), ivdata); XmlEncryption exml = new XmlEncryption(); byte[] encrypted = exml.EncryptData(body, param, false); EncryptedData edata = new EncryptedData(); edata.Type = XmlNameSpace.Url[NS.XmlEncElementUrl]; edata.EncryptionMethod = new EncryptionMethod(NS.XmlEncAES256Url); EncryptedKey ekey = new EncryptedKey(); byte[] encKeyBytes = keydata; ekey.CipherData = new CipherData(encKeyBytes); ekey.EncryptionMethod = new EncryptionMethod(NS.XmlEncRSA15Url); DataReference dr = new DataReference(); dr.Uri = "_0"; ekey.AddReference(dr); edata.KeyInfo.AddClause(new KeyInfoEncryptedKey(ekey)); ekey.KeyInfo.AddClause(new RsaKeyValue()); edata.CipherData.CipherValue = encrypted; XmlDecryption.ReplaceElement(doc.DocumentElement, edata, false); doc.Save(new XmlTextWriter(sw)); } { var aes = CipherUtilities.GetCipher("AES/CBC/ZEROBYTEPADDING"); var random = new SecureRandom(); var ivdata = new byte[aes.GetBlockSize()]; var keydata = Convert.FromBase64String("o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640="); random.NextBytes(ivdata); var param = new ParametersWithIV(new KeyParameter(keydata), ivdata); XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.LoadXml(sw.ToString()); XmlDecryption encxml = new XmlDecryption(doc); EncryptedData edata = new EncryptedData(); edata.LoadXml(doc.DocumentElement); encxml.ReplaceData(doc.DocumentElement, encxml.DecryptData(edata, param)); } } }
private void ReplaceEncryptedData(XmlElement encryptedDataElement, byte[] decrypted) { XmlNode parent = encryptedDataElement.ParentNode; if (parent.NodeType == XmlNodeType.Document) { parent.InnerXml = XmlDecryption.GetEncoding().GetString(decrypted); } else { XmlDecryption.ReplaceData(encryptedDataElement, decrypted); } }
private static void Decrypt(XmlDocument doc, ICipherParameters key) { var encryptedElement = (XmlElement)doc.GetElementsByTagName("EncryptedData")[0]; var encryptedData = new EncryptedData(); encryptedData.LoadXml(encryptedElement); var encryptedXml = new XmlDecryption(); byte[] rgbOutput = encryptedXml.DecryptData(encryptedData, key); encryptedXml.ReplaceData(encryptedElement, rgbOutput); }
public void Sample2() { var aes = CipherUtilities.GetCipher("AES/CBC/ZEROBYTEPADDING"); var random = new SecureRandom(); var ivdata = new byte[aes.GetBlockSize()]; var keydata = Convert.FromBase64String("o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640="); random.NextBytes(ivdata); var param = new ParametersWithIV(new KeyParameter(keydata), ivdata); XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.Load(TestHelpers.LoadResourceStream("Org.BouncyCastle.Crypto.Xml.Tests.EncryptedXmlSample2.xml")); XmlDecryption encxml = new XmlDecryption(doc); EncryptedData edata = new EncryptedData(); edata.LoadXml(doc.DocumentElement); encxml.ReplaceData(doc.DocumentElement, encxml.DecryptData(edata, param)); }
public void ReplaceData_XmlElementNull() { XmlDecryption ex = new XmlDecryption(); Assert.Throws <ArgumentNullException>(() => ex.ReplaceData(null, new byte[0])); }