对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

ClientEncryption.decrypt()(mongosh方法)

ClientEncryption.decrypt(encryptedValue)

ClientEncryption.decrypt() decrypts the encryptionValue if the current database connection was configured with access to the Key Management Service (KMS) and key vault used to encrypt encryptionValue.

返回:

解密的值。

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

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:

以下示例使用本地托管的 KMS 进行客户端字段级加密配置。

1
  1. 启动 mongosh

    运行:

    mongosh --nodb

    --nodb 表示不连接到数据库。

  2. 生成密钥string

    生成一个基本 64 96 字节的string :

    const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64")
  3. 创建加密选项对象

    要创建客户端字段级加密选项对象,请使用上一步中的 TEST_LOCAL_KEY string :

    var autoEncryptionOpts = {
    "keyVaultNamespace" : "encryption.__dataKeys",
    "kmsProviders" : {
    "local" : {
    "key" : BinData(0, TEST_LOCAL_KEY)
    }
    }
    }
  4. 创建加密客户端对象

    要创建加密的客户端对象,请使用Mongo()构造函数。 将 mongodb://myMongo.example.net URI 替换为目标集群的连接string URI 。 示例:

    encryptedClient = Mongo(
    "mongodb://myMongo.example.net:27017/?replSetName=myMongo",
    autoEncryptionOpts
    )
2

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() 。

给本页内容打分