Navigation

Encrypt a Realm - .NET SDK

On this page

  • Overview
  • Example

You can encrypt your local realm to ensure data security. For more information, see Encryption - .NET SDK.

Note

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.

Warning

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);
Give Feedback

On this page

  • Overview
  • Example