public void WriteDictionaryToFile(string path) { File.Delete(path); StreamWriter w = File.AppendText(path); w.AutoFlush = true; Dictionary<string, KeyFileObject> dic = this.GetFileInfo(); AESEncrypt AES = new AESEncrypt(); foreach (KeyValuePair<string, KeyFileObject> record in dic) { w.WriteLine(record.Key + "\t" + AES.Encrypt_AES(record.Value)); } w.Close(); }
private KeyFileObject ReadAESKey(string Key, string Value) { AESEncrypt AES = new AESEncrypt(); string[] secure_value = AES.Decrypt_AES(Value).Split('\t'); if (secure_value.Length != 6 || secure_value[5] != "@" + System.Windows.Forms.SystemInformation.ComputerName) { return null; } else { return new KeyFileObject(secure_value[0], secure_value[1], secure_value[2], secure_value[3], secure_value[4]); } }
public void RefreshFile(string path) { //if key file is not sorted, then rewrite the key file if (!isRefresh) { List<KeyFileObject> list = new List<KeyFileObject>(); CredentialDic = new Dictionary<string, KeyFileObject>(); if (!File.Exists(path)) return; string strLine = ""; FileInfo f = new FileInfo(path); StreamReader srFile = f.OpenText(); while ((strLine = srFile.ReadLine()) != null) { if (strLine.Trim().Length > 0) { if (strLine != EOF_Mark) { string[] strWord = strLine.Split('\t'); KeyFileObject key = ReadAESKey(strWord[0], strWord[1]); if (key == null) { srFile.Close(); File.Delete(path); return; } list.Add(key); } else { list.Add(new KeyFileObject(EOF_Mark, "", "", "", "")); } } } srFile.Close(); if (list.Count != 0) { KeyFileObject EOF_Mark_Object = new KeyFileObject(EOF_Mark, "", "", "", ""); if (list[list.Count - 1] == EOF_Mark_Object) { list.Remove(EOF_Mark_Object); list.Sort(); foreach (KeyFileObject record in list) { CredentialDic.Add(record.Path, record); } } else { list.Remove(EOF_Mark_Object); list.Sort(); File.Delete(path); StreamWriter w = File.AppendText(path); AESEncrypt AES = new AESEncrypt(); foreach (KeyFileObject record in list) { CredentialDic.Add(record.Path, record); w.WriteLine(record.Path + "\t" + AES.Encrypt_AES(record)); } w.WriteLine(EOF_Mark); w.Flush(); w.Close(); } } } isRefresh = true; }