/// <summary>
 /// Creates a <see cref="SecurityToken"/> instance from the parsed encrypted <paramref name="securityToken"/>.
 /// The token itself is decrypted using the <see cref="AdvancedEncryptionStandardUtility.Decrypt"/> method.
 /// </summary>
 /// <param name="securityToken">The security token to parse.</param>
 /// <param name="securityKey">The key to use in the decryption algorithm.</param>
 /// <param name="securityInitializationVector">The initialization vector (IV) to use in the decryption algorithm.</param>
 /// <returns>An instance of the <see cref="SecurityToken"/> object.</returns>
 public static SecurityToken ParseEncryptedSecurityToken(byte[] securityToken, byte[] securityKey, byte[] securityInitializationVector)
 {
     return(SecurityToken.Parse(StringConverter.FromBytes(AdvancedEncryptionStandardUtility.Decrypt(securityToken, securityKey, securityInitializationVector), options =>
     {
         options.Encoding = Encoding.UTF8;
         options.Preamble = PreambleSequence.Remove;
     })));
 }
        /// <summary>
        /// Revert the obfuscated XML document of <paramref name="value"/> to its original state by applying the mappable XML document of <paramref name="mapping"/>.
        /// </summary>
        /// <param name="value">The obfuscated <see cref="Stream"/> to revert.</param>
        /// <param name="mapping">A <see cref="Stream"/> containing mappable values necessary to revert <paramref name="value"/> to its original state.</param>
        /// <returns>
        /// A <see cref="Stream"/> object where the obfuscated XML document has been reverted to its original XML document.
        /// </returns>
        public override Stream Revert(Stream value, Stream mapping)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (mapping == null)
            {
                throw new ArgumentNullException(nameof(mapping));
            }

            MemoryStream tempOutput = null;

            try
            {
                XmlDocument document    = XmlDocumentConverter.FromStream(mapping);
                var         mappingNode = document.DocumentElement;

                XmlNode encryptedNode = mappingNode.GetElementsByTagName(MappingEncryptedElement).Item(0);
                if (encryptedNode != null)
                {
                    mappingNode.InnerXml = StringConverter.FromBytes(AdvancedEncryptionStandardUtility.Decrypt(Convert.FromBase64String(encryptedNode.InnerText), Key, InitializationVector), options =>
                    {
                        options.Encoding = Encoding;
                        options.Preamble = PreambleSequence.Remove;
                    });
                }

                tempOutput = new MemoryStream();
                using (XmlWriter writer = XmlWriter.Create(tempOutput, XmlWriterUtility.CreateSettings(o => o.Encoding = Encoding)))
                {
                    document.WriteTo(writer);
                }
                tempOutput.Position = 0;
                mapping             = tempOutput;
                tempOutput          = null;
            }
            finally
            {
                if (tempOutput != null)
                {
                    tempOutput.Dispose();
                }
            }

            return(base.Revert(value, mapping));
        }