After initializing the KMS instance SDK client, you can use it to call the Encrypt and Decrypt APIs for data encryption and decryption. This topic provides code examples for this.
Complete example
Example walkthrough
Initialize client
<?php
use AlibabaCloud\Dkms\Gcs\Sdk\Client as AlibabaCloudDkmsGcsSdkClient;
use AlibabaCloud\Dkms\Gcs\OpenApi\Models\Config as AlibabaCloudDkmsGcsOpenApiConfig;
function getDkmsGcsSdkClient()
{
global $clientKeyContent, $password, $endpoint;
// Construct the KMS instance SDK client configuration.
$config = new AlibabaCloudDkmsGcsOpenApiConfig();
// The connection protocol. Set the value to https. The KMS instance service only allows access through the HTTPS protocol.
$config->protocol = 'https';
// Client Key.
$config->clientKeyContent = $clientKeyContent;
// Client Key security token.
$config->password = $password;
// The endpoint of your KMS instance. Set the value in the following format: <ID of your KMS instance >.cryptoservice.kms.aliyuncs.com.
$config->endpoint = $endpoint;
// Instance CA certificate.
$config->caFilePath = 'path/to/caCert.pem';
// Construct the KMS instance SDK client object.
return new AlibabaCloudDkmsGcsSdkClient($config);
}Call the Encrypt API to encrypt data using a symmetric key
/**
* Encryption example
* @param AlibabaCloudDkmsGcsSdkClient $client
* @param string $keyId
* @param string $plaintext
* @param string $algorithm
* @return AesEncryptContext
*/
function aesEncryptSample($client, $keyId, $plaintext, $algorithm)
{
// Construct encryption request
$encryptRequest = new EncryptRequest();
$encryptRequest->keyId = $keyId;
$encryptRequest->algorithm = $algorithm;
$encryptRequest->plaintext = AlibabaCloudTeaUtils::toBytes($plaintext);
$runtimeOptions = new RuntimeOptions();
// Ignore the server certificate
//$runtimeOptions->ignoreSSL = true;
try {
// Call the encryption API to encrypt
$encryptResponse = $client->encryptWithOptions($encryptRequest, $runtimeOptions);
// Key ID
$keyId = $encryptResponse->keyId;
// When the master key is a symmetric key, the decrypt API requires the Iv returned by encryption
$iv = $encryptResponse->iv;
// Data ciphertext
$cipher = $encryptResponse->ciphertextBlob;
// Encryption algorithm
$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;
}Call the Decrypt API to decrypt ciphertext using a symmetric key
/**
* Decryption example
* @param AlibabaCloudDkmsGcsSdkClient $client
* @param AesEncryptContext $ctx
* @return int[]|null
*/
function aesDecryptSample($client, $ctx)
{
// Construct decryption request object
$decryptRequest = new DecryptRequest();
$decryptRequest->keyId = $ctx->keyId;
$decryptRequest->ciphertextBlob = $ctx->ciphertextBlob;
$decryptRequest->algorithm = $ctx->algorithm;
$decryptRequest->iv = $ctx->iv;
$runtimeOptions = new RuntimeOptions();
// Ignore the certificate
//$runtimeOptions->ignoreSSL = true;
try {
// Call the decryption API to decrypt
$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;
}