Search in sources :

Example 6 with EncryptResponse

use of com.aliyuncs.kms.model.v20160120.EncryptResponse in project spring-cloud-gcp by GoogleCloudPlatform.

the class KmsTemplateTests method testEncryptDecrypt.

@Test
void testEncryptDecrypt() {
    EncryptResponse encryptResponse = createEncryptResponse();
    DecryptResponse decryptResponse = createDecryptResponse();
    when(this.client.encrypt(any(EncryptRequest.class))).thenReturn(encryptResponse);
    when(this.client.decrypt(any(DecryptRequest.class))).thenReturn(decryptResponse);
    String cryptoKeyNameStr = "test-project/europe-west2/key-ring-id/key-id";
    byte[] encryptedBytes = kmsTemplate.encryptText(cryptoKeyNameStr, "1234");
    String decryptedText = kmsTemplate.decryptText(cryptoKeyNameStr, encryptedBytes);
    Assert.assertEquals("1234", decryptedText);
}
Also used : EncryptResponse(com.google.cloud.kms.v1.EncryptResponse) DecryptResponse(com.google.cloud.kms.v1.DecryptResponse) ByteString(com.google.protobuf.ByteString) DecryptRequest(com.google.cloud.kms.v1.DecryptRequest) EncryptRequest(com.google.cloud.kms.v1.EncryptRequest) Test(org.junit.jupiter.api.Test)

Example 7 with EncryptResponse

use of com.aliyuncs.kms.model.v20160120.EncryptResponse in project spring-cloud-gcp by GoogleCloudPlatform.

the class KmsTemplateTests method testEncryptDecryptMissMatch.

@Test
void testEncryptDecryptMissMatch() {
    EncryptResponse encryptResponse = createEncryptResponse();
    when(this.client.encrypt(any(EncryptRequest.class))).thenReturn(encryptResponse);
    when(this.client.decrypt(any(DecryptRequest.class))).thenThrow(InvalidArgumentException.class);
    String cryptoKeyNameStr = "test-project/europe-west2/key-ring-id/key-id";
    byte[] encryptedBytes = kmsTemplate.encryptText(cryptoKeyNameStr, "1234");
    assertThatThrownBy(() -> kmsTemplate.decryptText(cryptoKeyNameStr, encryptedBytes)).isInstanceOf(com.google.api.gax.rpc.InvalidArgumentException.class);
}
Also used : EncryptResponse(com.google.cloud.kms.v1.EncryptResponse) ByteString(com.google.protobuf.ByteString) DecryptRequest(com.google.cloud.kms.v1.DecryptRequest) EncryptRequest(com.google.cloud.kms.v1.EncryptRequest) Test(org.junit.jupiter.api.Test)

Example 8 with EncryptResponse

use of com.aliyuncs.kms.model.v20160120.EncryptResponse in project aliyun-oss-java-sdk by aliyun.

the class KmsEncryptionMaterials method encryptPlainText.

/**
 * Encrypt the plain text to cipherBlob.
 */
private EncryptResponse encryptPlainText(String keyId, String plainText) throws ClientException {
    DefaultAcsClient kmsClient = createKmsClient(region, credentialsProvider);
    final EncryptRequest encReq = new EncryptRequest();
    encReq.setSysProtocol(ProtocolType.HTTPS);
    encReq.setAcceptFormat(FormatType.JSON);
    encReq.setSysMethod(MethodType.POST);
    encReq.setKeyId(keyId);
    encReq.setPlaintext(plainText);
    final EncryptResponse encResponse;
    try {
        encResponse = kmsClient.getAcsResponse(encReq);
    } catch (Exception e) {
        throw new ClientException("the kms client encrypt data failed." + e.getMessage(), e);
    }
    return encResponse;
}
Also used : EncryptResponse(com.aliyuncs.kms.model.v20160120.EncryptResponse) DefaultAcsClient(com.aliyuncs.DefaultAcsClient) ClientException(com.aliyun.oss.ClientException) ClientException(com.aliyun.oss.ClientException) EncryptRequest(com.aliyuncs.kms.model.v20160120.EncryptRequest)

Example 9 with EncryptResponse

use of com.aliyuncs.kms.model.v20160120.EncryptResponse in project java-docs-samples by GoogleCloudPlatform.

the class SnippetsIT method testDecryptSymmetric.

@Test
public void testDecryptSymmetric() throws IOException {
    String plaintext = "my plaintext";
    byte[] ciphertext;
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
        CryptoKeyName keyName = CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID);
        EncryptResponse result = client.encrypt(keyName, ByteString.copyFromUtf8(plaintext));
        ciphertext = result.getCiphertext().toByteArray();
    }
    new DecryptSymmetric().decryptSymmetric(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID, ciphertext);
    assertThat(stdOut.toString()).contains(plaintext);
}
Also used : EncryptResponse(com.google.cloud.kms.v1.EncryptResponse) CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) ByteString(com.google.protobuf.ByteString) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient) Test(org.junit.Test)

Example 10 with EncryptResponse

use of com.aliyuncs.kms.model.v20160120.EncryptResponse in project java-kms by googleapis.

the class SnippetsIT method testDecryptSymmetric.

@Test
public void testDecryptSymmetric() throws IOException {
    String plaintext = "my plaintext";
    byte[] ciphertext;
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
        CryptoKeyName keyName = CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID);
        EncryptResponse result = client.encrypt(keyName, ByteString.copyFromUtf8(plaintext));
        ciphertext = result.getCiphertext().toByteArray();
    }
    new DecryptSymmetric().decryptSymmetric(PROJECT_ID, LOCATION_ID, KEY_RING_ID, SYMMETRIC_KEY_ID, ciphertext);
    assertThat(stdOut.toString()).contains(plaintext);
}
Also used : EncryptResponse(com.google.cloud.kms.v1.EncryptResponse) CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) ByteString(com.google.protobuf.ByteString) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient) Test(org.junit.Test)

Aggregations

EncryptResponse (com.google.cloud.kms.v1.EncryptResponse)13 KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)8 ByteString (com.google.protobuf.ByteString)8 EncryptRequest (com.google.cloud.kms.v1.EncryptRequest)7 CryptoKeyName (com.google.cloud.kms.v1.CryptoKeyName)6 Test (org.junit.jupiter.api.Test)3 ClientException (com.aliyun.oss.ClientException)2 EncryptResponse (com.aliyuncs.kms.model.v20160120.EncryptResponse)2 DecryptRequest (com.google.cloud.kms.v1.DecryptRequest)2 Test (org.junit.Test)2 DefaultAcsClient (com.aliyuncs.DefaultAcsClient)1 EncryptRequest (com.aliyuncs.kms.model.v20160120.EncryptRequest)1 ResourceName (com.google.api.resourcenames.ResourceName)1 DecryptResponse (com.google.cloud.kms.v1.DecryptResponse)1 IOException (java.io.IOException)1 SecretKey (javax.crypto.SecretKey)1 RuntimeCryptoException (org.bouncycastle.crypto.RuntimeCryptoException)1