@@ -40,6 +40,8 @@ documentation.
40
40
41
41
## Examples
42
42
43
+ ### Automatic client-side encryption
44
+
43
45
The following is a sample app that assumes the ** key** and ** schema** have
44
46
already been created in MongoDB. The example uses a local key, however using AWS
45
47
Key Management Service is also an option. The data in the ` encryptedField ` field
@@ -132,11 +134,13 @@ namespace MongoDB.Driver.Examples
132
134
keyVaultNamespace ,
133
135
kmsProviders );
134
136
135
- var clientEncryption = new ClientEncryption (clientEncryptionSettings );
136
- var dataKeyId = clientEncryption .CreateDataKey (" local" , new DataKeyOptions (), CancellationToken .None );
137
- var base64DataKeyId = Convert .ToBase64String (GuidConverter .ToBytes (dataKeyId , GuidRepresentation .Standard ));
138
- clientEncryption .Dispose ();
137
+ Guid dataKeyId ;
138
+ using (var clientEncryption = new ClientEncryption (clientEncryptionSettings ))
139
+ {
140
+ dataKeyId = clientEncryption .CreateDataKey (" local" , new DataKeyOptions (), CancellationToken .None );
141
+ }
139
142
143
+ var base64DataKeyId = Convert .ToBase64String (GuidConverter .ToBytes (dataKeyId , GuidRepresentation .Standard ));
140
144
var collectionNamespace = CollectionNamespace .FromFullName (" test.coll" );
141
145
142
146
var schemaMap = $@" {{
@@ -181,4 +185,149 @@ namespace MongoDB.Driver.Examples
181
185
}
182
186
```
183
187
184
- ** Coming soon:** An example using the community version and demonstrating explicit encryption/decryption.
188
+ ### Explicit Encryption and Decryption
189
+
190
+ Explicit encryption and decryption is a ** MongoDB Community Server** feature and does not use the ` mongocryptd ` process. Explicit encryption is provided by the ` ClientEncryption ` class. The following example has been adapted from [ ` ExplicitEncryptionExamples.cs ` ] ( https://github.com/mongodb/mongo-csharp-driver/blob/master/tests/MongoDB.Driver.Examples/ExplicitEncryptionExamples.cs ) :
191
+
192
+ ``` csharp
193
+ using System ;
194
+ using System .Collections .Generic ;
195
+ using System .Threading ;
196
+ using MongoDB .Driver .Encryption ;
197
+ using MongoDB .Libmongocrypt ;
198
+
199
+ namespace MongoDB .Driver .Examples
200
+ {
201
+ public class ExplicitEncryptionExamples
202
+ {
203
+ private const string LocalMasterKey = " Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk" ;
204
+
205
+ public static void Main (string [] args )
206
+ {
207
+ var localMasterKey = Convert .FromBase64String (LocalMasterKey );
208
+ var kmsProviders = new Dictionary <string , IReadOnlyDictionary <string , object >>();
209
+ var localKey = new Dictionary <string , object >
210
+ {
211
+ { " key" , localMasterKey }
212
+ };
213
+ kmsProviders .Add (" local" , localKey );
214
+
215
+ var keyVaultNamespace = CollectionNamespace .FromFullName (" admin.datakeys" );
216
+ var keyVaultClient = new MongoClient (" mongodb://localhost" );
217
+ var keyVaultDatabase = keyVaultClient .GetDatabase (keyVaultNamespace .DatabaseNamespace .DatabaseName );
218
+ keyVaultDatabase .DropCollection (keyVaultNamespace .CollectionName );
219
+
220
+ // Create the ClientEncryption instance
221
+ var clientEncryptionSettings = new ClientEncryptionOptions (
222
+ keyVaultClient ,
223
+ keyVaultNamespace ,
224
+ kmsProviders );
225
+ using (var clientEncryption = new ClientEncryption (clientEncryptionSettings ))
226
+ {
227
+ var dataKeyId = clientEncryption .CreateDataKey (
228
+ " local" ,
229
+ new DataKeyOptions (),
230
+ CancellationToken .None );
231
+
232
+ var originalString = " 123456789" ;
233
+ Console .WriteLine ($" Original string {originalString }." );
234
+
235
+ // Explicitly encrypt a field
236
+ var encryptOptions = new EncryptOptions (
237
+ EncryptionAlgorithm .AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic .ToString (),
238
+ keyId : dataKeyId );
239
+ var encryptedFieldValue = clientEncryption .Encrypt (
240
+ originalString ,
241
+ encryptOptions ,
242
+ CancellationToken .None );
243
+ Console .WriteLine ($" Encrypted value {encryptedFieldValue }." );
244
+
245
+ // Explicitly decrypt the field
246
+ var decryptedValue = clientEncryption .Decrypt (encryptedFieldValue , CancellationToken .None );
247
+ Console .WriteLine ($" Decrypted value {decryptedValue }." );
248
+ }
249
+ }
250
+ }
251
+ }
252
+ ```
253
+
254
+ ### Explicit Encryption and Auto Decryption
255
+
256
+ Although automatic encryption requires MongoDB 4.2 Enterprise Server or a MongoDB 4.2 Atlas cluster, automatic decryption is supported for all users. To configure automatic decryption without automatic encryption set ` bypassAutoEncryption=true ` . The following example has been adapted from [ ` ExplicitEncryptionExamples.cs ` ] ( https://github.com/mongodb/mongo-csharp-driver/blob/master/tests/MongoDB.Driver.Examples/ExplicitEncryptionExamples.cs ) :
257
+
258
+ ``` csharp
259
+ using System ;
260
+ using System .Collections .Generic ;
261
+ using System .Threading ;
262
+ using MongoDB .Bson ;
263
+ using MongoDB .Driver .Encryption ;
264
+ using MongoDB .Libmongocrypt ;
265
+
266
+ namespace MongoDB .Driver .Examples
267
+ {
268
+ public class ExplicitEncryptionAndAutoDecryptionExamples
269
+ {
270
+ private const string LocalMasterKey = " Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk" ;
271
+
272
+ public static void Main (string [] args )
273
+ {
274
+ var localMasterKey = Convert .FromBase64String (LocalMasterKey );
275
+ var kmsProviders = new Dictionary <string , IReadOnlyDictionary <string , object >>();
276
+ var localKey = new Dictionary <string , object >
277
+ {
278
+ { " key" , localMasterKey }
279
+ };
280
+ kmsProviders .Add (" local" , localKey );
281
+
282
+ var keyVaultNamespace = CollectionNamespace .FromFullName (" admin.datakeys" );
283
+ var collectionNamespace = CollectionNamespace .FromFullName (" test.coll" );
284
+ var autoEncryptionOptions = new AutoEncryptionOptions (
285
+ keyVaultNamespace ,
286
+ kmsProviders ,
287
+ bypassAutoEncryption : true );
288
+ var clientSettings = MongoClientSettings .FromConnectionString (" mongodb://localhost" );
289
+ clientSettings .AutoEncryptionOptions = autoEncryptionOptions ;
290
+ var mongoClient = new MongoClient (clientSettings );
291
+ var database = mongoClient .GetDatabase (collectionNamespace .DatabaseNamespace .DatabaseName );
292
+ database .DropCollection (collectionNamespace .CollectionName );
293
+ var collection = database .GetCollection <BsonDocument >(collectionNamespace .CollectionName );
294
+
295
+ var keyVaultClient = new MongoClient (" mongodb://localhost" );
296
+ var keyVaultDatabase = keyVaultClient .GetDatabase (keyVaultNamespace .DatabaseNamespace .DatabaseName );
297
+ keyVaultDatabase .DropCollection (keyVaultNamespace .CollectionName );
298
+
299
+ // Create the ClientEncryption instance
300
+ var clientEncryptionSettings = new ClientEncryptionOptions (
301
+ keyVaultClient ,
302
+ keyVaultNamespace ,
303
+ kmsProviders );
304
+ using (var clientEncryption = new ClientEncryption (clientEncryptionSettings ))
305
+ {
306
+ var dataKeyId = clientEncryption .CreateDataKey (
307
+ " local" ,
308
+ new DataKeyOptions (),
309
+ CancellationToken .None );
310
+
311
+ var originalString = " 123456789" ;
312
+ Console .WriteLine ($" Original string {originalString }." );
313
+
314
+ // Explicitly encrypt a field
315
+ var encryptOptions = new EncryptOptions (
316
+ EncryptionAlgorithm .AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic .ToString (),
317
+ keyId : dataKeyId );
318
+ var encryptedFieldValue = clientEncryption .Encrypt (
319
+ originalString ,
320
+ encryptOptions ,
321
+ CancellationToken .None );
322
+ Console .WriteLine ($" Encrypted value {encryptedFieldValue }." );
323
+
324
+ collection .InsertOne (new BsonDocument (" encryptedField" , encryptedFieldValue ));
325
+
326
+ // Automatically decrypts the encrypted field.
327
+ var decryptedValue = collection .Find (FilterDefinition <BsonDocument >.Empty ).First ();
328
+ Console .WriteLine ($" Decrypted document {decryptedValue .ToJson ()}." );
329
+ }
330
+ }
331
+ }
332
+ }
333
+ ```
0 commit comments