服务端加密

最近更新时间:2024-08-13 16:47:51

我的收藏

简介

本文档提供关于如何使用在上传对象时开启服务端加密。服务端加密的密钥分为三种:
COS 托管加密密钥
客户提供的加密密钥
KMS 托管加密密钥

前期准备

创建 CosAPI

调用 COS 的接口之前,必须先创建一个 CosAPI 的实例。这个实例用来后续调用请求。
qcloud_cos::CosAPI InitCosAPI() {
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";// bucket 的地域,请参见 https://cloud.tencent.com/document/product/436/62
std::string secret_id = "AKIDXXXXXXXX"; //用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
std::string secret_key = "1A2Z3YYYYYYYYYY"; //用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

使用临时密钥创建 CosAPI

如果要使用临时密钥请求 COS,则需要用临时密钥创建 CosAPI 实例。
qcloud_cos::CosAPI InitCosAPI() {
// 需要已经获取到临时密钥的结果:tmp_secret_id、tmp_secret_key、
// 临时密钥的生成参见 https://cloud.tencent.com/document/product/436/14048#cos-sts-sdk
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";
std::string tmp_secret_id = "AKIDXXXXXXXX";
std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
std::string tmp_token = "token";
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

使用案例

使用 COS 托管加密密钥的服务端加密(SSE-COS)保护数据

由腾讯云 COS 托管主密钥和管理数据。COS 会帮助您在数据写入数据中心时自动加密,并在您取用该数据时自动解密。目前支持使用 COS 主密钥对数据进行 AES-256 加密。
void PutObjectByFileDemo(qcloud_cos::CosAPI& cos) {
std::string object_name = "test.txt";
std::string file_path = "./test_file/text.txt";
qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
req.SetXCosServerSideEncryption("AES256");
qcloud_cos::PutObjectByFileResp resp;
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
std::cout << "====================PutObjectByFile======================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}

使用客户提供的加密密钥的服务端加密 (SSE-C)保护数据

加密密钥由用户自己提供,用户在上传对象时,COS 将使用用户提供的加密密钥对用户的数据进行 AES-256 加密。
注意:
该加密所运行的服务需要使用 HTTPS 请求。
用户需要提供一个32字节的字符串作为密钥,支持数字、字母、字符的组合,不支持中文。
如果上传文件时设置了密钥加密,那么在使用 GET(下载)、HEAD(查询)源对象时也需要在请求中带上相同的密钥,才能正常响应。
void PutObjectByFileDemo(qcloud_cos::CosAPI& cos) {
std::string object_name = "test.txt";
std::string file_path = "./test_file/text.txt";
qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
req.SetHttps();
req.TurnOffComputeConentMd5();
req.AddHeader("x-cos-server-side-encryption-customer-algorithm", "AES256"); // 服务端加密算法,目前仅支持 AES256
req.AddHeader("x-cos-server-side-encryption-customer-key","key"); // 服务端加密密钥的 Base64 编码, 例如MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=
req.AddHeader("x-cos-server-side-encryption-customer-key-MD5","key-MD5"); // 服务端加密密钥的 MD5 哈希值,使用 Base64 编码 例如U5L61r7jcwdNvT7frmUG8g==
// 这里 MD5 哈希值的 Base64 编码,需在二进制编码的 MD5 基础上计算
qcloud_cos::PutObjectByFileResp resp;
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
std::cout << "====================PutObjectByFile======================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}
void GetObjectByFileDemo(qcloud_cos::CosAPI& cos) {
std::string object_name = "test.txt";
std::string file_path = "./test_file/text2.txt";
qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
req.SetHttps();
req.SetCheckMD5(false);
req.AddHeader("x-cos-server-side-encryption-customer-algorithm", "AES256"); // 服务端加密算法,目前仅支持 AES256
req.AddHeader("x-cos-server-side-encryption-customer-key","key"); // 服务端加密密钥的 Base64 编码, 例如MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=
req.AddHeader("x-cos-server-side-encryption-customer-key-MD5","key-MD5"); // 服务端加密密钥的 MD5 哈希值,使用 Base64 编码 例如U5L61r7jcwdNvT7frmUG8g==
qcloud_cos::GetObjectByFileResp resp;
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
std::cout << "===================GetObjectResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}

使用 KMS 托管加密密钥的服务端加密(SSE-KMS)保护数据

SSE-KMS 加密即使用 KMS 托管密钥的服务端加密。KMS 是腾讯云推出的一款安全管理类服务,使用经过第三方认证的硬件安全模块 HSM(Hardware Security Module)来生成和保护密钥。它能够帮助用户轻松创建和管理密钥,满足用户多应用多业务的密钥管理需求以及满足监管和合规要求。关于如何开通 KMS 服务请参见 服务端加密概述
void PutObjectByFileDemo(qcloud_cos::CosAPI& cos) {
std::string object_name = "test.txt";
std::string file_path = "./test_file/text.txt";
qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
req.SetXCosServerSideEncryption("cos/kms");
req.TurnOffComputeConentMd5();
req.AddHeader("x-cos-server-side-encryption-cos-kms-key-id","key-id"); // 指定 KMS 的用户主密钥 CMK,如不指定,则使用 COS 默认创建的 CMK
req.AddHeader("x-cos-server-side-encryption-context","context"); // 指定加密上下文,值为 JSON 格式加密上下文键值对的 Base64 编码。例如eyJhIjoiYXNkZmEiLCJiIjoiMTIzMzIxIn0=
qcloud_cos::PutObjectByFileResp resp;
qcloud_cos::CosResult result = cos.PutObject(req, &resp);
std::cout << "====================PutObjectByFile======================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}

void GetObjectByFileDemo(qcloud_cos::CosAPI& cos) {
std::string object_name = "test.txt";
std::string file_path = "./test_file/text2.txt";
qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
req.SetHttps();
req.SetCheckMD5(false);
qcloud_cos::GetObjectByFileResp resp;
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
std::cout << "===================GetObjectResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}