SecretID, SecretKey, and endpoint. The general format of the endpoint is *.tencentcloudapi.com. For example, the endpoint of KMS is kms.tencentcloudapi.com. For more information, please see the documentation of the specified product.pip install tencentcloud-sdk-python
CreateKey API.Encrypt API of KMS to encrypt your sensitive data and get the ciphertext.Decrypt API of KMS to decrypt the ciphertext into plaintext.Encrypt API is used to encrypt up to 4 KB of data, such as database passwords, RSA keys, or other sensitive information. This document describes how to encrypt data through the SDK for Python. You can also use other supported programming languages.KeyId and Plaintext parameters are required for this API. For more information, please see the Encrypt API document.# -*- coding: utf-8 -*-import base64from tencentcloud.common import credentialfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.common.profile.client_profile import ClientProfilefrom tencentcloud.common.profile.http_profile import HttpProfilefrom tencentcloud.kms.v20190118 import kms_client, modelsdef KmsInit(region="ap-guangzhou", secretId="", secretKey=""):try:credProfile = credential.Credential(secretId, secretKey)client = kms_client.KmsClient(credProfile, region)return clientexcept TencentCloudSDKException as err:print(err)return Nonedef Encrypt(client, keyId="", plaintext=""):try:req = models.EncryptRequest()req.KeyId = keyIdreq.Plaintext = base64.b64encode(plaintext)rsp = client.Encrypt(req) # Call the `Encrypt` APIreturn rspexcept TencentCloudSDKException as err:print(err)return Noneif __name__ == '__main__':# User-defined parameterssecretId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"region = "ap-guangzhou"keyId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"plaintext = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"client = KmsInit(region, secretId, secretKey)rsp = Encrypt(client, keyId, plaintext)print "plaintext=", plaintext, ", cipher=", rsp.CiphertextBlob
Decrypt API is used to decrypt data.CiphertextBlob parameter is required for this API. For more information, please see the Decrypt API document.# -*- coding: utf-8 -*-import base64from tencentcloud.common import credentialfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.common.profile.client_profile import ClientProfilefrom tencentcloud.common.profile.http_profile import HttpProfilefrom tencentcloud.kms.v20190118 import kms_client, modelsdef KmsInit(region="ap-guangzhou", secretId="", secretKey=""):try:credProfile = credential.Credential(secretId, secretKey)client = kms_client.KmsClient(credProfile, region)return clientexcept TencentCloudSDKException as err:print(err)return Nonedef Decrypt(client, keyId="", ciphertextBlob=""):try:req = models.DecryptRequest()req.CiphertextBlob = ciphertextBlobrsp = client.Decrypt(req) # Call the `Decrypt` APIreturn rspexcept TencentCloudSDKException as err:print(err)return Noneif __name__ == '__main__':# User-defined parameterssecretId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"region = "ap-guangzhou"keyId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"ciphertextBlob = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"client = KmsInit(region, secretId, secretKey)rsp = Decrypt(client, keyId, ciphertextBlob)print "cipher=", ciphertextBlob, ", base64 decoded plaintext=", base64.b64decode(rsp.Plaintext)
Feedback