public override void Save(object value, string sectionName, string fileName)
        {
            XmlNode xmlNode = this.Serialize(value);
            XmlDocument document = new XmlDocument();
            document.AppendChild(document.ImportNode(xmlNode,true));

            // Encrypt xml
            EncryptXml enc = new EncryptXml(document);
            enc.AddKeyNameMapping("db", ReadServerEncryptionKey());

            XmlNodeList list = document.SelectNodes("//Password");

            foreach ( XmlNode n in list )
            {
                XmlElement el = (XmlElement)n;
                EncryptedData data = enc.Encrypt(el, "db");
                enc.ReplaceElement(el, data);
            }

            XmlTextWriter writer = new XmlTextWriter(fileName, null);
            writer.Formatting = Formatting.Indented;
            document.WriteTo(writer);
            writer.Flush();
            writer.Close();
        }
        public override object Load(string sectionName, string fileName)
        {
            XmlDocument document = new XmlDocument();
            document.Load(fileName);

            // add EncryptedData to new document
            XmlNode node = document.SelectSingleNode("//EncryptedData");

            if ( node != null )
            {
                // decrypt
                EncryptXml decrypt = new EncryptXml(document);
                decrypt.AddKeyNameMapping("db", ReadServerEncryptionKey());
                decrypt.DecryptDocument();

                // create object (desearilize)
                AccountMessageSerializer serializer = new AccountMessageSerializer();
                AccountDatabase database = (AccountDatabase)Create(document);

                return database;
            }
            else
            {
                return base.Load (sectionName, fileName);
            }
        }
        /// <summary>
        /// Decrypts the body object.
        /// </summary>
        /// <param name="envelope"> The SoapEnvelope.</param>
        /// <returns> The XmlDocument with the decrypted Soap Body object.</returns>
        private XmlDocument DecryptBodyObject(SoapEnvelope envelope)
        {
            // add to new document
            XmlNode node = envelope.SelectSingleNode("//EncryptedData");
            XmlDocument document = new XmlDocument();
            document.AppendChild(document.ImportNode(node,true));

            // decrypt
            EcyXmlEncryption.EncryptXml decrypt = new EcyXmlEncryption.EncryptXml(document);
            decrypt.AddKeyNameMapping("accountMessage", ReadTransportKey());
            decrypt.DecryptDocument();

            return document;
        }
        /// <summary>
        /// Decrypts the body object.
        /// </summary>
        /// <param name="envelope"> The SoapEnvelope.</param>
        /// <returns> The XmlDocument with the decrypted Soap Body object.</returns>
        private XmlDocument Decrypt(XmlDocument encryptedDocument)
        {
            // add to new document
            XmlNode node = encryptedDocument.SelectSingleNode("//EncryptedData");
            XmlDocument document = new XmlDocument();
            document.AppendChild(document.ImportNode(node,true));

            // decrypt
            EcyXmlEncryption.EncryptXml decrypt = new EcyXmlEncryption.EncryptXml(document);
            decrypt.AddKeyNameMapping("scriptingApplication", decrypt.GetMachineStoreKey("Ecyware.ScrAppEncryption"));
            decrypt.DecryptDocument();

            return document;
        }
        /// <summary>
        /// Decrypts a XmlDocument representing a secure login credentials.
        /// </summary>
        /// <param name="encryptedDocument"> A XmlDocument type.</param>
        /// <returns> A decrypted secure login credentials.</returns>
        public static SecureLoginCredentials Load()
        {
            XmlDocument encryptedDocument = new XmlDocument();
            encryptedDocument.Load(location);

            // add to new document
            XmlNode node = encryptedDocument.SelectSingleNode("//EncryptedData");
            SecureLoginCredentials sec = null;
            SecureLoginCredentialsSerializer serializer = new SecureLoginCredentialsSerializer();

            if ( node != null )
            {
                XmlDocument document = new XmlDocument();
                document.AppendChild(document.ImportNode(node,true));

                // decrypt
                EncryptXml decrypt = new EncryptXml(document);
                decrypt.AddKeyNameMapping("slcreds", decrypt.GetMachineStoreKey("Ecyware.SecLogCreds"));
                decrypt.DecryptDocument();

                sec = (SecureLoginCredentials)serializer.Create(document.DocumentElement.OuterXml);
            }

            return sec;
        }
        /// <summary>
        /// Saves the scripting application.
        /// </summary>
        public void Save()
        {
            try
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(this.ToXml());

                // Encrypt xml
                EncryptXml enc = new EncryptXml(document);
                enc.AddKeyNameMapping("slcreds", enc.CreateMachineStoreKey("Ecyware.SecLogCreds"));

                XmlElement el = (XmlElement)document.FirstChild;
                EncryptedData data = enc.Encrypt(el, "slcreds");
                enc.ReplaceElement(el, data);

                document.Save(location);
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// Encrypts the scripting application.
        /// </summary>
        /// <returns> A encrypted XML string.</returns>
        public string Encrypt()
        {
            try
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(this.ToXml());

                // Encrypt xml
                EncryptXml enc = new EncryptXml(document);
                enc.AddKeyNameMapping("scriptingApplication", enc.CreateMachineStoreKey("Ecyware.ScrAppEncryption"));

                XmlElement el = (XmlElement)document.FirstChild;
                EncryptedData data = enc.Encrypt(el, "scriptingApplication");
                enc.ReplaceElement(el, data);

                return document.DocumentElement.OuterXml;
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// Decrypts a XmlDocument representing a scripting application.
        /// </summary>
        /// <param name="encryptedDocument"> A XmlDocument type.</param>
        /// <returns> A decrypted Scripting Application.</returns>
        public static ScriptingApplication Decrypt(XmlDocument encryptedDocument)
        {
            // add to new document
            XmlNode node = encryptedDocument.SelectSingleNode("//EncryptedData");
            ScriptingApplication scrapp = null;
            ScriptingApplicationSerializer serializer = new ScriptingApplicationSerializer();

            if ( node != null )
            {
                XmlDocument document = new XmlDocument();
                document.AppendChild(document.ImportNode(node,true));

                // decrypt
                EncryptXml decrypt = new EncryptXml(document);
                decrypt.AddKeyNameMapping("scriptingApplication", decrypt.GetMachineStoreKey("Ecyware.ScrAppEncryption"));
                decrypt.DecryptDocument();

                scrapp = (ScriptingApplication)serializer.Create(document.DocumentElement.OuterXml);
            }
            else
            {
                scrapp = (ScriptingApplication)serializer.Create(encryptedDocument.DocumentElement.OuterXml);
            }

            return scrapp;
        }
        /// <summary>
        /// Encrypts the account message.
        /// </summary>
        /// <param name="message"> The AccountMessage.</param>
        /// <returns> A encrypted XML String.</returns>
        private string EncryptAccountMessage(AccountMessage message)
        {
            try
            {
                AccountMessageSerializer serializer = new AccountMessageSerializer();
                XmlNode xmlNode = serializer.Serialize(message);
                XmlDocument document = new XmlDocument();
                document.AppendChild(document.ImportNode(xmlNode,true));

                // Encrypt xml
                EcyXmlEncryption.EncryptXml enc = new EcyXmlEncryption.EncryptXml(document);
                enc.AddKeyNameMapping("accountMessage", ReadKey());

                XmlElement el = (XmlElement)document.SelectSingleNode("/AccountMessage");
                EcyXmlEncryption.EncryptedData data = enc.Encrypt(el, "accountMessage");
                enc.ReplaceElement(el, data);

                return document.DocumentElement.OuterXml;
            }
            catch
            {
                throw;
            }
        }