对于 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:用于云中 MongoDB 部署的完全托管服务

ClientEncryption.decrypt 通过以下语法实现:

clientEncryption = db.getMongo().getClientEncryption()
clientEncryption.decrypt(encryptedValue)

encryptedValue必须是使用客户端字段级加密创建的子类型为binary data 6对象。

从访问权限正确密钥管理服务configured (KMS ) 和密钥保管库的数据库连接 发出的读取操作可以自动解密使用ClientEncryption.encrypt() 加密的字段值。客户端只需使用decrypt() 来解密未存储在文档字段中的Binary 子类型6 值。

The mongosh client-side field level encryption methods require a database connection with client-side field level encryption enabled. If the current database connection was not initiated with client-side field level 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()

给本页内容打分