Explicit Encryption显式加密
On this page本页内容
Overview概述Use Explicit Encryption使用显式加密Create a ClientEncryption Instance创建ClientEncryption实例Encrypt Fields in Read and Write Operations读取和写入操作中的加密字段Manual Decryption手动解密Automatic Decryption自动解密Example实例Create a MongoClient Instance创建MongoClient实例Create a ClientEncryption Instance创建ClientEncryption实例Encrypt Fields and Insert加密字段并插入Retrieve Document and Decrypt Fields检索文档和解密字段Server-Side Field Level Encryption Enforcement服务器端字段级加密强制Learn More了解更多信息
Overview概述
Learn how to use the explicit encryption mechanism of Client-Side Field Level Encryption (CSFLE).了解如何使用客户端字段级加密(CSFLE)的显式加密机制。
Explicit encryption is a mechanism in which you specify how to encrypt and decrypt fields in your document for each operation you perform on your database.显式加密是一种机制,在该机制中,您可以为对数据库执行的每个操作指定如何加密和解密文档中的字段。
Explicit encryption is available in the following MongoDB products of version 4.2 or later:显式加密在以下4.2或更高版本的MongoDB产品中可用:
- MongoDB Community Server
- MongoDB Enterprise Advanced
- MongoDB Atlas
Use Explicit Encryption使用显式加密
To use explicit encryption you must perform the following actions in your CSFLE-enabled application:要使用显式加密,您必须在启用CSFLE的应用程序中执行以下操作:
Create a ClientEncryption Instance创建ClientEncryption
实例Encrypt Fields in Read and Write Operations读取和写入操作中的加密字段Manually or Automatically Decrypt Fields in Your Documents手动或自动解密文档中的字段
Create a ClientEncryption Instance创建ClientEncryption
实例
To use explicit encryption, you must create a 若要使用显式加密,必须创建ClientEncryption
instance. ClientEncryption
实例。ClientEncryption
is an abstraction used across drivers and 是一个跨驱动程序和mongosh
that encapsulates the Key Vault collection and KMS operations involved in explicit encryption.mongosh
使用的抽象,它封装了显式加密中涉及的键库集合和KMS操作。
To create a 要创建ClientEncryption
instance, you must specify the following information:ClientEncryption
实例,必须指定以下信息:
A可以访问键库集合的MongoClient
instance with access to your Key Vault collectionMongoClient
实例The namespace of your Key Vault collection键库集合的命名空间AkmsProviders
object configured with access to the KMS hosting your Customer Master KeykmsProviders
对象配置为可以访问托管客户主键的KMS
For more 有关更多ClientEncryption
options, see CSFLE-Specific MongoClient Options.ClientEncryption
选项,请参阅CSFLE特定的MongoClient
选项。
To view code snippets that show how to create a 要查看显示如何创建ClientEncryption
instance, see the Example section of this guide.ClientEncryption
实例的代码片段,请参阅本指南的示例部分。
Encrypt Fields in Read and Write Operations读取和写入操作中的加密字段
You must update read and write operations throughout your application such that your application encrypts fields before performing read and write operations.您必须在整个应用程序中更新读写操作,以便应用程序在执行读写操作之前对字段进行加密。
To encrypt fields, use the 要加密字段,请使用encrypt
method of your ClientEncryption
instance.ClientEncryption
实例的加密方法。
To view code snippets that show how to use the 要查看显示如何使用encrypt
method, see the Example section of this guide.encrypt
方法的代码片段,请参阅本指南的示例”部分。
Manual Decryption手动解密
You can decrypt your encrypted fields manually or automatically when using explicit encryption.使用显式加密时,可以手动或自动解密加密字段。
To decrypt your fields manually, use the 要手动解密字段,请使用decrypt
method of your ClientEncryption
instance.ClientEncryption
实例的decrypt
(解密)方法。
To view code snippets that show how to use the 要查看显示如何使用decrypt
method, see the Example section of this guide.decrypt
方法的代码片段,请参阅本指南的示例部分。
Automatic Decryption自动解密
To decrypt your fields automatically, configure your 要自动解密字段,请按如下方式配置MongoClient
instance as follows:MongoClient
实例:
Specify your Key Vault collection指定键保管库集合Specify a指定kmsProviders
objectkmsProviders
对象If you use MongoDB Community Server, set the如果使用MongoDB Community Server,请将bypassAutoEncryption
option toTrue
bypassAutoEncryption
选项设置为True
Automatic Decryption is Available in MongoDB Community ServerMongoDB社区服务器提供自动解密功能
Although automatic encryption requires MongoDB Enterprise or MongoDB Atlas, automatic decryption is available in the following MongoDB products of version 4.2 or later:尽管自动加密需要MongoDB Enterprise或MongoDB Atlas,但以下4.2或更高版本的MongoDB产品中提供了自动解密功能:
MongoDB Community ServerMongoDB社区服务器- MongoDB Enterprise Advanced
- MongoDB Atlas
To view a code snippet demonstrating how to enable automatic decryption, select the tab corresponding to your preferred language:要查看演示如何启用自动解密的代码片段,请选择与首选语言对应的选项卡:
const client = new MongoClient(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
monitorCommands: true,
autoEncryption: {
keyVaultNamespace,
kmsProviders,
bypassAutoEncryption: true,
},
});
Example实例
Assume you want to insert documents with the following structure into your MongoDB instance:假设您想将具有以下结构的文档插入到MongoDB实例中:
{
"name": "<name of person>",
"age": <age of person>,
"favorite-foods": ["<array of foods>"]
}
Create a MongoClient Instance创建MongoClient实例
In this example, you use the same 在本例中,您使用相同的MongoClient
instance to access your Key Vault collection and to read and write encrypted data.MongoClient
实例来访问您的键库集合,并读取和写入加密数据。
The following code snippets show how to create a 以下代码片段显示了如何创建MongoClient
instance:MongoClient
实例:
const client = new MongoClient(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
Encrypt Fields and Insert加密字段并插入
You want to encrypt the fields of your document using the following algorithms:您希望使用以下算法加密文档的字段:
name | Deterministic | String |
age | No encryption | Int |
favorite-foods | Random | Array |
The following code snippets show how to manually encrypt the fields in your document and insert your document into MongoDB:以下代码片段显示了如何手动加密文档中的字段并将文档插入MongoDB:
The 以下示例中的dataKeyId变量指的是数据加密键(DEK)。要了解如何使用本地键提供程序生成DEK,请参阅快速入门。dataKeyId
variable in the following examples refers to a Data Encryption Key (DEK). To learn how to generate a DEK with your Local Key Provider, see the Quick Start. To learn how to create a DEK with a specific Key Management System, see Tutorials.要了解如何使用特定的键管理系统创建DEK,请参阅教程。
encryptedName = await encryption.encrypt("Greg", {
algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
keyId: dataKeyId,
});
encryptedFoods = await encryption.encrypt(["Cheese", "Grapes"], {
algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
keyId: dataKeyId,
});
await collection.insertOne({
name: encryptedName,
age: 83,
foods: encryptedFoods,
});
Retrieve Document and Decrypt Fields检索文档和解密字段
The following code snippets show how to retrieve your inserted document and manually decrypt the encrypted fields:以下代码段显示了如何检索插入的文档并手动解密加密字段:
queryEncryptedName = await encryption.encrypt("Greg", {
algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
keyId: dataKeyId,
});
let doc = await collection.findOne({ name: queryEncryptedName });
console.log("Encrypted Document: ", doc);
doc.name = encryption.decrypt(doc.name);
doc.foods = encryption.decrypt(doc.foods);
console.log("Decrypted document: ", doc);
Server-Side Field Level Encryption Enforcement服务器端字段级加密强制
MongoDB supports using schema validation to enforce encryption of specific fields in a collection.MongoDB支持使用模式验证来强制加密集合中的特定字段。
A client performing Client-Side Field Level Encryption with the explicit encryption mechanism on a MongoDB instance configured to enforce encryption of certain fields must encrypt those fields as specified on the MongoDB instance.在配置为强制加密某些字段的MongoDB实例上使用显式加密机制执行客户端字段级加密的客户端必须按照MongoDB实例中的指定加密这些字段。
To learn how to set up server-side CSFLE enforcement, see CSFLE Server-Side Schema Enforcement.要了解如何设置服务器端CSFLE强制,请参阅CSFLE服务器端模式强制。
Learn More了解更多信息
To learn more about Key Vault collections, Data Encryption Keys, and Customer Master Keys, see Keys and Key Vaults.要了解有关键库集合、数据加密键和客户主键的详细信息,请参阅键和键库。
To learn more about KMS providers and 要了解有关KMS提供程序和kmsProviders
objects, see CSFLE KMS Providers.kmsProviders
对象的更多信息,请参阅CSFLE KMS提供器。