Search in sources :

Example 1 with DecryptRequest

use of com.google.cloud.kms.v1.DecryptRequest in project gapic-generator-java by googleapis.

the class AsyncDecrypt method asyncDecrypt.

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

Example 2 with DecryptRequest

use of com.google.cloud.kms.v1.DecryptRequest in project gapic-generator-java by googleapis.

the class SyncDecrypt method syncDecrypt.

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

Example 3 with DecryptRequest

use of com.google.cloud.kms.v1.DecryptRequest in project aliyun-oss-java-sdk by aliyun.

the class KmsEncryptionMaterials method decryptCipherBlob.

/**
 * Decrypt the cipherBlob to palin text.
 */
private DecryptResponse decryptCipherBlob(KmsClientSuite kmsClientSuite, String cipherBlob) throws ClientException {
    final DefaultAcsClient kmsClient = createKmsClient(kmsClientSuite.region, kmsClientSuite.credentialsProvider);
    final DecryptRequest decReq = new DecryptRequest();
    decReq.setSysProtocol(ProtocolType.HTTPS);
    decReq.setAcceptFormat(FormatType.JSON);
    decReq.setSysMethod(MethodType.POST);
    decReq.setCiphertextBlob(cipherBlob);
    final DecryptResponse decResponse;
    try {
        decResponse = kmsClient.getAcsResponse(decReq);
    } catch (Exception e) {
        throw new ClientException("The kms client decrypt data faild." + e.getMessage(), e);
    }
    return decResponse;
}
Also used : DecryptResponse(com.aliyuncs.kms.model.v20160120.DecryptResponse) DefaultAcsClient(com.aliyuncs.DefaultAcsClient) ClientException(com.aliyun.oss.ClientException) DecryptRequest(com.aliyuncs.kms.model.v20160120.DecryptRequest) ClientException(com.aliyun.oss.ClientException)

Example 4 with DecryptRequest

use of com.google.cloud.kms.v1.DecryptRequest in project java-docs-samples by GoogleCloudPlatform.

the class DecryptSymmetric method decryptSymmetric.

// Decrypt data that was encrypted using a symmetric key.
public void decryptSymmetric(String projectId, String locationId, String keyRingId, String keyId, byte[] ciphertext) throws IOException {
    // safely clean up any remaining background resources.
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
        // Build the key version from the project, location, key ring, and key.
        CryptoKeyName keyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
        // Optional, but recommended: compute ciphertext's CRC32C. See helpers below.
        long ciphertextCrc32c = getCrc32cAsLong(ciphertext);
        // Decrypt the ciphertext.
        DecryptRequest request = DecryptRequest.newBuilder().setName(keyName.toString()).setCiphertext(ByteString.copyFrom(ciphertext)).setCiphertextCrc32C(Int64Value.newBuilder().setValue(ciphertextCrc32c).build()).build();
        DecryptResponse response = client.decrypt(request);
        // https://cloud.google.com/kms/docs/data-integrity-guidelines
        if (!crcMatches(response.getPlaintextCrc32C().getValue(), response.getPlaintext().toByteArray())) {
            throw new IOException("Decrypt: response from server corrupted");
        }
        System.out.printf("Plaintext: %s%n", response.getPlaintext().toStringUtf8());
    }
}
Also used : DecryptResponse(com.google.cloud.kms.v1.DecryptResponse) CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) IOException(java.io.IOException) DecryptRequest(com.google.cloud.kms.v1.DecryptRequest) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 5 with DecryptRequest

use of com.google.cloud.kms.v1.DecryptRequest in project spring-cloud-gcp by GoogleCloudPlatform.

the class KmsTemplate method decryptBytes.

@Override
public byte[] decryptBytes(String cryptoKey, byte[] cipherText) {
    CryptoKeyName cryptoKeyName = KmsPropertyUtils.getCryptoKeyName(cryptoKey, projectIdProvider);
    ByteString encryptedByteString = ByteString.copyFrom(cipherText);
    long crc32c = longCrc32c(encryptedByteString);
    DecryptRequest request = DecryptRequest.newBuilder().setName(cryptoKeyName.toString()).setCiphertext(encryptedByteString).setCiphertextCrc32C(Int64Value.newBuilder().setValue(crc32c).build()).build();
    DecryptResponse response = client.decrypt(request);
    assertCrcMatch(response);
    return response.getPlaintext().toByteArray();
}
Also used : DecryptResponse(com.google.cloud.kms.v1.DecryptResponse) CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) ByteString(com.google.protobuf.ByteString) DecryptRequest(com.google.cloud.kms.v1.DecryptRequest)

Aggregations

DecryptRequest (com.google.cloud.kms.v1.DecryptRequest)4 DecryptResponse (com.google.cloud.kms.v1.DecryptResponse)4 KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)3 CryptoKeyName (com.google.cloud.kms.v1.CryptoKeyName)2 ClientException (com.aliyun.oss.ClientException)1 DefaultAcsClient (com.aliyuncs.DefaultAcsClient)1 DecryptRequest (com.aliyuncs.kms.model.v20160120.DecryptRequest)1 DecryptResponse (com.aliyuncs.kms.model.v20160120.DecryptResponse)1 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1