Esempio n. 1
0
        /// <summary>
        /// Export ListView entries into encrypted XML-Files for saveDialog
        /// </summary>
        /// <param name="listView"></param>
        /// <param name="saveFileDialog"></param>
        public static void ExportEncryptedToXml(ListView listView, SaveFileDialog saveFileDialog)
        {
            string sEncryptionKey = null;

            XmlWriterSettings xmlSettings = new XmlWriterSettings
            {
                Indent = true,
                NewLineOnAttributes = true,
                CloseOutput         = true
            };

            if (File.Exists(DialogManager.GetKeyFilePath()))
            {
                StreamReader reader = new StreamReader(DialogManager.GetKeyFilePath());
                sEncryptionKey = reader.ReadLine();
                reader.Close();
            }
            if (File.Exists(DialogManager.GetKeyFilePathSave()))
            {
                StreamReader reader = new StreamReader(DialogManager.GetKeyFilePathSave());
                sEncryptionKey = reader.ReadLine();
                reader.Close();
            }
            XmlWriter xmlWriter = XmlWriter.Create(saveFileDialog.FileName, xmlSettings);

            xmlWriter.WriteStartElement("pwlist");

            for (int index = 0; index < listView.Items.Count; index++)
            {
                xmlWriter.WriteStartElement("pwentry");

                xmlWriter.WriteStartElement("title");
                xmlWriter.WriteString(Cryptography.AES_Encrypt(listView.Items[index].SubItems[0].Text, sEncryptionKey));
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("username");
                xmlWriter.WriteString(Cryptography.AES_Encrypt(listView.Items[index].SubItems[1].Text, sEncryptionKey));
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("password");
                xmlWriter.WriteString(Cryptography.AES_Encrypt(listView.Items[index].SubItems[2].Text, sEncryptionKey));
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("url");
                xmlWriter.WriteString(Cryptography.AES_Encrypt(listView.Items[index].SubItems[3].Text, sEncryptionKey));
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("notes");
                xmlWriter.WriteString(Cryptography.AES_Encrypt(listView.Items[index].SubItems[4].Text, sEncryptionKey));
                xmlWriter.WriteEndElement();

                xmlWriter.WriteEndElement();
            }

            xmlWriter.WriteEndElement();
            xmlWriter.Close();
        }