use of com.google.cloud.kms.v1.AsymmetricDecryptRequest in project gapic-generator-java by googleapis.
the class AsyncAsymmetricDecrypt method asyncAsymmetricDecrypt.
public static void asyncAsymmetricDecrypt() throws Exception {
// It may require modifications to work in your environment.
try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
AsymmetricDecryptRequest request = AsymmetricDecryptRequest.newBuilder().setName(CryptoKeyVersionName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]", "[CRYPTO_KEY_VERSION]").toString()).setCiphertext(ByteString.EMPTY).setCiphertextCrc32C(Int64Value.newBuilder().build()).build();
ApiFuture<AsymmetricDecryptResponse> future = keyManagementServiceClient.asymmetricDecryptCallable().futureCall(request);
// Do something.
AsymmetricDecryptResponse response = future.get();
}
}
use of com.google.cloud.kms.v1.AsymmetricDecryptRequest in project gapic-generator-java by googleapis.
the class SyncAsymmetricDecrypt method syncAsymmetricDecrypt.
public static void syncAsymmetricDecrypt() throws Exception {
// It may require modifications to work in your environment.
try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
AsymmetricDecryptRequest request = AsymmetricDecryptRequest.newBuilder().setName(CryptoKeyVersionName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]", "[CRYPTO_KEY_VERSION]").toString()).setCiphertext(ByteString.EMPTY).setCiphertextCrc32C(Int64Value.newBuilder().build()).build();
AsymmetricDecryptResponse response = keyManagementServiceClient.asymmetricDecrypt(request);
}
}
use of com.google.cloud.kms.v1.AsymmetricDecryptRequest in project java-docs-samples by GoogleCloudPlatform.
the class DecryptAsymmetric method decryptAsymmetric.
// Decrypt data that was encrypted using the public key component of the given
// key version.
public void decryptAsymmetric(String projectId, String locationId, String keyRingId, String keyId, String keyVersionId, byte[] ciphertext) 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.
CryptoKeyVersionName keyVersionName = CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);
// Optional, but recommended: compute ciphertext's CRC32C. See helpers below.
long ciphertextCrc32c = getCrc32cAsLong(ciphertext);
// Decrypt the ciphertext.
AsymmetricDecryptRequest request = AsymmetricDecryptRequest.newBuilder().setName(keyVersionName.toString()).setCiphertext(ByteString.copyFrom(ciphertext)).setCiphertextCrc32C(Int64Value.newBuilder().setValue(ciphertextCrc32c).build()).build();
AsymmetricDecryptResponse response = client.asymmetricDecrypt(request);
// https://cloud.google.com/kms/docs/data-integrity-guidelines
if (!response.getVerifiedCiphertextCrc32C()) {
throw new IOException("AsymmetricDecrypt: request to server corrupted");
}
if (!crcMatches(response.getPlaintextCrc32C().getValue(), response.getPlaintext().toByteArray())) {
throw new IOException("AsymmetricDecrypt: response from server corrupted");
}
System.out.printf("Plaintext: %s%n", response.getPlaintext().toStringUtf8());
}
}
Aggregations