// Function to decrypt the main store. public static List <string> DecryptStoreFile(string masterPassword) { // Get required variables. string[] storeFileContents = GetStoreFileContents(); byte[] nonce = GetNonce(); byte[] key = GetKey(masterPassword); List <string> decryptedList = new List <string>(); // Decrypt each password entry. Work backwards with last nonce used, as nonce decrements. for (int i = storeFileContents.Length - 1; i > -1; i--) { byte[] dataToDecrypt = Convert.FromBase64String(storeFileContents[i]); var decrypted = SecretAeadAes.Decrypt(dataToDecrypt, nonce, key); decryptedList.Add(Encoding.ASCII.GetString(decrypted)); // Decrement nonce to get each nonce used to encrypt password entry. ByteOperation.Decrement(ref nonce); } // Return list containing all decrypted password entries. return(decryptedList); }
// Function to encrypt the main store. public static void EncryptStoreFile(string masterPassword, string[] dataToEncrypt) { // Get required variables. byte[] nonce = GetNonce(); byte[] key = GetKey(masterPassword); // Clear main store. File.WriteAllText(PIMUX_STORE, ""); // Encrypt each password entry. for (int i = 0; i < dataToEncrypt.Length; i++) { // Increment nonce so every password entry uses a different nonce. ByteOperation.Increment(ref nonce); byte[] byteDataToEnc = Encoding.ASCII.GetBytes(dataToEncrypt[i]); var encrypted = SecretAeadAes.Encrypt(byteDataToEnc, nonce, key); File.AppendAllText(PIMUX_STORE, Convert.ToBase64String(encrypted) + Environment.NewLine); } // Write nonce to file. File.WriteAllText(PIMUX_STORE_NONCE, Convert.ToBase64String(nonce)); }