private static void UsePasswordGroup(PasswordDb db, PasswordGroup passwordGroup) { while (true) { Console.WriteLine(); Console.WriteLine($"Group: {passwordGroup.GroupName}"); Console.WriteLine("0: Back to main menu"); var menuItem = 1; var entries = passwordGroup.PasswordEntries.OrderBy(pe => pe.Name).ToList(); foreach (var entry in entries) { var entryValue = entry.IsValueEncrypted ? "****" : entry.Value; Console.WriteLine($"{menuItem}: {entry.Name}\t{entryValue}"); menuItem++; } Console.WriteLine(); Console.Write("Option: "); if (!int.TryParse(Console.ReadLine().Trim(), out var choice) || choice < 0 || choice >= menuItem) { continue; } if (choice == 0) { return; } DecryptPassword(db, passwordGroup, entries[choice - 1]); } }
private static void UsePasswordDb(PasswordDb db) { while (true) { Console.WriteLine(); Console.WriteLine("0: Exit"); var menuItem = 1; var passwordGroups = db.PasswordGroups.OrderBy(pg => pg.GroupName).ToList(); foreach (var passwordGroup in passwordGroups) { Console.WriteLine($"{menuItem}: {passwordGroup.GroupName}"); menuItem++; } Console.WriteLine(); Console.Write("Option: "); if (!int.TryParse(Console.ReadLine().Trim(), out var choice) || choice < 0 || choice >= menuItem) { continue; } if (choice == 0) { return; } UsePasswordGroup(db, passwordGroups[choice - 1]); } }
private static void DecryptPassword(PasswordDb db, PasswordGroup passwordGroup, PasswordEntry passwordEntry) { while (true) { if (!passwordEntry.IsValueEncrypted || masterPassword != null) { break; } Console.WriteLine(); Console.Write("Enter master password: "******"Invalid master password entered, please try again."); continue; } masterPassword = enteredMasterPassword; break; } } Console.WriteLine(); Console.WriteLine($"Group: {passwordGroup.GroupName}"); Console.WriteLine($"Name: {passwordEntry.Name}"); var passwordValue = passwordEntry.Value; if (passwordEntry.IsValueEncrypted) { using (var secure = new Secure()) { passwordValue = secure.Decrypt(masterPassword, db.IV, passwordEntry.Salt, passwordValue); } } Console.WriteLine($"Value: {passwordValue}"); Console.WriteLine(); Console.WriteLine("Press return to continue..."); Console.ReadLine(); }
public static int StorePassword(PasswordData passwordData) { var aesLogic = new AesLogic(); var user = Context.Users.First(x => x.Login == UserName); var encryptPassword = aesLogic.EncryptPassword(passwordData.Password, Password); //Get encrypted password var passwordDb = new PasswordDb { Login = passwordData.Login, PasswordHash = encryptPassword, WebAddress = passwordData.WebAddress, Description = passwordData.Description, User = user, IdUser = user.Id }; Context.Passwords.Add(passwordDb); Context.SaveChanges(); return(passwordDb.Id); }
public static void SetPasswordDb(this UserAccount userAccount, PasswordDb passwordDb) { userAccount.PasswordDatabase = JsonSerializer.Serialize(passwordDb); }
public void TestSetAndGet() { var group1 = new PasswordGroup { GroupName = "group 1", PasswordEntries = { new PasswordEntry { Name = "entry 1.1", IsValueEncrypted = false, Value = "plain text 1.1" }, new PasswordEntry { Name = "entry 1.2", IsValueEncrypted = true, Value = "encrypted value 1.2", Salt = new byte[]{ 1, 2 } } } }; var group2 = new PasswordGroup { GroupName = "group 2", PasswordEntries = { new PasswordEntry { Name = "entry 2.1", IsValueEncrypted = true, Value = "encrypted value 2.1", Salt = new byte[]{ 2, 1 } } } }; var db = new PasswordDb { MasterPassword = "******", IV = new byte[] { 1, 2, 3, 4 }, PasswordGroups = { group1, group2 } }; var account = new UserAccount(); Assert.Null(account.PasswordDatabase); PasswordDbExtensions.SetPasswordDb(account, db); Assert.NotNull(account.PasswordDatabase); Assert.NotEqual("", account.PasswordDatabase); var deserialized = PasswordDbExtensions.GetPasswordDb(account); Assert.NotNull(deserialized); Assert.Equal("hashed password", deserialized.MasterPassword); Assert.Equal(new byte[] { 1, 2, 3, 4 }, deserialized.IV); Assert.Equal(2, deserialized.PasswordGroups.Count); Assert.Equal("group 1", deserialized.PasswordGroups.ElementAt(0).GroupName); Assert.Equal(2, deserialized.PasswordGroups.ElementAt(0).PasswordEntries.Count); Assert.Equal("entry 1.1", deserialized.PasswordGroups.ElementAt(0).PasswordEntries.ElementAt(0).Name); Assert.False(deserialized.PasswordGroups.ElementAt(0).PasswordEntries.ElementAt(0).IsValueEncrypted); Assert.Equal("plain text 1.1", deserialized.PasswordGroups.ElementAt(0).PasswordEntries.ElementAt(0).Value); Assert.Null(deserialized.PasswordGroups.ElementAt(0).PasswordEntries.ElementAt(0).Salt); Assert.Equal("entry 1.2", deserialized.PasswordGroups.ElementAt(0).PasswordEntries.ElementAt(1).Name); Assert.True(deserialized.PasswordGroups.ElementAt(0).PasswordEntries.ElementAt(1).IsValueEncrypted); Assert.Equal("encrypted value 1.2", deserialized.PasswordGroups.ElementAt(0).PasswordEntries.ElementAt(1).Value); Assert.Equal(new byte[] { 1, 2 }, deserialized.PasswordGroups.ElementAt(0).PasswordEntries.ElementAt(1).Salt); Assert.Equal("group 2", deserialized.PasswordGroups.ElementAt(1).GroupName); Assert.Single(deserialized.PasswordGroups.ElementAt(1).PasswordEntries); Assert.Equal("entry 2.1", deserialized.PasswordGroups.ElementAt(1).PasswordEntries.ElementAt(0).Name); Assert.True(deserialized.PasswordGroups.ElementAt(1).PasswordEntries.ElementAt(0).IsValueEncrypted); Assert.Equal("encrypted value 2.1", deserialized.PasswordGroups.ElementAt(1).PasswordEntries.ElementAt(0).Value); Assert.Equal(new byte[] { 2, 1 }, deserialized.PasswordGroups.ElementAt(1).PasswordEntries.ElementAt(0).Salt); }
public PasswordDbViewModel(PasswordDb db, string dropboxToken) { Groups = db.PasswordGroups.Select(pg => new GroupViewModel(pg)).OrderBy(pg => pg.Name); HasDropboxToken = !string.IsNullOrEmpty(dropboxToken); }