Search in sources :

Example 11 with EncryptResponse

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

the class EncryptSymmetric method encryptSymmetric.

// Encrypt data with a given key.
public void encryptSymmetric(String projectId, String locationId, String keyRingId, String keyId, String plaintext) throws IOException {
    // safely clean up any remaining background resources.
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
        // Build the key version name from the project, location, key ring, key,
        // and key version.
        CryptoKeyName keyVersionName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
        // Encrypt the plaintext.
        EncryptResponse response = client.encrypt(keyVersionName, ByteString.copyFromUtf8(plaintext));
        System.out.printf("Ciphertext: %s%n", response.getCiphertext().toStringUtf8());
    }
}
Also used : EncryptResponse(com.google.cloud.kms.v1.EncryptResponse) CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 12 with EncryptResponse

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

the class KmsTemplate method encryptBytes.

@Override
public byte[] encryptBytes(String cryptoKey, byte[] bytes) {
    CryptoKeyName cryptoKeyName = KmsPropertyUtils.getCryptoKeyName(cryptoKey, projectIdProvider);
    long crc32c = longCrc32c(bytes);
    EncryptRequest request = EncryptRequest.newBuilder().setName(cryptoKeyName.toString()).setPlaintext(ByteString.copyFrom(bytes)).setPlaintextCrc32C(Int64Value.newBuilder().setValue(crc32c).build()).build();
    EncryptResponse response = client.encrypt(request);
    assertCrcMatch(response);
    return response.getCiphertext().toByteArray();
}
Also used : EncryptResponse(com.google.cloud.kms.v1.EncryptResponse) CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) EncryptRequest(com.google.cloud.kms.v1.EncryptRequest)

Example 13 with EncryptResponse

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

the class AsyncEncrypt method asyncEncrypt.

public static void asyncEncrypt() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        EncryptRequest request = EncryptRequest.newBuilder().setName(CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]").toString()).setPlaintext(ByteString.EMPTY).setAdditionalAuthenticatedData(ByteString.EMPTY).setPlaintextCrc32C(Int64Value.newBuilder().build()).setAdditionalAuthenticatedDataCrc32C(Int64Value.newBuilder().build()).build();
        ApiFuture<EncryptResponse> future = keyManagementServiceClient.encryptCallable().futureCall(request);
        // Do something.
        EncryptResponse response = future.get();
    }
}
Also used : EncryptResponse(com.google.cloud.kms.v1.EncryptResponse) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient) EncryptRequest(com.google.cloud.kms.v1.EncryptRequest)

Example 14 with EncryptResponse

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

the class SyncEncrypt method syncEncrypt.

public static void syncEncrypt() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        EncryptRequest request = EncryptRequest.newBuilder().setName(CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]").toString()).setPlaintext(ByteString.EMPTY).setAdditionalAuthenticatedData(ByteString.EMPTY).setPlaintextCrc32C(Int64Value.newBuilder().build()).setAdditionalAuthenticatedDataCrc32C(Int64Value.newBuilder().build()).build();
        EncryptResponse response = keyManagementServiceClient.encrypt(request);
    }
}
Also used : EncryptResponse(com.google.cloud.kms.v1.EncryptResponse) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient) EncryptRequest(com.google.cloud.kms.v1.EncryptRequest)

Example 15 with EncryptResponse

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

the class SyncEncryptStringBytestring method syncEncryptStringBytestring.

public static void syncEncryptStringBytestring() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        String name = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]").toString();
        ByteString plaintext = ByteString.EMPTY;
        EncryptResponse response = keyManagementServiceClient.encrypt(name, plaintext);
    }
}
Also used : EncryptResponse(com.google.cloud.kms.v1.EncryptResponse) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

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