Search in sources :

Example 1 with EncryptRequest

use of com.google.api.services.cloudkms.v1.model.EncryptRequest in project tink by google.

the class GcpKmsAead method encrypt.

@Override
public byte[] encrypt(final byte[] plaintext, final byte[] aad) throws GeneralSecurityException {
    try {
        EncryptRequest request = new EncryptRequest().encodePlaintext(plaintext).encodeAdditionalAuthenticatedData(aad);
        EncryptResponse response = this.kmsClient.projects().locations().keyRings().cryptoKeys().encrypt(this.kmsKeyUri, request).execute();
        return response.decodeCiphertext();
    } catch (IOException e) {
        throw new GeneralSecurityException("encryption failed", e);
    }
}
Also used : EncryptResponse(com.google.api.services.cloudkms.v1.model.EncryptResponse) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) EncryptRequest(com.google.api.services.cloudkms.v1.model.EncryptRequest)

Example 2 with EncryptRequest

use of com.google.api.services.cloudkms.v1.model.EncryptRequest in project java-docs-samples by GoogleCloudPlatform.

the class CryptFile method encrypt.

// [START kms_encrypt]
/**
 * Encrypts the given plaintext using the specified crypto key.
 */
public static byte[] encrypt(String projectId, String locationId, String keyRingId, String cryptoKeyId, byte[] plaintext) throws IOException {
    // The resource name of the cryptoKey
    String resourceName = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
    // Create the Cloud KMS client.
    CloudKMS kms = createAuthorizedClient();
    EncryptRequest request = new EncryptRequest().encodePlaintext(plaintext);
    EncryptResponse response = kms.projects().locations().keyRings().cryptoKeys().encrypt(resourceName, request).execute();
    return response.decodeCiphertext();
}
Also used : EncryptResponse(com.google.api.services.cloudkms.v1.model.EncryptResponse) CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) EncryptRequest(com.google.api.services.cloudkms.v1.model.EncryptRequest)

Aggregations

EncryptRequest (com.google.api.services.cloudkms.v1.model.EncryptRequest)2 EncryptResponse (com.google.api.services.cloudkms.v1.model.EncryptResponse)2 CloudKMS (com.google.api.services.cloudkms.v1.CloudKMS)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1