public static IVaultContent Deserialize(string filePath, SecureString password) { var allText = File.ReadAllText(filePath); int index = allText.IndexOf(FieldSeperator); if (index < 3 || index + 3 > allText.Length) { throw new FormatException("Invalid file format."); } Version fileVersion; if (!Version.TryParse(allText.Substring(0, index), out fileVersion)) { throw new FormatException("Invalid file format."); } if (CurrentVersion.Major != fileVersion.Major || CurrentVersion.Minor != fileVersion.Minor) { throw new FormatException("Unknown file version."); } var bytes = Convert.FromBase64String(allText.Substring(index + 1)); IVaultContent vaultContent = new VaultContent(); vaultContent.Text = VaultSecurity.Decrypt(bytes, password); return vaultContent; }
private bool SaveCurrent(bool forcePrompts = false) { var effectiveFilePath = lastFilePath; var effectivePassword = lastPassword; if (forcePrompts || effectiveFilePath == null) { if (saveFileDialog.ShowDialog(this) != true) { return false; } effectiveFilePath = saveFileDialog.FileName; } if (forcePrompts || effectivePassword == null) { var passwordDialog = new PasswordDialog(); if (passwordDialog.ShowDialog() != true) { return false; } effectivePassword = passwordDialog.Password; } IVaultContent content = new VaultContent() { Text = textBox.Text }; VaultSerializer.Serialize(effectiveFilePath, content, effectivePassword); lastFilePath = effectiveFilePath; lastPassword = effectivePassword; dataIsChanged = false; UpdateActions(); return true; }