/********************************************************************************************************** * UserData LoadUserData(string MasterPassword) * Purpose: Loads data, if it exists, from the disk. The data is encrypted using AES using the * master password's hash as the secret key. **********************************************************************************************************/ private UserData LoadUserData(string MasterPassword) { // Need 128 bits password for the encryption key. ApplicationEntry self = new ApplicationEntry("MasterPass", 128 / sizeof(char) / 8, 0, true, true, true, false); HashedPassword fileName = self.GeneratePassword(MasterPassword); HashedPassword aesKey = self.GeneratePassword(MasterPassword); HashedPassword aesIV = self.GeneratePassword(MasterPassword); System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create(); // Even if aes is broken the master password is unrecoverable. aes.Key = Encoding.Unicode.GetBytes(aesKey.Password); aes.IV = Encoding.Unicode.GetBytes(aesIV.Password); aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ICryptoTransform decryptor = aes.CreateDecryptor(); UserData loadedData = null; // If there is no data don't load it. if (File.Exists(fileName.Password + ".pass") == false) { return(loadedData); } // Open the file using (FileStream outputStream = new FileStream(fileName.Password + ".pass", FileMode.Open)) { // Use a safe to file encryption method using (CryptoStream csDecrypt = new CryptoStream(outputStream, decryptor, CryptoStreamMode.Read)) { // Convert the object to a byte array using (MemoryStream objectStream = new MemoryStream()) { byte[] buffer = new byte[1024]; int bytesRead = csDecrypt.Read(buffer, 0, buffer.Length); while (bytesRead > 0) { objectStream.Write(buffer, 0, bytesRead); bytesRead = csDecrypt.Read(buffer, 0, buffer.Length); } csDecrypt.Flush(); objectStream.Position = 0; IFormatter formatter = new BinaryFormatter(); loadedData = formatter.Deserialize(objectStream) as UserData; } } } return(loadedData); }
/********************************************************************************************************** * void DeleteApplicationEventListener(ApplicationEntry Application) * Purpose: Listens for the event to delete an application. Removes that application from the * display and removes it from the user's data. * * Parameters: * ApplicationEntry Application * The application to remove. **********************************************************************************************************/ private void DeleteApplicationEventListener(ApplicationEntry Application) { foreach (ApplicationEntryDisplay element in uxApplicationEntryContainer.Children) { if (element.Application == Application) { uxApplicationEntryContainer.Children.Remove(element); break; } } Data.Applications.Remove(Application); }
/********************************************************************************************************** * void CreateApplicationEventListener(ApplicationCreationWindow CreatingWindow) * Purpose: Event listener to be fired when an application is to be created. Processes the values * from the creating window and creates a new application from it. * * Parameters: * ApplicationCreationWindow CreatingWindow * The window that issued the creation event. **********************************************************************************************************/ private void CreateApplicationEventListener(ApplicationCreationWindow CreatingWindow) { ApplicationEntry newApplication = new ApplicationEntry(CreatingWindow.ApplicationName, CreatingWindow.PasswordLength, Convert.ToByte(CreatingWindow.Seed % 255), CreatingWindow.LowerCaseAllowed, CreatingWindow.UpperCaseAllowed, CreatingWindow.NumbersAllowed, CreatingWindow.SpecialCharactersAllowed); Data.Applications.Add(newApplication); ApplicationEntryDisplay newApplicationDisplay = new ApplicationEntryDisplay(MasterPassword, newApplication); newApplicationDisplay.DeleteApplicationEvent += DeleteApplicationEventListener; uxApplicationEntryContainer.Children.Add(newApplicationDisplay); }
public ApplicationEntryDisplay(string MasterPassword, ApplicationEntry Application) { InitializeComponent(); this.Application = Application; uxApplicationName.Text = Application.ApplicationName; this.MasterPassword = MasterPassword; foreach (HashedPassword password in Application.Passwords) { HashedPasswordCompactDisplay newDisplay = new HashedPasswordCompactDisplay(password); newDisplay.DeletePasswordEvent += DeletePasswordEventListener; uxHashedPassContainer.Children.Add(newDisplay); } }
/********************************************************************************************************** * void MainWindow_Closing * Purpose: Fires when the window is to be closed. Prompts the user to save changes. * Encrypts the file using AES using a SHA-3 key derived from the MasterPassword. **********************************************************************************************************/ void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { MessageBoxResult save = MessageBox.Show("Save your data?", "Save Prompt", MessageBoxButton.YesNoCancel); if (save == MessageBoxResult.No) { return; } else if (save == MessageBoxResult.Cancel) { e.Cancel = true; return; } // Need 128 bits password for the encryption key. ApplicationEntry self = new ApplicationEntry("MasterPass", 128 / sizeof(char) / 8, 0, true, true, true, false); HashedPassword fileName = self.GeneratePassword(MasterPassword); HashedPassword aesKey = self.GeneratePassword(MasterPassword); HashedPassword aesIV = self.GeneratePassword(MasterPassword); System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create(); // Even if aes is broken the master password is unrecoverable. aes.Key = Encoding.Unicode.GetBytes(aesKey.Password); aes.IV = Encoding.Unicode.GetBytes(aesIV.Password); aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ICryptoTransform encryptor = aes.CreateEncryptor(); // Open the file using (FileStream outputStream = new FileStream(fileName.Password + ".pass", FileMode.OpenOrCreate)) { // Use a safe to file encryption method using (CryptoStream csEncrypt = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write)) { // Convert the object to a byte array using (MemoryStream objectStream = new MemoryStream()) { // Throw the userData into the object stream. IFormatter formatter = new BinaryFormatter(); formatter.Serialize(objectStream, Data); objectStream.Position = 0; byte[] buffer = new byte[1024]; int bytesRead = objectStream.Read(buffer, 0, buffer.Length); // While there are still more bytes to write while (bytesRead > 0) { // Write them to the file. csEncrypt.Write(buffer, 0, bytesRead); bytesRead = objectStream.Read(buffer, 0, buffer.Length); } // Flush the final block. csEncrypt.FlushFinalBlock(); } } } }