/// <summary> /// Opens a puppet file. /// </summary> private void OpenFile() { if (normalOpenFileDialog.ShowDialog() == DialogResult.OK) { var passwordDialog = new PasswordDialog(); var dialogResult = passwordDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { if (this.ShowSavePrompt() != DialogResult.Cancel) { this.ResetNations(); FileStream fileStream = null; byte[] iv = null; byte[] encryptedBytes = null; try { fileStream = new FileStream(normalOpenFileDialog.FileName, FileMode.Open); iv = new byte[16]; encryptedBytes = new byte[fileStream.Length - 16]; fileStream.Read(iv, 0, 16); fileStream.Read(encryptedBytes, 0, encryptedBytes.Length); } catch (Exception) { MessageBox.Show("An error occurred while opening or parsing data from the file. Do you have sufficient permissions to access the file?", "NationStates Nation Manager"); if (fileStream != null) { fileStream.Close(); } return; } MemoryStream decryptedStream = null; CryptoStream cryptoStream = null; try { var key = this.GenerateKeyFromPassword(passwordDialog.Password); var decryptor = new AesCryptoServiceProvider().CreateDecryptor(key, iv); decryptedStream = new MemoryStream(); cryptoStream = new CryptoStream(decryptedStream, decryptor, CryptoStreamMode.Write); cryptoStream.Write(encryptedBytes, 0, encryptedBytes.Length); cryptoStream.FlushFinalBlock(); decryptedStream.Seek(0, SeekOrigin.Begin); var formatter = new BinaryFormatter(); var nations = (List<Nation>)formatter.Deserialize(decryptedStream); foreach (Nation nation in nations) { this.AddNation(nation); } } catch (Exception) { MessageBox.Show("An error occurred while decrypting the file. Did you enter the correct password?", "NationStates Nation Manager"); if (decryptedStream != null) { decryptedStream.Close(); } if (cryptoStream != null) { cryptoStream.Close(); } return; } if (this.OpenFileStream != null) { this.OpenFileStream.Close(); } this.OpenFileStream = fileStream; this.OpenFileName = new FileInfo(normalOpenFileDialog.FileName).Name; this.OpenFilePassword = passwordDialog.Password; this.Text = "NationStates Nation Manager - " + this.OpenFileName; } } } }
/// <summary> /// Saves the puppet list to an new file. /// </summary> private void SaveFileNew() { if (normalSaveFileDialog.ShowDialog() == DialogResult.OK) { var passwordDialog = new PasswordDialog(); var dialogResult = passwordDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { byte[] iv = null; byte[] encryptedBytes = null; MemoryStream encryptedStream = null; CryptoStream cryptoStream = null; try { var serializationStream = new MemoryStream(); var formatter = new BinaryFormatter(); formatter.Serialize(serializationStream, this.Nations); var serializedBytes = serializationStream.ToArray(); iv = this.GenerateInitializationVector(); var key = this.GenerateKeyFromPassword(passwordDialog.Password); var encryptor = new AesCryptoServiceProvider().CreateEncryptor(key, iv); encryptedStream = new MemoryStream(); cryptoStream = new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write); cryptoStream.Write(serializedBytes, 0, serializedBytes.Length); cryptoStream.FlushFinalBlock(); encryptedStream.Seek(0, SeekOrigin.Begin); encryptedBytes = new byte[encryptedStream.Length]; encryptedStream.Read(encryptedBytes, 0, encryptedBytes.Length); } catch (Exception) { MessageBox.Show("An error occurred while encrypting the data to write to the file.", "NationStates Nation Manager"); if (encryptedStream != null) { encryptedStream.Close(); } if (cryptoStream != null) { cryptoStream.Close(); } return; } FileStream fileStream = null; try { fileStream = new FileStream(normalSaveFileDialog.FileName, FileMode.Create); fileStream.Write(iv, 0, iv.Length); fileStream.Write(encryptedBytes, 0, encryptedBytes.Length); fileStream.Flush(); } catch (Exception) { MessageBox.Show("An error occurred while creating or saving data to the file. Do you have sufficient permissions to access the file?", "NationStates Nation Manager"); if (fileStream != null) { fileStream.Close(); } return; } if (this.OpenFileStream != null) { this.OpenFileStream.Close(); } this.OpenFileStream = fileStream; this.OpenFileName = new FileInfo(normalSaveFileDialog.FileName).Name; this.OpenFilePassword = passwordDialog.Password; this.Text = "NationStates Nation Manager - " + this.OpenFileName; } } }