// Function to generate encryption key. public static void GenerateEncryptionKey(string masterPassword) { // Generate random 32 byte encryption key, 12 byte random nonce and 32 byte hash to use as key from master password. byte[] encryptionKey = SodiumCore.GetRandomBytes(32); byte[] nonce = SecretAeadAes.GenerateNonce(); byte[] key = GenericHash.Hash(masterPassword, (byte[])null, 32); // Encrypt encryption key with master password. byte[] encryptedKey = SecretAeadAes.Encrypt(encryptionKey, nonce, key); // Store bytes in base64 encoding. File.WriteAllText(PIMUX_KEY, Convert.ToBase64String(encryptedKey)); File.WriteAllText(PIMUX_KEY_NONCE, Convert.ToBase64String(nonce)); }
public void addKey(string website, string username, string password) { var nonce = SecretAeadAes.GenerateNonce(); var totalString = username + separator + password; var encryptedData = SimpleAESEncryption(secretKey, totalString, nonce); //var bsonCredentials = new BsonDocument { // { "credentials", encryptedData}, // { "nonce", Encoding.UTF8.GetString(nonce) } //}; var doc = new BsonDocument { { "website", website }, { "credentials", encryptedData }, { "nonce", Encoding.UTF8.GetString(nonce) } }; collection.InsertOne(doc); }
// Function to change the master password. public static void ChangeMasterPassword(string oldMasterPassword, string newMasterPassword) { // Get current encryption key. byte[] key = GetKey(oldMasterPassword); // Re-encrypt key with new master password and store. byte[] nonce = SecretAeadAes.GenerateNonce(); byte[] keyToEncryptKey = GenericHash.Hash(newMasterPassword, (byte[])null, 32); byte[] encryptedKey = SecretAeadAes.Encrypt(key, nonce, keyToEncryptKey); // Store bytes in base64 encoding. File.WriteAllText(PIMUX_KEY, Convert.ToBase64String(encryptedKey)); File.WriteAllText(PIMUX_KEY_NONCE, Convert.ToBase64String(nonce)); // Change authentication hash. string newArgonHash = ArgonHash(newMasterPassword); File.WriteAllText(PIMUX_AUTH, newArgonHash); //Hashes the key in to PIMUX_KEY File.WriteAllText(PIMUX_KEY, newArgonHash); }