ClientEncryption.decrypt(encryptedValue)ClientEncryption.decrypt()decrypts theencryptionValueif the current database connection was configured with access to the Key Management Service (KMS) and key vault used to encryptencryptionValue.返回: 解密的值。
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
ClientEncryption.decrypt 通过以下语法实现:
clientEncryption = db.getMongo().getClientEncryption() clientEncryption.decrypt(encryptedValue)
encryptedValue必须是使用客户端字段级加密创建的子类型为 的binary data 6对象。
行为
Read operations issued from a database connection configured with access to the correct Key Management Service (KMS) and Key Vault can automatically decrypt field values encrypted using ClientEncryption.encrypt(). Clients only need to use decrypt() to decrypt Binary subtype 6 values not stored within a document field.
在数据库连接上启用客户端字段级加密
The mongosh ClientEncryption methods require a database connection with in-use encryption enabled. If the current database connection was not initiated with in-use encryption enabled, either:
Use the
mongoshcommand line options to establish a connection with the required options. The command line options only support the Amazon Web Services KMS provider for CMK management.
例子
以下示例使用本地托管的 KMS 进行客户端字段级加密配置。
创建加密连接
启动 mongosh
运行:
mongosh --nodb --nodb表示不连接到数据库。生成密钥string
生成一个基本 64 96 字节的string :
const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64") 创建加密选项对象
要创建客户端字段级加密选项对象,请使用上一步中的
TEST_LOCAL_KEYstring :var autoEncryptionOpts = { "keyVaultNamespace" : "encryption.__dataKeys", "kmsProviders" : { "local" : { "key" : BinData(0, TEST_LOCAL_KEY) } } } 创建加密客户端对象
要创建加密的客户端对象,请使用
Mongo()构造函数。 将mongodb://myMongo.example.netURI 替换为目标集群的连接string URI 。 示例:encryptedClient = Mongo( "mongodb://myMongo.example.net:27017/?replSetName=myMongo", autoEncryptionOpts )
解密数据
Retrieve the ClientEncryption object and use the ClientEncryption.decrypt() method to decrypt a value encrypted by ClientEncryption.encrypt().
clientEncryption = encryptedClient.getClientEncryption(); clientEncryption.decrypt(BinData(6,"AmTi2H3xaEk8u9+jlFNaLLkC3Q/+kmwDbbWrq+h9nuv9W+u7A5a0UnpULBNZH+Q21fAztPpU09wpKPrju9dKfpN1Afpj1/ZhFcH6LYZOWSBBOAuUNjPLxMNSYOOuITuuYWo="))
结果示例
If successful, decrypt() returns the decrypted value:
"123-45-6789"
了解详情
有关在启用客户端字段级加密的情况下启动 MongoDB 连接的完整文档,请参阅Mongo() 。