public void ExecuteDecrypt(object o) { if (Cipher != null) { if (IsBase64(Cipher)) { try { byte[] cipherBytes = Convert.FromBase64String(Cipher); byte[] masterKey = CryptoUtil.MasterKey; string decryptedPassword = ASCIIEncoding.ASCII.GetString(CryptoUtil.AES_GCMDecrypt(cipherBytes, masterKey)); Password = decryptedPassword; } catch (Exception e) { Cipher = "The cipher was invalid"; Console.WriteLine(e); } } else { Cipher = "Please enter a valid base64 string"; } } }
public VaultViewModel() { List <PasswordModel> dbPasswords = SQLiteDataAccess.LoadPasswords(); byte[] masterKey = CryptoUtil.MasterKey; foreach (PasswordModel pass in dbPasswords) { byte[] passwordBytes = Convert.FromBase64String(pass.Password); pass.Password = Encoding.ASCII.GetString(CryptoUtil.AES_GCMDecrypt(passwordBytes, masterKey)); } Passwords = new BindableCollection <PasswordModel>(dbPasswords); PassScreenType = new DisplayPasswordViewModel(null, this); }
public static bool AuthenticateUser(UserModel userToLogin) { using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString())) { var dbUser = cnn.Query <UserModel>("SELECT * FROM User WHERE Username = @Username", userToLogin).FirstOrDefault(); if (dbUser != null) { byte[] salt = Convert.FromBase64String(dbUser.Salt); byte[] keyEncryptionKey = CryptoUtil.ComputePBKDF2Hash(userToLogin.Password, salt); Console.WriteLine("Attempting authentication with key: " + Convert.ToBase64String(keyEncryptionKey)); // Attempts to decrypt the master key by deriving the key encryption key from the password input try { byte[] decryptedMasterKey = CryptoUtil.AES_GCMDecrypt(Convert.FromBase64String(dbUser.Password), keyEncryptionKey); Console.WriteLine($"Success! - Welcome {dbUser.Username}! =D"); Console.WriteLine($"Encrypted Master Key: {dbUser.Password}"); Console.WriteLine($"Decrypted Master Key: {Convert.ToBase64String(decryptedMasterKey)}"); CryptoUtil.MasterKey = decryptedMasterKey; CurrentUser = dbUser; return(true); } catch (InvalidCipherTextException e) { Console.WriteLine("Incorrect username or password! ;_;"); Console.WriteLine(e); } } return(false); } }