-GitHub地址 ,欢迎贡献代码以及反馈问题。
python2.7 目前不支持python3
获取python版本的方法:
linux shell
$python -V
Python 2.7.11
windows cmd
D:>python -V
Python 2.7.11
如果提示不是内部或者外部命令,请先在window环境变量PATH里面添加上python的绝对路径
secretId='xxxxxx' #替换为用户的secretId
secretKey = 'xxxxxx' #替换为用户的secretKey
endpoint = 'https://kms-region.api.tencentyun.com' # 替换为用户的region , 例如 sh 表示上海, gz表示广州,bj表示北京
kms_account = KMSAccount(endpoint,secretId,secretKey)
客户端默认使用sha1 签名算法,可以调用签名算法修改签名方式
kms_account.set_sign_method('sha256')
def create_key(self, Description=None, Alias="", KeyUsage='ENCRYPT/DECRYPT')
参数名 | 类型 | 默认值 | 参数描述 |
---|---|---|---|
Description | string | None | 主密钥描述 |
Alias | string | 空字符串 | 主密钥别名 |
KeyUsage | string | 'ENCRYPT/DECRYPT' | 主密钥用途:默认是加解密 |
返回值 KeyMetadata结构体 描述如下:
属性名称 | 类型 | 含义 |
---|---|---|
KeyId | string | 密钥id |
CreateTime | uinx time | 创建时间 |
Description | string | 密钥描述 |
KeyState | string | 密钥状态 |
KeyUsage | string | 密钥用途 |
Alias | string | 密钥别名 |
description ='for test'
alias = 'kms_test'
kms_meta = kms_account.create_key(description,alias)
def get_key_attributes(self, KeyId=None)
参数名 | 类型 | 默认值 | 参数描述 |
---|---|---|---|
KeyId | string | None | 主密钥Id |
返回值 KeyMetadata结构体 描述如下:
属性名称 | 类型 | 含义 |
---|---|---|
KeyId | string | 密钥id |
CreateTime | uinx time | 创建时间 |
Description | string | 密钥描述 |
KeyState | string | 密钥状态 |
KeyUsage | string | 密钥用途 |
Alias | string | 密钥别名 |
keyId='' # 请填写你的keyId
key_meta = kms_account.get_key_attributes("kms-awy8dndb")
print key_meta
def list_key(self, offset=0, limit=10)
参数名 | 类型 | 默认值 | 参数描述 |
---|---|---|---|
offset | int | 0 | 返回列表偏移值。 |
limit | int | 10 | 本次返回列表限制个数,不填写默认为返回10个。 |
返回值 KeyMetadata结构体 描述如下:
属性名称 | 类型 | 含义 |
---|---|---|
totalCount | int | 表示所有的密钥个数。 |
keys | array | key数组。 |
totalCount, keys = kms_account.list_key()
print keys
def generate_data_key(self, KeyId=None, KeySpec=None, NumberOfBytes=None, EncryptionContext=None)
参数名 | 类型 | 默认值 | 参数描述 |
---|---|---|---|
KeyId | string | None | 主密钥Id。 |
KeySpec | string | None | 生成数据密钥算法。 |
NumberOfBytes | int | None | 生成指定长度的数据密钥。 |
返回值 (plaintext, ciphertextBlob)
plaintext 表示生成的数据密钥明文
ciphertextBlob:表示生成的数据密钥密文
KeySpec = "AES_128"
Plaintext, CiphertextBlob = kms_account.generate_data_key(KeyId, KeySpec)
print "the data key : %s \n the encrypted data key :%s\n" % (Plaintext, CiphertextBlob)
def enable_key(self, KeyId=None)
参数名 | 类型 | 默认值 | 参数描述 |
---|---|---|---|
KeyId | string | None | 主密钥Id |
返回值 无
kms_account.enable_key(KeyId)
def disable_key(self, KeyId=None)
参数名 | 类型 | 默认值 | 参数描述 |
---|---|---|---|
KeyId | string | None | 主密钥Id |
返回值 无
kms_account.disable_key(KeyId)
def encrypt(self, KeyId=None, Plaintext="", EncryptionContext=None)
参数名 | 类型 | 默认值 | 参数描述 |
---|---|---|---|
KeyId | string | None | 主密钥Id |
Plaintext | string | 空字符串 | 明文 |
EncryptionContext | string | None | key/value对的json字符串,如果指定了该参数,则在调用Decrypt API时需要提供同样的参数。 |
返回值 ciphertextBlob 密文:
Plaintest = "test message data"
CiphertextBlob = kms_account.encrypt(kms_meta.KeyId, Plaintest)
print "the encrypted data is :%s \n" % CiphertextBlob
def decrypt(self, CiphertextBlob="", EncryptionContext=None)
参数名 | 类型 | 默认值 | 参数描述 |
---|---|---|---|
CiphertextBlob | string | 空字符串 | 密文 |
EncryptionContext | string | None | key/value对的json字符串,如果指定了该参数,则在调用Decrypt API时需要提供同样的参数。 |
返回值 plaintext 明文:
Plaintest = kms_account.decrypt(CiphertextBlob)
print "the decrypted data is :%s\n" % Plaintest