-GitHub地址 ,欢迎贡献代码以及反馈问题。
- 安装openssl的库和头文件
- 安装libcurl
- 安装cmake工具
- 从控制台获取APP ID, SecretID,SecretKey。
下载github上提供的源码,集成到您的开发环境。
执行下面的命令:
cd ${kms-cpp-sdk}
mkdir -p build
cd build
cmake ..
make
sample/kms_sample.cpp里面有常见的api例子,生成的kms_sample可以直接运行,生成的libKMS.a 和libKMS.so文件可以放到自己的lib文件夹下,inc目录拷贝到自己工程的include路径下。
string secretId="xxxxxx; #替换为用户的secretId
string secretKey = "xxxxxx"; #替换为用户的secretKey
string endpoint = "https://kms-region.api.tencentyun.com"; # 替换为用户的region , 例如 sh 表示上海, gz表示广州,bj表示北京
KMSAccount account(endpoint,secretId,secretKey);
客户端默认使用sha1 签名算法,可以调用签名算法修改签名方式
account.set_sign_method("sha256");
void create_key(KeyMetadata & meta,const string &Description="",const string & Alias = "" , const string & KeyUsage="ENCRYPT/DECRYPT");
| 参数名 | 类型 | 默认值 | 参数描述 |
|---|---|---|---|
| KeyMetadata | struct | 主密钥属性结构体,该参数返回创建的主密钥属性结构 | |
| Description | string | None | 主密钥描述 |
| Alias | string | 空字符串 | 主密钥别名 |
| KeyUsage | string | 'ENCRYPT/DECRYPT' | 主密钥用途:默认是加解密 |
返回值 KeyMetadata结构体 描述如下:
| 属性名称 | 类型 | 含义 |
|---|---|---|
| KeyId | string | 密钥id |
| CreateTime | uinx time | 创建时间 |
| Description | string | 密钥描述 |
| KeyState | string | 密钥状态 |
| KeyUsage | string | 密钥用途 |
| Alias | string | 密钥别名 |
KeyMetadata meta ;
string description ="test";
string alias = "kms_test";
string KeyUsage="ENCRYPT/DECRYPT";
account.create_key(meta,Description,Alias,KeyUsage);
void get_key_attributes(const string & KeyId, KeyMetadata & meta);
| 参数名 | 类型 | 默认值 | 参数描述 |
|---|---|---|---|
| KeyId | string | None | 主密钥Id |
| KeyMetadata | struct | 主密钥属性结构体,该参数返回创建的主密钥属性结构 |
返回值 KeyMetadata结构体 描述如下:
| 属性名称 | 类型 | 含义 |
|---|---|---|
| KeyId | string | 密钥id |
| CreateTime | uinx time | 创建时间 |
| Description | string | 密钥描述 |
| KeyState | string | 密钥状态 |
| KeyUsage | string | 密钥用途 |
| Alias | string | 密钥别名 |
KeyMetadata meta;
string keyId="" # 请填写你的keyId
account.get_key_attributes(meta.KeyId,meta);
void list_key(vector<string> & keyIds, const int offset= 0 , const int limit = 10);
| 参数名 | 类型 | 默认值 | 参数描述 |
|---|---|---|---|
| keyIds | vector | 无 | 返回keyid vector |
| offset | int | 0 | 返回列表偏移值 |
| limit | int | 10 | 本次返回列表限制个数,不填写默认为返回10个 |
vector<string> KeyIds;
account.list_key(KeyIds);
for(unsigned int i = 0 ; i < KeyIds.size(); ++i)
cout<<"the "<<i<<" key id is :"<<KeyIds[i]<<endl;
void generate_data_key( string &KeyId, const string & KeySpace, int NumberOfBytes,const string & EncryptionContext,string & Plaintext,string &CiphertextBlob);
| 参数名 | 类型 | 默认值 | 参数描述 |
|---|---|---|---|
| KeyId | string | None | 主密钥Id |
| KeySpec | string | None | 生成数据密钥算法 |
| NumberOfBytes | int | None | 生成指定长度的数据密钥 |
| EncryptionContext | json string | 无 | 生成数据密钥时提供的额外的json key-value |
| Plaintext | string | 无 | 生成的数据密钥明文 |
| CiphertextBlob | string | 无 | 生成的数据密钥密文 |
返回值 入参中: plaintext 表示生成的数据密钥明文 ciphertextBlob:表示生成的数据密钥密文
string KeySpec="AES_128";
string Plaintext,CiphertextBlob;
account.generate_data_key(meta.KeyId,KeySpec,1024,"",Plaintext, CiphertextBlob);
void enable_key(const string & KeyId);
| 参数名 | 类型 | 默认值 | 参数描述 |
|---|---|---|---|
| KeyId | string | None | 主密钥Id |
返回值 无
string KeyId= "" // 请填写你的keyId;
account.enable_key(KeyId)
void disable_key(const string & KeyId);
| 参数名 | 类型 | 默认值 | 参数描述 |
|---|---|---|---|
| KeyId | string | None | 主密钥Id |
返回值 无
string KeyId= "" // 请填写你的keyId;
account.disable_key(KeyId)
string encrypt(const string &KeyId , const string & plaintext, const string & EncryptionContext);
| 参数名 | 类型 | 默认值 | 参数描述 |
|---|---|---|---|
| KeyId | string | None | 主密钥Id |
| Plaintext | string | 空字符串 | 明文 |
| EncryptionContext | string | None | key/value对的json字符串,如果指定了该参数,则在调用Decrypt API时需要提供同样的参数 |
返回值 ciphertextBlob 密文:
string KeyId = "" ; // 请填写你的keyId;
string Plaintest = "test message data"
string CiphertextBlob = account.encrypt(KeyId,Plaintest,"");
string decrypt(const string & CiphertextBlob, const string & EncryptionContext);
| 参数名 | 类型 | 默认值 | 参数描述 |
|---|---|---|---|
| CiphertextBlob | string | 空字符串 | 密文 |
| EncryptionContext | string | None | key/value对的json字符串,如果指定了该参数,则在调用Decrypt API时需要提供同样的参数。 |
返回值 plaintext 明文:
Plaintext = account.decrypt(CiphertextBlob,"");