private void CreateFilePassword() { TechOperations.CheckIfSettingsExists(); string readJson = File.ReadAllText(TechOperations.CurrentWorkingFile); var decrypted = SecureHelper.DecryptString(readJson); var desJson = JsonConvert.DeserializeObject <List <PasswordModel> >(decrypted); if (desJson == null) { desJson = new List <PasswordModel>(); } var pass = new PasswordModel { Username = this.FileName, Password = this.FilePassword }; desJson.Add(pass); var json = JsonConvert.SerializeObject(desJson); using (var writer = new StreamWriter(TechOperations.SettingsFileLocation, true)) { var text = SecureHelper.EncryptString(json); writer.Write(text); } }
private bool AuthorizeFile(string file) { var text = File.ReadAllText(TechOperations.SettingsFileLocation); var decr = SecureHelper.DecryptString(text); var json = JsonConvert.DeserializeObject <List <PasswordModel> >(decr); var fileName = Path.GetFileNameWithoutExtension(file); var item = json.Where(x => x.Username.Equals(fileName)).FirstOrDefault(); if (item.Username.Equals(fileName) && item.Password.Equals(TechOperations.FilePass)) { return(true); } return(false); }
private void SavePassword() { var pass = new PasswordModel { Name = this.Name, Description = this.Description, Username = this.Username, Password = this.Password, Address = this.Address }; string readJson = File.ReadAllText(TechOperations.CurrentWorkingFile);//(@"C:\Development\Playground\secure.kh"); var decrypted = SecureHelper.DecryptString(readJson); var cleaner = File.CreateText(TechOperations.CurrentWorkingFile);//(@"C:\Development\Playground\secure.kh"); cleaner.Flush(); cleaner.Close(); var desJson = JsonConvert.DeserializeObject <List <PasswordModel> >(decrypted); if (desJson == null) { desJson = new List <PasswordModel>(); } desJson.Add(pass); var json = JsonConvert.SerializeObject(desJson); using (var writer = new StreamWriter(TechOperations.CurrentWorkingFile, true))//(@"C:\Development\Playground\secure.kh", true)) { var text = SecureHelper.EncryptString(json); writer.Write(text); } MessageBox.Show("Your password has been created!", "Info", MessageBoxButton.OK, MessageBoxImage.Information); this.Name = string.Empty; this.Description = string.Empty; this.Username = string.Empty; this.Password = string.Empty; this.Address = string.Empty; }
private void OpenFile() { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == true) { try { var win = new FilePasswordWindow(); win.ShowDialog(); var state = this.AuthorizeFile(ofd.FileName); if (state != true) { MessageBox.Show("Wrong password!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } var json = File.ReadAllText(ofd.FileName); this.SelectedFileLocation = ofd.FileName; if (string.IsNullOrEmpty(json)) { MessageBox.Show("Your database is empty!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); return; } json = SecureHelper.DecryptString(json); var passMask = JsonConvert.DeserializeObject <ObservableCollection <PasswordModel> >(json); foreach (var item in passMask) { item.Password = "******"; } this.PassCollection = passMask; } catch (Exception ex) { MessageBox.Show($"Error - {ex.StackTrace}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } }