Setelah menginisialisasi instance SDK client KMS, Anda dapat menggunakannya untuk memanggil API Encrypt dan Decrypt guna melakukan enkripsi dan dekripsi data. Topik ini menyediakan contoh kode terkait.
Contoh lengkap
Penjelasan contoh
Inisialisasi 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;
// Konstruksi konfigurasi client SDK instance KMS.
$config = new AlibabaCloudDkmsGcsOpenApiConfig();
// Protokol koneksi. Setel nilainya ke https. Layanan instance KMS hanya mengizinkan akses melalui protokol HTTPS.
$config->protocol = 'https';
// Client Key.
$config->clientKeyContent = $clientKeyContent;
// Token keamanan Client Key.
$config->password = $password;
// Endpoint instance KMS Anda. Setel nilainya dalam format berikut: <ID instance KMS Anda>.cryptoservice.kms.aliyuncs.com.
$config->endpoint = $endpoint;
// Sertifikat CA instance.
$config->caFilePath = 'path/to/caCert.pem';
// Konstruksi objek client SDK instance KMS.
return new AlibabaCloudDkmsGcsSdkClient($config);
}Panggil API Encrypt untuk mengenkripsi data menggunakan kunci simetris
/**
* Contoh enkripsi
* @param AlibabaCloudDkmsGcsSdkClient $client
* @param string $keyId
* @param string $plaintext
* @param string $algorithm
* @return AesEncryptContext
*/
function aesEncryptSample($client, $keyId, $plaintext, $algorithm)
{
// Membuat permintaan enkripsi
$encryptRequest = new EncryptRequest();
$encryptRequest->keyId = $keyId;
$encryptRequest->algorithm = $algorithm;
$encryptRequest->plaintext = AlibabaCloudTeaUtils::toBytes($plaintext);
$runtimeOptions = new RuntimeOptions();
// Abaikan sertifikat server
//$runtimeOptions->ignoreSSL = true;
try {
// Panggil API enkripsi untuk mengenkripsi
$encryptResponse = $client->encryptWithOptions($encryptRequest, $runtimeOptions);
// Key ID
$keyId = $encryptResponse->keyId;
// Saat kunci utama adalah kunci simetris, API decrypt memerlukan Iv yang dikembalikan oleh enkripsi
$iv = $encryptResponse->iv;
// Data ciphertext
$cipher = $encryptResponse->ciphertextBlob;
// Algoritma enkripsi
$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;
}Panggil API Decrypt untuk mendekripsi ciphertext menggunakan kunci simetris
/**
* Contoh dekripsi
* @param AlibabaCloudDkmsGcsSdkClient $client
* @param AesEncryptContext $ctx
* @return int[]|null
*/
function aesDecryptSample($client, $ctx)
{
// Membuat objek permintaan dekripsi
$decryptRequest = new DecryptRequest();
$decryptRequest->keyId = $ctx->keyId;
$decryptRequest->ciphertextBlob = $ctx->ciphertextBlob;
$decryptRequest->algorithm = $ctx->algorithm;
$decryptRequest->iv = $ctx->iv;
$runtimeOptions = new RuntimeOptions();
// Abaikan sertifikat
//$runtimeOptions->ignoreSSL = true;
try {
// Panggil API dekripsi untuk mendekripsi
$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;
}