初始化KMS執行個體SDK用戶端後,您可以通過用戶端調用Encrypt和Decrypt介面對資料進行加密解密。本文介紹加密解密的程式碼範例。
完整程式碼範例
整合KMS進行對稱式加密解密包含三個步驟:
源碼github地址:AesEncryptDecrypt.php
程式碼範例解析
初始化用戶端
關於初始化用戶端的詳細介紹,請參見初始化用戶端。
<?php
use AlibabaCloud\Dkms\Gcs\Sdk\Client as AlibabaCloudDkmsGcsSdkClient;
use AlibabaCloud\Dkms\Gcs\OpenApi\Models\Config as AlibabaCloudDkmsGcsOpenApiConfig;
function getDkmsGcsSdkClient()
{
global $clientKeyContent, $password, $endpoint;
// 構建KMS執行個體SDK Client配置
$config = new AlibabaCloudDkmsGcsOpenApiConfig();
//連線協定請設定為"https"。KMS執行個體服務僅允許通過HTTPS協議訪問。
$config->protocol = 'https';
//Client Key。
$config->clientKeyContent = $clientKeyContent;
//Client Key口令。
$config->password = $password;
//設定endpoint為<your KMS Instance Id>.cryptoservice.kms.aliyuncs.com。
$config->endpoint = $endpoint;
// 執行個體CA認證
$config->caFilePath = 'path/to/caCert.pem';
// 構建KMS執行個體SDK Client對象
return new AlibabaCloudDkmsGcsSdkClient($config);
}調用Encrypt介面使用對稱金鑰對資料加密
/**
* 加密樣本
* @param AlibabaCloudDkmsGcsSdkClient $client
* @param string $keyId
* @param string $plaintext
* @param string $algorithm
* @return AesEncryptContext
*/
function aesEncryptSample($client, $keyId, $plaintext, $algorithm)
{
// 構建加密請求
$encryptRequest = new EncryptRequest();
$encryptRequest->keyId = $keyId;
$encryptRequest->algorithm = $algorithm;
$encryptRequest->plaintext = AlibabaCloudTeaUtils::toBytes($plaintext);
$runtimeOptions = new RuntimeOptions();
// 忽略服務端認證
//$runtimeOptions->ignoreSSL = true;
try {
// 調用加密介面進行加密
$encryptResponse = $client->encryptWithOptions($encryptRequest, $runtimeOptions);
// 密鑰ID
$keyId = $encryptResponse->keyId;
// 主要金鑰是對稱金鑰時,decrypt介面需要加密返回的Iv
$iv = $encryptResponse->iv;
// 資料密文
$cipher = $encryptResponse->ciphertextBlob;
// 密碼編譯演算法
$algorithm = $encryptResponse->algorithm;
var_dump($encryptResponse->toMap());
return new AesEncryptContext([
'keyId' => $keyId,
'iv' => $iv,
'ciphertextBlob' => $cipher,
'algorithm' => $algorithm
]);
} catch (\Exception $error) {
if ($error instanceof \AlibabaCloud\Tea\Exception\TeaError) {
var_dump($error->getErrorInfo());
}
var_dump($error->getMessage());
var_dump($error->getTraceAsString());
}
return null;
}調用Decrypt介面使用對稱金鑰解密密文
/**
* 解密樣本
* @param AlibabaCloudDkmsGcsSdkClient $client
* @param AesEncryptContext $ctx
* @return int[]|null
*/
function aesDecryptSample($client, $ctx)
{
// 構建解密請求對象
$decryptRequest = new DecryptRequest();
$decryptRequest->keyId = $ctx->keyId;
$decryptRequest->ciphertextBlob = $ctx->ciphertextBlob;
$decryptRequest->algorithm = $ctx->algorithm;
$decryptRequest->iv = $ctx->iv;
$runtimeOptions = new RuntimeOptions();
// 忽略認證
//$runtimeOptions->ignoreSSL = true;
try {
// 調用解密介面進行解密
$decryptResponse = $client->decryptWithOptions($decryptRequest, $runtimeOptions);
var_dump($decryptResponse->toMap());
return $decryptResponse->plaintext;
} catch (Exception $error) {
if ($error instanceof \AlibabaCloud\Tea\Exception\TeaError) {
var_dump($error->getErrorInfo());
}
var_dump($error->getMessage());
var_dump($error->getTraceAsString());
}
return null;
}