/// <summary> /// Set encrypted password (used if the master password is changed) /// </summary> /// <param name="encryptedPassword"></param> public void SetEncryptedPassword(string encryptedPassword) { _encryptedPassword = encryptedPassword; if (!_encrypted) { _passwordValue = AESEncryption.DecryptWithPassword(_encryptedPassword, _masterPassword); } }
/// <summary> /// Save data to encrypted binary file. /// </summary> /// <param name="path"></param> /// <param name="data"></param> /// <param name="encryptionPassword"></param> public void SaveEncrypted(string path, List <CredentialEntry> data, SecureString encryptionPassword) { SaveData saveData = new SaveData { CredentialEntries = new List <SerializeableCredentialEntry>() }; // prepare data for serialization foreach (CredentialEntry entry in data) { saveData.CredentialEntries.Add(entry.ToSerializeable()); // convert entries } BinaryFormatter serializer = new BinaryFormatter(); if (File.Exists(path)) { File.Copy(path, path + ".backup", true); File.Delete(path); // ATTENTION: The file has to be removed because if there are less entries present in the new version the garbage data at the file end will corrupt the save!!! } try { // Create memory stream MemoryStream memoryStream = new MemoryStream(); // Create a new StreamWriter FileStream writer = File.OpenWrite(path); // Serialize the file serializer.Serialize(memoryStream, saveData); memoryStream.Seek(0, SeekOrigin.Begin); byte[] bytes = new byte[memoryStream.Length]; memoryStream.Read(bytes, 0, (int)memoryStream.Length); byte[] encryptedBytes = AESEncryption.EncryptWithPassword(bytes, encryptionPassword); writer.Write(encryptedBytes, 0, encryptedBytes.Length); // Close the writer writer.Close(); } catch { if (File.Exists(path)) { File.Delete(path); // rollback if something failed } if (File.Exists(path + ".backup")) { File.Move(path + ".backup", path); } throw new Exception("Unable to save File."); } finally { if (File.Exists(path + ".backup")) { File.Delete(path + ".backup"); // remove backup file } } }
/// <summary> /// Load binary encrypted data from a given file. /// </summary> /// <param name="path"></param> /// <param name="decryptionPassword"></param> /// <returns></returns> public List <CredentialEntry> OpenEncrypted(string path, SecureString decryptionPassword) { // create binary formatter BinaryFormatter formatter = new BinaryFormatter(); // Deserialize the file SaveData deserialized; byte[] encryptedBytes = File.ReadAllBytes(path); byte[] decryptionBytes = AESEncryption.DecryptWithPassword(encryptedBytes, decryptionPassword); if (decryptionBytes == null) { throw new Exception("Wrong Password."); } try { MemoryStream memoryStream = new MemoryStream(decryptionBytes, 0, decryptionBytes.Length); deserialized = (SaveData)formatter.Deserialize(memoryStream); } catch { throw new Exception("Unable to open file."); } List <CredentialEntry> collection = new List <CredentialEntry>(); // fill in loaded data foreach (SerializeableCredentialEntry deserializedEntry in deserialized.CredentialEntries) { CredentialEntry entry = new CredentialEntry(decryptionPassword, deserializedEntry.Password); entry.Title = deserializedEntry.Title; entry.Id = deserializedEntry.Id; entry.UserName = deserializedEntry.UserName; entry.ProvidedName = deserializedEntry.ProvidedName; entry.AddressData = deserializedEntry.AddressData; entry.Url = deserializedEntry.Url; entry.Email = deserializedEntry.Email; collection.Add(entry); } // Return the object return(collection); }