public Boolean save(String file_path, String Password) { MemoryStream ms = new MemoryStream(); XmlTextWriter textWriter = new XmlTextWriter(ms, new UTF8Encoding(false)); textWriter.Namespaces = false; textWriter.Indentation = 2; textWriter.IndentChar = '\t'; textWriter.Formatting = Formatting.Indented; textWriter.WriteStartDocument(); textWriter.WriteComment("Configuration File"); textWriter.WriteComment("Original Filename: " + System.IO.Path.GetFileName(file_path)); textWriter.WriteStartElement("root"); foreach (DictionaryEntry de in ConfigContainer) { textWriter.WriteStartElement("item"); textWriter.WriteAttributeString("name", (String)de.Key); textWriter.WriteAttributeString("value", (String)de.Value); textWriter.WriteEndElement(); } textWriter.WriteEndElement(); textWriter.WriteEndDocument(); textWriter.Close(); byte[] b = ms.GetBuffer(); string ste = Crypt.Encrypt(Serialize.UTF8ByteArrayToString(b), Password); FileStream fs = new FileStream(file_path, FileMode.OpenOrCreate, FileAccess.Write); byte[] b2 = Serialize.StringToUTF8ByteArray(ste); fs.Write(b2, 0, b2.Length); fs.Close(); return(true); }
public Boolean load(String file_path, String Password) { //System.IO.FileInfo fileInfo = new System.IO.FileInfo(file_path); //if (fileInfo.Length > 0) //{ Password = Encryption.md5(Password);//reduces key length by half (not sure), BUT serves to format the input into a 32 character string (temporary solution) FileStream fs = new FileStream(file_path, FileMode.OpenOrCreate, FileAccess.Read); if (fs.Length < 1) { fs.Close(); return(false); } byte[] b = new byte[fs.Length]; fs.Read(b, 0, (int)fs.Length); fs.Close(); string s = Serialize.UTF8ByteArrayToString(b); s = Crypt.Decrypt(s, Password); if (s == "") { return(false); } StringReader sr = new StringReader(s); XmlDocument.Load(sr); foreach (XmlNode n in XmlDocument.ChildNodes) { if (n.NodeType == XmlNodeType.Element && n.Name == "root") { foreach (XmlNode node in n.ChildNodes) { String setting_name = node.Attributes["name"].Value.ToString(); String setting_value = node.Attributes["value"].Value.ToString(); ConfigContainer[setting_name] = setting_value; } } } //} return(true); }