ChatGPT helped me write code for which there is no documentation

jagadish nallapaneni
4 min readFeb 12, 2023

--

At my work, I needed to use encryption with Google KMS. Our application is in C++ and all of google documentation on KMS was for every language other than C++. It was not very practical to write code in other languages just for this purpose. Google had a C++ client library in Github but the documentation provided with it is incomplete to say it nicely. I managed to get it to integrate to our build system after some difficulty but was looking for some concrete example to use for encryption specifically since there is nothing obvious unless you have already played with the mechanics of Google’s KMS. I did some searches and decided I will have to look at the C++ code implementing it and try it.

This is when I saw a youtube video about ChatGPT how someone is using it for devOps tasks to generate code and decided to try it to test chatGPT if it can actually generate the correct code.

My first Prompt to ChatGPT is

“Show me how to use google clouds Kms to encrypt some text”

To which ChatGPT responded with this

To use Google Cloud KMS (Key Management Service) to encrypt text, you need to follow these steps:

  1. Create a key ring and key: In the Google Cloud Console, navigate to the Cloud KMS page, then create a new key ring and key to be used for encryption.
  2. Enable the Cloud KMS API: In the Cloud Console, navigate to the APIs & Services page, then enable the Cloud KMS API.
  3. Set up a Google Cloud project and configure authentication: If you haven’t already, set up a Google Cloud project and configure authentication so that your local machine can access the Google Cloud API.
  4. Encrypt the plaintext: You can use the gcloud command-line tool to encrypt your plaintext. Use the following command, replacing [PROJECT_ID], [LOCATION], [KEY_RING_NAME], and [KEY_NAME] with the appropriate values for your project:
echo -n "YOUR_PLAINTEXT" | gcloud kms encrypt \
--plaintext-file=- \
--ciphertext-file=- \
--location=[LOCATION] \
--keyring=[KEY_RING_NAME] \
--key=[KEY_NAME] | base64

This will encrypt the plaintext and return the ciphertext in base64 encoding, which you can then store or use as needed.

  1. Decrypt the ciphertext: You can use the gcloud command-line tool to decrypt the ciphertext. Use the following command, replacing [PROJECT_ID], [LOCATION], [KEY_RING_NAME], and [KEY_NAME] with the appropriate values for your project

This will encrypt the plaintext and return the ciphertext in base64 encoding, which you can then store or use as needed.

echo "YOUR_CIPHERTEXT_IN_BASE64" | base64 --decode | gcloud kms decrypt \
--ciphertext-file=- \
--plaintext-file=- \
--location=[LOCATION] \
--keyring=[KEY_RING_NAME] \
--key=[KEY_NAME]

My next prompt to ChatGPT is:

“How do I do this in my c++ application”

You can use the Google Cloud C++ client library to encrypt and decrypt text in your C++ application. The following is an example of how you can use the client library to encrypt and decrypt text using Google Cloud KMS:

  1. Set up the development environment: You need to install the Google Cloud C++ client library and its dependencies, such as the Google Cloud SDK, and set up the Google Cloud project, key ring, and key.
  2. Encrypt the plaintext: Here’s an example of how to encrypt a plaintext string using the Google Cloud C++ client library:
#include "google/cloud/kms/v1/key_management_service_client.h"
#include "google/cloud/kms/v1/crypto_key.pb.h"
#include "google/cloud/kms/v1/encrypt_response.pb.h"
#include <iostream>

using google::cloud::kms::v1::KeyManagementServiceClient;
using google::cloud::kms::v1::EncryptRequest;
using google::cloud::kms::v1::EncryptResponse;

int main(int argc, char* argv[]) {
// Replace [PROJECT_ID], [LOCATION], [KEY_RING_NAME], and [KEY_NAME] with
// the appropriate values for your project.
std::string project_id = "[PROJECT_ID]";
std::string location = "[LOCATION]";
std::string key_ring_name = "[KEY_RING_NAME]";
std::string key_name = "[KEY_NAME]";

// Initialize the client.
auto client = KeyManagementServiceClient(
google::cloud::kms::v1::CreateKeyManagementServiceClient());

// Create the EncryptRequest.
EncryptRequest request;
request.set_name(
client.CryptoKeyName(project_id, location, key_ring_name, key_name));
request.set_plaintext("YOUR_PLAINTEXT");

// Call the encrypt method.
auto response = client.Encrypt(request);
std::cout << "Ciphertext: " << response.ciphertext() << std::endl;

return 0;
}

3. Decrypt the ciphertext: Here’s an example of how to decrypt a ciphertext string using the Google Cloud C++ client library:

#include "google/cloud/kms/v1/key_management_service_client.h"
#include "google/cloud/kms/v1/crypto_key.pb.h"
#include "google/cloud/kms/v1/decrypt_response.pb.h"
#include <iostream>

using google::cloud::kms::v1::KeyManagementServiceClient;
using google::cloud::kms::v1::DecryptRequest;
using google::cloud::kms::v1::DecryptResponse;

int main(int argc, char* argv[]) {
// Replace [PROJECT_ID], [LOCATION], [KEY_RING_NAME], and [KEY_NAME] with
// the appropriate values for your project.
std::string project_id = "[PROJECT_ID]";
std::

It seems to have stopped halfway either due to some kind of rate limit or something. So I asked ChatGPT

Seems you have stopped halfway

ChatGPT replied with

I apologize for that. Here’s the rest of the example to complete the code for decrypting a ciphertext:

  std::string location = "[LOCATION]";
std::string key_ring_name = "[KEY_RING_NAME]";
std::string key_name = "[KEY_NAME]";

// Initialize the client.
auto client = KeyManagementServiceClient(
google::cloud::kms::v1::CreateKeyManagementServiceClient());

// Create the DecryptRequest.
DecryptRequest request;
request.set_name(
client.CryptoKeyName(project_id, location, key_ring_name, key_name));
request.set_ciphertext("YOUR_CIPHERTEXT");

// Call the decrypt method.
auto response = client.Decrypt(request);
std::cout << "Plaintext: " << response.plaintext() << std::endl;

return 0;
}

Note that you need to replace [PROJECT_ID], [LOCATION], [KEY_RING_NAME], and [KEY_NAME] with the appropriate values for your Google Cloud project. Also, replace "YOUR_PLAINTEXT" with the plaintext you want to encrypt, and replace "YOUR_CIPHERTEXT" with the ciphertext you want to decrypt.

My observations:

  1. It seems generated working code.
  2. The explanation is excellent.
  3. My earlier assumption about ChatGPT was it is able to recall but it cannot create something is probably wrong.
  4. It does seem to understand the conversational/follow up aspect of my request and it is a great thing search engines currently lack.

--

--