0

我创建了一个密钥环,然后创建了一个密钥。然后使用导入作业创建了一个包装密钥。之后使用下面的代码来解密普通文本。但我得到以下错误:

.InvalidArgument: 400 解包 KmsWrappedCryptoKey "projects/XXXXXXXXXXXX/location s/global/keyRings/demo-keyring/cryptoKeys/demo_v1" 时从 Cloud KMS 收到以下错误消息:解密失败:密文无效。

下面是代码:

# Import the client library
import google.cloud.dlp

# Instantiate a client
dlp = google.cloud.dlp_v2.DlpServiceClient()
project = 'XXXXXX'
stringVal = 'My name is Sonal Singh and my email id is : sonalsingh@gmail.com'
alphabet='ALPHA_NUMERIC'
surrogate_type='EMAIL_ADDRESS'
wrapped_key=('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+'
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+gr'
'l+XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+'
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+/+'
'//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=')

#key_name = ('projects/XXXXXXXXXXXXX/locations/global/keyRings/demo-keyring/cryptoKeys/demo_key')


parent = dlp.project_path(project)
# The wrapped key is base64-encoded, but the library expects a binary
# string, so decode it here.
import base64

wrapped_key = base64.b64decode(wrapped_key)

# Construct FPE configuration dictionary
crypto_replace_ffx_fpe_config = {
        "crypto_key": {
            "kms_wrapped": {
                "wrapped_key": wrapped_key,
                "crypto_key_name": key_name,
            }
        },
        "common_alphabet": alphabet,
}

# Add surrogate type
if surrogate_type:
        crypto_replace_ffx_fpe_config["surrogate_info_type"] = {
            "name": surrogate_type
        }

# Construct inspect configuration dictionary
inspect_config = {
        "info_types": [{"name": info_type} for info_type in ["FIRST_NAME", "LAST_NAME", "EMAIL_ADDRESS"]]
        }

# Construct deidentify configuration dictionary
deidentify_config = {
        "info_type_transformations": {
            "transformations": [
                {
                    "primitive_transformation": {
                        "crypto_replace_ffx_fpe_config": crypto_replace_ffx_fpe_config
                    }
                }
            ]
        }
    }

# Convert string to item
item = {"value": stringVal}

# Call the API
response = dlp.deidentify_content(
        parent,
        inspect_config=inspect_config,
        deidentify_config=deidentify_config,
        item=item
    )

# Print results
print(response.item.value)

我可以看到另一个具有相同问题的堆栈溢出帖子: GCP DLP(数据丢失预防)得到“解密失败:密文无效。” 但不确定此步骤是什么意思:在您对 Google Cloud DLP API 的请求中使用此生成的值。

如何在上面的代码中使用这个值?

4

1 回答 1

0

是的,我认为您发现的另一个 StackOverflow 问题可以在这里为我们提供帮助。

我对python不是很精通,但是我看到了一些我想指出的东西。我认为您正在正确执行其他 StackOverflow 帖子1中的第 1 步和第 3步,但是您缺少使用 Cloud KMS 加密的第 2 步(根据您的情况解密)。

你有没有机会过去:

https://cloud.google.com/kms/docs/reference/libraries#client-libraries-usage-python https://cloud.google.com/kms/docs/encrypt-decrypt#kms-howto-encrypt-python

另外,请知道您已在代码上发布了您的电子邮件,您可能需要对其进行编辑。


于 2020-03-19T15:52:08.810 回答