【问题标题】:Why does decryption function of Java SDK of AWS KMS does not require an encryption context?为什么 AWS KMS 的 Java SDK 的解密功能不需要加密上下文?
【发布时间】:2023-04-07 15:05:01
【问题描述】:

From the description of AWS

在加密请求中提供加密上下文时,它会以密码方式绑定到密文,因此需要相同的加密上下文来解密(或解密和重新加密)数据。如果解密请求中提供的加密上下文不是精确的、区分大小写的匹配,则解密请求将失败。只有加密上下文对的顺序可以变化。

但是,in the example code of JAVA SDK,解密时没有指定加密上下文。

crypto.decryptString(prov, ciphertext);

这两个帖子对我来说听起来有点矛盾,因为我认为解密用户需要自己提供加密上下文。我在sdk中查看了AwsCrypto.decryptString(final CryptoMaterialsManager provider, final String ciphertext))的源代码,似乎加密上下文也包含在密文中。

我可以知道为什么要这样设置吗?

【问题讨论】:

  • 该示例使用的是“AWS Encryption SDK”,而不是“Java SDK”。 Java SDK 确实将加密上下文作为解密请求的一部分,如您所见 here
  • 感谢您指出。我被这两个弄糊涂了。似乎从加密 sdk 返回的加密结果与使用 EncryptRequest 对象的 Java SDK 加密结果不同。我可以知道你是否知道为什么会有这样的差异?

标签: java amazon-web-services encryption aws-kms


【解决方案1】:

经过一番研究,我发现至少有两种方法可以进行加密和解密。如果有人感兴趣,请将它们发布在这里。它是用 kotlin 编写的。

class AwsEncryptionSDKWrapper(private val keyIdArn: String, region: String) {
    private val crypto = AwsCrypto()
    private val prov: KmsMasterKeyProvider = KmsMasterKeyProvider.builder().withDefaultRegion(region).withKeysForEncryption(keyIdArn).build()

    fun encrypt(raw: String, encryptionContext: Map<String, String>): String {
        return crypto.encryptString(prov, raw, encryptionContext).result
    }

    fun decrypt(cipherText: String, encryptionContext: Map<String, String>): String {
        val decryptedResponse = crypto.decryptString(prov, cipherText)
        if (decryptedResponse.masterKeyIds[0] != keyIdArn) {
            throw IllegalStateException("Wrong key ID!")
        }

        encryptionContext.entries.forEach { (key, value) ->
            if (value != decryptedResponse.encryptionContext[key]) {
                throw IllegalStateException("Wrong Encryption Context!")
            }
        }
        return decryptedResponse.result
    }
}

class AwsKMSSDKWrapper(region: String) {
    private val client = AWSKMSClientBuilder.standard().withRegion(region).build()

    fun encrypt(keyIdArn: String, raw: String, encryptionContext: Map<String, String>): String {
        val plaintextBytes = raw.toByteArray(StandardCharsets.UTF_8)

        val encReq = EncryptRequest()
        encReq.keyId = keyIdArn
        encReq.plaintext = ByteBuffer.wrap(plaintextBytes)
        encReq.withEncryptionContext(encryptionContext)
        val cipherText = client.encrypt(encReq).ciphertextBlob

        return Base64.getEncoder().encodeToString(cipherText.array())
    }

    fun decrypt(base64CipherText: String, encryptionContext: Map<String, String>, keyIdArn: String): String {
        val req = DecryptRequest()
            .withCiphertextBlob(ByteBuffer.wrap(Base64.getDecoder().decode(base64CipherText)))
            .withEncryptionContext(encryptionContext)

        val resp = client.decrypt(req)
        if (resp.keyId == null || resp.keyId!!.contentEquals(keyIdArn))  throw IllegalStateException("keyid not match ! provided $keyIdArn, actual ${resp.keyId}");

        return resp.plaintext.array().toString(StandardCharsets.UTF_8)
    }
}

特别感谢 @kdgregory 指出我的困惑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 2014-07-09
    相关资源
    最近更新 更多