SDK参考

<p class="shortdesc"></p> <p class="p">目前,平安云密钥管理服务提供JAVA语言版本的SDK,SDK下载具体链接如下:</p> <p class="p"><a class="xref" href="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/sdk%2Fkms_service_sdk-1.1.0.jar?response-content-disposition=attachment%3Bfilename%3Dkms_service_sdk-1.1.0.jar" target="_blank">KMS SDK 1.1.0</a></p> <p class="p"><a class="xref" href="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201803203923-16c02e089476.jar" target="_blank">KMS SDK 1.2.0</a></p> <p class="p">SDK调用示例代码:</p> <pre class="pre codeblock"><code>import java.sql.Timestamp; import cn.com.paic.pacloud.sdk.kms.client.IClientProfile; import cn.com.paic.pacloud.sdk.kms.client.KmsClient; import cn.com.paic.pacloud.sdk.kms.client.KmsDefaultProfile; import cn.com.paic.pacloud.sdk.kms.exception.KmsClientException; import cn.com.paic.pacloud.sdk.kms.http.MethodType; import cn.com.paic.pacloud.sdk.kms.http.ProtocolType; import cn.com.paic.pacloud.sdk.kms.json.JSONObject; import cn.com.paic.pacloud.sdk.kms.request.req.CancelKeyDeletionRequest; import cn.com.paic.pacloud.sdk.kms.request.req.CreateKeyRequest; import cn.com.paic.pacloud.sdk.kms.request.req.DecryptRequest; import cn.com.paic.pacloud.sdk.kms.request.req.DeleteKeyMaterialRequest; import cn.com.paic.pacloud.sdk.kms.request.req.DescribeKeyRequest; import cn.com.paic.pacloud.sdk.kms.request.req.DisableKeyRequest; import cn.com.paic.pacloud.sdk.kms.request.req.EnableKeyRequest; import cn.com.paic.pacloud.sdk.kms.request.req.EncryptRequest; import cn.com.paic.pacloud.sdk.kms.request.req.GenerateDataKeyRequest; import cn.com.paic.pacloud.sdk.kms.request.req.GetKeychainPasswordRequest; import cn.com.paic.pacloud.sdk.kms.request.req.GetParametersForImportRequest; import cn.com.paic.pacloud.sdk.kms.request.req.ImportKeyMaterialRequest; import cn.com.paic.pacloud.sdk.kms.request.req.ImportKeychainRequest; import cn.com.paic.pacloud.sdk.kms.request.req.ListKeysRequest; import cn.com.paic.pacloud.sdk.kms.request.req.ScheduleKeyDeletionRequest; import cn.com.paic.pacloud.sdk.kms.response.AcsError; import cn.com.paic.pacloud.sdk.kms.response.AcsResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.CancelKeyDeletionResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.CreateKeyResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.DecryptResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.DeleteKeyMaterialResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.DescribeKeyResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.DisableKeyResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.EnableKeyResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.EncryptResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.GenerateDataKeyResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.GetKeychainPasswordResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.GetParametersForImportResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.ImportKeyMaterialResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.ImportKeychainResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.ListKeysResponse; import cn.com.paic.pacloud.sdk.kms.response.resp.ScheduleKeyDeletionResponse; public class Sample {            static KmsClient kmsClient;            private static KmsClient kmsClient(String accessKeyId, String accessKeySecret) throws KmsClientException {                      IClientProfile profile = KmsDefaultProfile.getProfile("kms-cn-shanghai.yun.pingan.com", accessKeyId, accessKeySecret);                      KmsClient client = new KmsClient(profile);                      return client;            }            public static void main(String[] args) throws KmsClientException {                    System.out.println("===========================================");                      System.out.println("Getting Started with KMS Service");                      System.out.println("===========================================\n");                      String accessKeyId = "OTJFREJFQTQxMEFDNDI4Qzk0MkRFMjFBODE2N0E4Nj1";                      String accessKeySecret = "RkVFRDc3MTg5RTgwNEEwODhENEYxMUEzOEVBQ0NDRDd";                      kmsClient = kmsClient(accessKeyId, accessKeySecret);                      try {                                  CreateKey("test", "test alias", 0, "RSA");                      } catch (KmsClientException eResponse) {                                 System.out.println("Failed.");                                 System.out.println("Error code: " + eResponse.getErrCode());                                 System.out.println("Error message: " + eResponse.getErrMsg());                      }            }           public static void CreateKey(String keyDesc, String alias, Integer origin, String algorithm) throws KmsClientException {                      final CreateKeyRequest request = new CreateKeyRequest();                      request.setProtocol(ProtocolType.HTTPS);                      request.setMethod(MethodType.POST);                      request.setAlias(alias);                      request.setDescription(keyDesc);                      request.setKeyUsage(0);                      request.setOrigin(origin);                      request.setAlgorithm(algorithm);                      final AcsResponse response = kmsClient.getAcsResponse(request);                      if (response instanceof CreateKeyResponse) {                                 CreateKeyResponse createKeyResponse = (CreateKeyResponse) response;                                 JSONObject jsonObject = new JSONObject(createKeyResponse);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Create Key Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Create Key Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void DisableKey(String keyId) throws KmsClientException {                      final DisableKeyRequest disableKeyRequest = new DisableKeyRequest();                      disableKeyRequest.setProtocol(ProtocolType.HTTPS);                      disableKeyRequest.setMethod(MethodType.POST);                      disableKeyRequest.setKeyId(keyId);                      final AcsResponse response = kmsClient.getAcsResponse(disableKeyRequest);                      if (response instanceof DisableKeyResponse) {                                 DisableKeyResponse disableKeyResponse = (DisableKeyResponse) response;                                 JSONObject jsonObject = new JSONObject(disableKeyResponse);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Disable Key Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 System.out.println(jsonObject);                                 System.out.println("===========================================");                                 System.out.println("Disable Key Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void GetKeyParametersToImport(String keyId, String wappingKeySpec, String wrappingAlgorithm) throws KmsClientException {                      final GetParametersForImportRequest getParametersForImportRequest = new GetParametersForImportRequest();                      getParametersForImportRequest.setProtocol(ProtocolType.HTTPS);                      getParametersForImportRequest.setMethod(MethodType.POST);                      getParametersForImportRequest.setKeyId(keyId);                      getParametersForImportRequest.setWrappingKeySpec("RSA");                      getParametersForImportRequest.setWrappingAlgorithm("RSAES_OAEP_SHA_1");                      getParametersForImportRequest.setWrappingKeySpec(wappingKeySpec);                      getParametersForImportRequest.setWrappingAlgorithm(wrappingAlgorithm);                      final AcsResponse response = kmsClient.getAcsResponse(getParametersForImportRequest);                      if (response instanceof GetParametersForImportResponse) {                                 GetParametersForImportResponse disableKeyResponse = (GetParametersForImportResponse) response;                                 JSONObject jsonObject = new JSONObject(disableKeyResponse);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Disable Key Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 System.out.println(jsonObject);                                 System.out.println("===========================================");                                 System.out.println("Disable Key Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            private static void EnableKey(String keyId) throws KmsClientException {                      final EnableKeyRequest enableKeyRequest = new EnableKeyRequest();                      enableKeyRequest.setProtocol(ProtocolType.HTTPS);                      enableKeyRequest.setMethod(MethodType.POST);                      enableKeyRequest.setKeyId(keyId);                      final AcsResponse response = kmsClient.getAcsResponse(enableKeyRequest);                      if (response instanceof EnableKeyResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Enable Key Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Disable Key Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void DescribeKey(String keyId) {                      DescribeKeyRequest request = new DescribeKeyRequest();                      request.setKeyId(keyId);                      request.setMethod(MethodType.POST);                      request.setProtocol(ProtocolType.HTTPS);                      final AcsResponse response = kmsClient.getAcsResponse(request);                      if (response instanceof DescribeKeyResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("DescribeKey Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("DescribeKey Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            private static void ListKeys() throws KmsClientException {                      final ListKeysRequest decryptRequest = new ListKeysRequest();                      decryptRequest.setProtocol(ProtocolType.HTTPS);                      decryptRequest.setMethod(MethodType.POST);                      decryptRequest.setPageSize(10);                      decryptRequest.setPageNumber(1);                      final AcsResponse response = kmsClient.getAcsResponse(decryptRequest);                      if (response instanceof ListKeysResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("ListKeys Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Disable Key Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void ScheduleKeyDeletion(String keyId, Integer pendingWindowInDays) {                      ScheduleKeyDeletionRequest request = new ScheduleKeyDeletionRequest();                      request.setKeyId(keyId);                      request.setPendingWindowInDays(pendingWindowInDays);                      request.setMethod(MethodType.POST);                      request.setProtocol(ProtocolType.HTTPS);                      final AcsResponse response = kmsClient.getAcsResponse(request);                      if (response instanceof ScheduleKeyDeletionResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("ScheduleKeyDeletion Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("ScheduleKeyDeletion Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void CancelKeyDeletion(String keyId) {                      CancelKeyDeletionRequest ckdr = new CancelKeyDeletionRequest();                      ckdr.setKeyId(keyId);                      ckdr.setMethod(MethodType.POST);                      ckdr.setProtocol(ProtocolType.HTTPS);                      final AcsResponse response = kmsClient.getAcsResponse(ckdr);                      if (response instanceof CancelKeyDeletionResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("CancelKeyDeletion Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("CancelKeyDeletion Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            private static void Encrypt(String keyId, String plainText, String encryptionContext) throws KmsClientException {                      final EncryptRequest encryptRequest = new EncryptRequest();                      encryptRequest.setProtocol(ProtocolType.HTTPS);                      encryptRequest.setMethod(MethodType.POST);                      encryptRequest.setKeyId(keyId);                      encryptRequest.setPlainText(plainText);                      encryptRequest.setEncryptionContext(encryptionContext);                      final AcsResponse response = kmsClient.getAcsResponse(encryptRequest);                      if (response instanceof EncryptResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Encrypt Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Disable Key Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            private static void Decrypt(String cipherTextBlob, String encryptionContext) throws KmsClientException {                      final DecryptRequest decryptRequest = new DecryptRequest();                      decryptRequest.setProtocol(ProtocolType.HTTPS);                      decryptRequest.setMethod(MethodType.POST);                      decryptRequest.setCipherTextBlob(cipherTextBlob);                      decryptRequest.setEncryptionContext(encryptionContext);                      final AcsResponse response = kmsClient.getAcsResponse(decryptRequest);                      if (response instanceof DecryptResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Decrypt Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("Disable Key Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void GenerateDataKey(String keyId, String keySpec, Integer numberOfBytes, String encryptionContext) {                      GenerateDataKeyRequest request = new GenerateDataKeyRequest();                      request.setKeyId(keyId);                      request.setKeySpec(keySpec);                      request.setNumberOfBytes(numberOfBytes);                      request.setEncryptionContext(encryptionContext);                      request.setMethod(MethodType.POST);                      request.setProtocol(ProtocolType.HTTPS);                      final AcsResponse response = kmsClient.getAcsResponse(request);                      if (response instanceof GenerateDataKeyResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("GenerateDataKey Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("GenerateDataKey Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void GetParametersForImport(String keyId, String wrappingAlgorithm, String wrappingKeySpec) {                      GetParametersForImportRequest request = new GetParametersForImportRequest();                      request.setKeyId(keyId);                      request.setWrappingAlgorithm(wrappingAlgorithm);                      request.setWrappingKeySpec(wrappingKeySpec);                      request.setMethod(MethodType.POST);                      request.setProtocol(ProtocolType.HTTPS);                      final AcsResponse response = kmsClient.getAcsResponse(request);                      if (response instanceof GetParametersForImportResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("GetParametersForImport Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("GetParametersForImport Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void ImportKeyMaterial(String encryptedKeyMaterial, String importToken, Timestamp keyMaterialExpireUnix) {                      ImportKeyMaterialRequest request = new ImportKeyMaterialRequest();                      request.setEncryptedKeyMaterial(encryptedKeyMaterial);                      request.setImportToken(importToken);                      request.setKeyMaterialExpireUnix(keyMaterialExpireUnix);                      request.setMethod(MethodType.POST);                      request.setProtocol(ProtocolType.HTTPS);                      final AcsResponse response = kmsClient.getAcsResponse(request);                      if (response instanceof ImportKeyMaterialResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("ImportKeyMaterial Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("ImportKeyMaterial Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void DeleteKeyMaterial(String keyId) {                      DeleteKeyMaterialRequest request = new DeleteKeyMaterialRequest();                      request.setKeyId(keyId);                      request.setMethod(MethodType.POST);                      request.setProtocol(ProtocolType.HTTPS);                      final AcsResponse response = kmsClient.getAcsResponse(request);                      if (response instanceof DeleteKeyMaterialResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("DeleteKeyMaterial Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("DeleteKeyMaterial Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void ImportKeychain(String productName, String productCode, String keychainAccount, String keychainPassword, String algorithm, String description) {                      ImportKeychainRequest request = new ImportKeychainRequest();                      request.setProductName(productName);                      request.setProductCode(productCode);                      request.setKeychainAccount(keychainAccount);                      request.setKeychainPassword(keychainPassword);                      request.setAlgorithm(algorithm);                      request.setDescription(description);                      request.setMethod(MethodType.POST);                      request.setProtocol(ProtocolType.HTTPS);                      final AcsResponse response = kmsClient.getAcsResponse(request);                      if (response instanceof ImportKeychainResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("ImportKeychain Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("ImportKeychain Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            }            public static void GetKeychainPassword(String appId, String keychainAccount, String sign, String reason, String requestTime) {                      GetKeychainPasswordRequest request = new GetKeychainPasswordRequest();                      request.setAppId(appId);                      request.setKeychainAccount(keychainAccount);                      request.setSign(sign);                      request.setReason(reason);                      request.setRequestTime(requestTime);                      request.setMethod(MethodType.POST);                      request.setProtocol(ProtocolType.HTTPS);                      final AcsResponse response = kmsClient.getAcsResponse(request);                      if (response instanceof GetKeychainPasswordResponse) {                                 JSONObject jsonObject = new JSONObject(response);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("GetKeychainPassword Success!");                      } else {                                 AcsError error = (AcsError) response;                                 JSONObject jsonObject = new JSONObject(error);                                 String jsonResponse = jsonObject.toString();                                 System.out.println(jsonResponse);                                 System.out.println("===========================================");                                 System.out.println("GetKeychainPassword Failed! Reason: " + error.getMessage());                      }                      System.out.println("===========================================\n");            } }</code></pre>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询 - 7x24 小时

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

7x24 小时,急速解答

工单支持

解决云产品相关技术问题