Encryption and decryption
Use the KMS instance SDK client to call the Encrypt and Decrypt APIs for symmetric key encryption and decryption. This page provides a complete PHP example followed by a step-by-step walkthrough.
Prerequisites
Before you begin, ensure that you have:
A KMS instance with a symmetric master key (CMK) created
A ClientKey obtained from KMS application management
The KMS instance SDK for PHP installed
For client initialization details, see Initialize the client.
How it works
The encryption and decryption flow has three steps:
Initialize the KMS instance SDK client with your ClientKey and instance endpoint.
Call the
EncryptAPI with the plaintext. The response includes the ciphertext and an initialization vector (IV).Call the
DecryptAPI with the ciphertext, algorithm, and the IV from the encrypt response.
Save the iv field from the encrypt response. The Decrypt API requires the exact IV used during encryption — without it, decryption will fail.
Complete example
Example walkthrough
The following sections break down the complete example into three parts. The code snippets are extracted from the complete example above.
Initialize the client
See Initialize the client for full details. The relevant snippet from the complete example:
<?php
use AlibabaCloud\Dkms\Gcs\Sdk\Client as AlibabaCloudDkmsGcsSdkClient;
use AlibabaCloud\Dkms\Gcs\OpenApi\Models\Config as AlibabaCloudDkmsGcsOpenApiConfig;
function getDkmsGcsSdkClient()
{
global $clientKeyContent, $password, $endpoint;
$config = new AlibabaCloudDkmsGcsOpenApiConfig();
$config->protocol = 'https'; // KMS instance only allows HTTPS
$config->clientKeyContent = $clientKeyContent;
$config->password = $password; // ClientKey security token
$config->endpoint = $endpoint; // Format: <instance-id>.cryptoservice.kms.aliyuncs.com
$config->caFilePath = 'path/to/caCert.pem';
return new AlibabaCloudDkmsGcsSdkClient($config);
}Call the Encrypt API to encrypt data using a symmetric key
function aesEncryptSample($client, $keyId, $plaintext, $algorithm)
{
$encryptRequest = new EncryptRequest();
$encryptRequest->keyId = $keyId;
$encryptRequest->algorithm = $algorithm;
$encryptRequest->plaintext = AlibabaCloudTeaUtils::toBytes($plaintext);
$runtimeOptions = new RuntimeOptions();
// Uncomment the following line to skip SSL certificate verification (not recommended for production).
// $runtimeOptions->ignoreSSL = true;
try {
$encryptResponse = $client->encryptWithOptions($encryptRequest, $runtimeOptions);
var_dump($encryptResponse->toMap());
// Save all four fields — you need them to decrypt the ciphertext later.
return new AesEncryptContext([
'keyId' => $encryptResponse->keyId,
'iv' => $encryptResponse->iv, // Required by Decrypt
'ciphertextBlob' => $encryptResponse->ciphertextBlob,
'algorithm' => $encryptResponse->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;
}Call the Decrypt API to decrypt ciphertext using a symmetric key
function aesDecryptSample($client, $ctx)
{
$decryptRequest = new DecryptRequest();
$decryptRequest->keyId = $ctx->keyId;
$decryptRequest->ciphertextBlob = $ctx->ciphertextBlob;
$decryptRequest->algorithm = $ctx->algorithm;
$decryptRequest->iv = $ctx->iv; // Must match the IV from encryption
$runtimeOptions = new RuntimeOptions();
// Uncomment the following line to skip SSL certificate verification (not recommended for production).
// $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;
}