Encrypt a Realm - .NET SDK
Overview
You can encrypt your local realm to ensure data security. For more information, see Encryption - .NET SDK.
The same encryption key must be supplied every time you obtain a Realm instance.
If you don't provide a key, or specify the wrong key, for an encrypted
Realm, you will get a
RealmFileAccessErrorException
when you call GetInstance
.
Accessing an Encrypted Realm from Multiple Process
Realm forbids opening the same encrypted realm from multiple processes. Attempting to do so will throw the error: "Encrypted interprocess sharing is currently unsupported."
Example
Do not use cryptographically-weak hashes for realm encryption keys. For optimal security, we recommend generating random rather than derived encryption keys.
The following code demonstrates how to generate an encryption key and open an encrypted realm:
// Check if we already have a key stored in the platform's secure storage. // If we don't, generate a new one: var encryptionKey = new byte[64]; using var rng = new RNGCryptoServiceProvider(); rng.GetBytes(encryptionKey); // Store the key securely to be used next time we want to open the Realm. // Create configuration. var config = new RealmConfiguration { EncryptionKey = encryptionKey }; // Open or create a realm with the encryption key. var realm = Realm.GetInstance(config);