public static T LoadXml(string filename) { using (Stream fs = File.OpenRead(filename)) { return(LoadXml(fs, filename, HealthCheckEncryption.GetAllPrivateKeys())); } }
private static void SaveAsXmlEncrypted(T data, TextWriter outStream) { XmlDocument xmlDoc = GetXmlDocumentClearText(data); XmlElement elementToEncrypt = xmlDoc.DocumentElement; Rijndael sessionKey = null; // Create a 256 bit Rijndael key. sessionKey = new RijndaelManaged(); sessionKey.KeySize = 256; EncryptedXml eXml = new EncryptedXml(); byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false); EncryptedData edElement = new EncryptedData(); edElement.Type = EncryptedXml.XmlEncElementUrl; edElement.Id = elementToEncrypt.Name; edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url); EncryptedKey ek = new EncryptedKey(); byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, HealthCheckEncryption.GetRSAEncryptionKey(), false); ek.CipherData = new CipherData(encryptedKey); ek.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url); edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek)); KeyInfoName kin = new KeyInfoName(); kin.Value = "rsaKey"; ek.KeyInfo.AddClause(kin); edElement.CipherData.CipherValue = encryptedElement; EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false); xmlDoc.Save(outStream); }