public synchronized String encrypt(String keyRingId, String keyId, String message) {

    CryptoKeyName keyName = CryptoKeyName.of(projectId, region, keyRingId, keyId);
    LOG.info("Encrypting given message using key {}.", keyName.toString());

    try (KeyManagementServiceClient client = clientFactory.getKMSClient()) {

      EncryptResponse response = client.encrypt(keyName, ByteString.copyFromUtf8(message));

      LOG.info("Successfully encrypted message.");
      return new String(
          Base64.getEncoder().encode(response.getCiphertext().toByteArray()),
          StandardCharsets.UTF_8);
    }
  }
@Test
  public void testEncryptShouldEncodeEncryptedMessageWithBase64() {
    String ciphertext = "ciphertext";
    EncryptResponse encryptedResponse =
        EncryptResponse.newBuilder().setCiphertext(ByteString.copyFromUtf8(ciphertext)).build();

    when(kmsClientFactory.getKMSClient()).thenReturn(serviceClient);
    when(serviceClient.encrypt(any(CryptoKeyName.class), any(ByteString.class)))
        .thenReturn(encryptedResponse);

    String encryptedMessage = testManager.encrypt(KEYRING_ID, KEY_ID, "test message");
    String actual =
        new String(
            Base64.getDecoder().decode(encryptedMessage.getBytes(StandardCharsets.UTF_8)),
            StandardCharsets.UTF_8);

    assertThat(actual).isEqualTo(ciphertext);
  }