/// <summary> /// Imports the <see cref="AuthenticatedEncryptorDescriptor"/> from serialized XML. /// </summary> public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element) { if (element == null) { throw new ArgumentNullException(nameof(element)); } // <descriptor> // <encryption algorithm="..." /> // <validation algorithm="..." /> <!-- only if not GCM --> // <masterKey requiresEncryption="true">...</masterKey> // </descriptor> var settings = new AuthenticatedEncryptionSettings(); var encryptionElement = element.Element("encryption"); settings.EncryptionAlgorithm = (EncryptionAlgorithm)Enum.Parse(typeof(EncryptionAlgorithm), (string)encryptionElement.Attribute("algorithm")); // only read <validation> if not GCM if (!AuthenticatedEncryptionSettings.IsGcmAlgorithm(settings.EncryptionAlgorithm)) { var validationElement = element.Element("validation"); settings.ValidationAlgorithm = (ValidationAlgorithm)Enum.Parse(typeof(ValidationAlgorithm), (string)validationElement.Attribute("algorithm")); } Secret masterKey = ((string)element.Elements("masterKey").Single()).ToSecret(); return(new AuthenticatedEncryptorDescriptor(settings, masterKey, _services)); }
public XmlSerializedDescriptorInfo ExportToXml() { // <descriptor> // <encryption algorithm="..." /> // <validation algorithm="..." /> <!-- only if not GCM --> // <masterKey requiresEncryption="true">...</masterKey> // </descriptor> var encryptionElement = new XElement("encryption", new XAttribute("algorithm", Settings.EncryptionAlgorithm)); var validationElement = (AuthenticatedEncryptionSettings.IsGcmAlgorithm(Settings.EncryptionAlgorithm)) ? (object)new XComment(" AES-GCM includes a 128-bit authentication tag, no extra validation algorithm required. ") : (object)new XElement("validation", new XAttribute("algorithm", Settings.ValidationAlgorithm)); var outerElement = new XElement("descriptor", encryptionElement, validationElement, MasterKey.ToMasterKeyElement()); return(new XmlSerializedDescriptorInfo(outerElement, typeof(AuthenticatedEncryptorDescriptorDeserializer))); }