Docs Menu

Docs HomeRealm

Encrypt a Realm - .NET SDK

On this page

  • Overview
  • Accessing an Encrypted Realm from Multiple Process
  • Example

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.

Realm forbids opening the same encrypted realm from multiple processes. Attempting to do so will throw the error: "Encrypted interprocess sharing is currently unsupported."


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();
// 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);
←  Query MongoDB - .NET SDKAdvanced Guides - .NET SDK →
Give Feedback
© 2022 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.