全部產品
Search
文件中心

Key Management Service:專屬KMS PHP SDK

更新時間:Jul 06, 2024

專屬KMS SDK for PHP協助您通過簡單的編程訪問專屬KMS的API,實現加密解密、簽名驗簽和擷取憑據資訊的業務訴求。

背景資訊

您可以訪問開原始碼倉庫,查看SDK源碼及程式碼範例。同時也歡迎您提出寶貴意見,或者提供程式碼範例。

前提條件

  • 您已經啟用專屬KMS執行個體並正常串連密碼機,為執行個體建立密鑰及應用存取點,並儲存了Client Key及CA認證。具體操作,請參見快速入門
    說明 CA認證下載後檔案名稱預設為PrivateKmsCA_kst-******.pem,應用身份憑證檔案下載後檔案名稱預設為ClientKey_******.json。
  • 已經擷取專屬KMS執行個體VPC地址,並確保可以通過以下方式訪問專屬KMS執行個體VPC地址:
    • 在啟用密碼機執行個體叢集時設定的VPC中訪問專屬KMS執行個體VPC地址。
    • 本地裝置所在網路可以正常解析並訪問專屬KMS執行個體VPC地址。

    具體操作,請參見查詢專屬KMS標準版執行個體

安裝SDK

  • 方式一(推薦):通過Composer來管理專案依賴

    1. 在終端中切換到專案目錄,直接執行以下代碼安裝AlibabaCloud DKMS-GCS SDK for PHP作為依賴項。
      composer require alibabacloud/dkms-gcs-sdk
    2. 在composer.json中添加以下內容,聲明對AlibabaCloud DKMS-GCS SDK for PHP的依賴。
      "require": {
        "alibabacloud/dkms-gcs-sdk": "SDK版本"
        }
      說明 關於最新的SDK版本,請參考開原始碼倉庫
    3. 在終端中切換到專案目錄下,執行以下代碼安裝依賴。
      composer install
    4. 使用Composer安裝完成後,在PHP代碼中引入依賴。
      require_once __DIR__ . '/vendor/autoload.php';
  • 方式二:直接下載SDK源碼

    訪問開原始碼倉庫下載SDK源碼,在PHP代碼中引入SDK目錄下的autoload.php檔案。

    require_once '/path/to/dkms-gcs-sdk/autoload.php';

初始化SDK

您可以初始化一個專屬KMS標準版執行個體的PHP用戶端,用於調用專屬KMS標準版執行個體管理的密鑰等資源。使用PHP SDK發起專屬KMS API請求,您需要初始化一個Client執行個體,並根據需要修改Config的預設配置項。

  1. 配置CA認證。

    為保障生產環境通訊安全,需要配置PHP可信認證。

    設定RuntimeOptionsverify欄位。範例程式碼如下:
    <?php
    
    use AlibabaCloud\Dkms\Gcs\OpenApi\Util\Models\RuntimeOptions;
    
    $runtimeOptions = new RuntimeOptions();
    $runtimeOptions->verify = 'path/to/caCert.pem';
    
    ...
    $encryptResponse = $client->encryptWithOptions($encryptRequest, $runtimeOptions);

    開發環境可使用RuntimeOptionsignoreSSL設定臨時忽略可信認證的驗證。範例程式碼如下:

    <?php
    
    use AlibabaCloud\Dkms\Gcs\OpenApi\Util\Models\RuntimeOptions;
    
    $runtimeOptions = new RuntimeOptions();
    $runtimeOptions->ignoreSSL = true;
    
    ...
    $encryptResponse = $client->encryptWithOptions($encryptRequest, $runtimeOptions);
  2. 建立專屬KMS標準版Client。

    建立專屬KMS標準版Client時,需要指定執行個體的Endpoint。EndPoint為專屬KMS標準版執行個體VPC地址去掉https://。關於專屬KMS標準版執行個體VPC地址的更多資訊,請參見查詢專屬KMS標準版執行個體

    <?php
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Client as AlibabaCloudDkmsGcsSdkClient;
    use AlibabaCloud\Dkms\Gcs\OpenApi\Models\Config as AlibabaCloudDkmsGcsOpenApiConfig;
    
    $config = new AlibabaCloudDkmsGcsOpenApiConfig();
    //連線協定,固定為HTTPS。
    $config->protocol = 'https';
    //專屬KMS標準版執行個體Client Key。
    $config->clientKeyContent = '<your client key content>';
    //專屬KMS標準版執行個體Client Key解密口令。
    $config->password = '<your client key password>';
    //Endpoint,專屬KMS標準版執行個體的服務地址去掉“https://”。
    //樣本:<service_id>.cryptoservice.kms.aliyuncs.com;
    $config->endpoint = '<your dkms instance service address>';
    
    $client = new AlibabaCloudDkmsGcsSdkClient($config);

程式碼範例

  • 專屬KMS標準版Client調用Encrypt介面使用對稱金鑰密碼編譯資料

    詳細程式碼範例,請參見原始代碼

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Models\EncryptRequest;
    
    $encryptRequest = new EncryptRequest();
    //專屬KMS標準版執行個體加密金鑰的ID或別名(Alias)。
    $encryptRequest->keyId = '<your cipher key id>';
    //待加密資料。
    $encryptRequest->plaintext = \AlibabaCloud\Dkms\Gcs\OpenApi\Util\Utils::toBytes('encrypt plaintext');
    
    $encryptResponse = $client->encryptWithOptions($encryptRequest, $runtimeOptions);
    //密文。
    $ciphertextBlob = $encryptResponse->ciphertextBlob;
    //Cipher初始向量,用於解密資料。
    $iv = $encryptResponse->iv;
    //請求ID。
    $requestId = $encryptResponse->requestId;
  • 專屬KMS標準版Client調用Decrypt介面使用對稱金鑰解密密文

    詳細程式碼範例,請參見原始代碼

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Models\DecryptRequest;
    
    $decryptRequest = new DecryptRequest();
    //專屬KMS標準版執行個體解密密鑰的ID或別名(Alias)。
    $decryptRequest->keyId = '<your cipher key id>';
    //待解密資料,加密返回的密文。
    $decryptRequest->ciphertextBlob = <your ciphertext>;
    //Cipher初始向量,必須與加密時一致。
    $decryptRequest->iv = <IV value>;
    
    $decryptResponse = $client->decryptWithOptions($decryptRequest, $runtimeOptions);
    //原始明文資料。
    $plaintext = $decryptResponse->plaintext;
    //請求ID。
    $requestId = $decryptResponse->requestId;
  • 專屬KMS標準版Client調用Sign介面使用非對稱金鑰進行數位簽章

    詳細程式碼範例,請參見原始代碼

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Models\SignRequest;
    
    $signRequest = new SignRequest();
    //專屬KMS標準版執行個體簽名密鑰的ID或別名(Alias)。
    $signRequest->keyId = 'your cipher key id';
    //待簽名資料。
    $signRequest->message = <the data to sign>;
    
    $signResponse = $client->signWithOptions($signRequest, $runtimeOptions);
    //簽名值。
    $signature = $signResponse->signature;
    //請求ID。
    $requestId = $signResponse->requestId;
  • 專屬KMS標準版Client調用Verify介面使用非對稱金鑰驗證數位簽章

    詳細程式碼範例,請參見原始代碼

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Models\VerifyRequest;
    
    $verifyRequest = new VerifyRequest();
    //專屬KMS標準版執行個體簽名密鑰的ID或別名(Alias)。
    $verifyRequest->keyId = 'your cipher key id';
    //待驗證簽名的資料。
    $verifyRequest->message = <the data to sign>;
    //待驗證簽名值。
    $verifyRequest->signature = <the signature to verify>;
    
    $verifyResponse = $client->verifyWithOptions($verifyRequest, $runtimeOptions);
    //驗簽結果。
    $value = $verifyResponse->value;
    //請求ID。
    $requestId = $verifyResponse->requestId;
  • 使用專屬KMS標準版Client調用GetSecretValue介面擷取憑據值

    詳細程式碼範例,請參見原始代碼

    重要 0.2.2及以上版本的專屬KMS PHP SDK才支援擷取憑據值。
    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use AlibabaCloud\Dkms\Gcs\Sdk\Models\GetSecretValueRequest;
    
    // 您在專屬KMS建立的憑據名稱。
    $secretName = '<your secret name>';
    
    $getSecretValueRequest = new GetSecretValueRequest([
        'secretName' => $secretName,
    ]);
    
    // 調用擷取憑據介面。
    $getSecretValueResponse = $client->getSecretValueWithOptions($getSecretValueRequest, $runtimeOptions);
    
    // 憑據值。
    $_secretData = $getSecretValueResponse->secretData;
    // 請求ID。
    $_requestId = $getSecretValueResponse->requestId;