This document provides information about creating and using a key for asymmetric encryption using an RSA key. If you want to use asymmetric keys for creating and validating signatures, see Creating and validating digital signatures. If you want to use symmetric keys for encryption and decryption, see Encrypting and decrypting data.
Asymmetric encryption uses the public key portion of the asymmetric key and decryption uses the private key portion of the key. Cloud Key Management Service provides functionality to retrieve the public key and functionality to decrypt ciphertext that was encrypted with the public key. Cloud KMS does not allow direct access to the private key.
Before you begin
This document provides examples that run at the command line. To simplify using the examples, use Cloud Shell. The encryption example uses OpenSSL, which is pre-installed on Cloud Shell.
Create an asymmetric key with key purpose of
ASYMMETRIC_DECRYPT. To see which algorithms are supported for key purposeASYMMETRIC_DECRYPT, see Asymmetric encryption algorithms. You can't follow this procedure with a key with purpose ofASYMMETRIC_SIGN.If you are going to use the command line, install OpenSSL if you don't already have it. If you use Cloud Shell, then OpenSSL is already installed.
- macOS users: The version of OpenSSL installed on macOS doesn't support the flags that are used to decrypt data in this topic. To follow these steps on macOS, install OpenSSL from Homebrew.
Control access to the key
For a user or service that will retrieve the public key, grant the
cloudkms.cryptoKeyVersions.viewPublicKeypermission on the asymmetric key. The public key is required for encrypting data.For a user or service that will decrypt data that was encrypted with the public key, grant the
cloudkms.cryptoKeyVersions.useToDecryptpermission on the asymmetric key.
For more information about permissions and roles in Cloud KMS, see Permissions and Roles.
Encrypt data
To encrypt data using an asymmetric encryption key, retrieve the public key and use the public key to encrypt the data.
gcloud
This sample requires OpenSSL to be installed on your local system.
Download public key
Download the public key:
gcloud kms keys versions get-public-key KEY_VERSION \
--key KEY_NAME \
--keyring KEY_RING \
--location LOCATION \
--output-file PUBLIC_KEY_PATH
Replace the following:
KEY_VERSION: the key version of the asymmetric key pair that you want to download the public key for.KEY_NAME: the name of the key.KEY_RING: the key ring that contains the key.LOCATION: the location of the key ring.PUBLIC_KEY_PATH: the path where you want to save the public key.
Encrypt data
Encrypt data using the public key you just downloaded and save the output to a file:
openssl pkeyutl -in INPUT_PATH \
-encrypt \
-pubin \
-inkey PUBLIC_KEY_PATH \
-pkeyopt rsa_padding_mode:oaep \
-pkeyopt rsa_oaep_md:HASH_SIZE \
-pkeyopt rsa_mgf1_md:HASH_SIZE \
> OUTPUT_PATH
Replace the following:
INPUT_PATH: the path to the plaintext data that you want to encrypt.PUBLIC_KEY_PATH: the path to the public key.HASH_SIZE: the size of the hash—for example,256for SHA256.OUTPUT_PATH: the path where you want to save the encrypted data.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
Decrypt data
Use Cloud KMS to perform the decryption.
gcloud
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
gcloud kms asymmetric-decrypt \
--version KEY_VERSION \
--key KEY_NAME \
--keyring KEY_RING \
--location LOCATION \
--ciphertext-file INPUT_PATH \
--plaintext-file OUTPUT_PATH
Replace the following:
KEY_VERSION: the key version of the asymmetric key pair that was used to encrypt the data. You can omit the--versionflag to detect the version automatically.KEY_NAME: the name of the key that was used to encrypt the data.KEY_RING: the name of the key ring that contains the key.LOCATION: the location of the key ring.INPUT_PATH: the path to the file that contains the ciphertext.OUTPUT_PATH: the path to the file that will contain the plaintext.
For information on all flags and possible values, run the command with the
--help flag.
To display the contents of the decrypted file, open it in a text editor or use
the cat command to view it in a terminal.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
API
These examples use curl as an HTTP client to demonstrate using the API. For more information about access control, see Accessing the Cloud KMS API.
Use the
CryptoKeyVersions.asymmetricDecrypt
method.
Troubleshooting
incorrect key purpose: ASYMMETRIC_SIGN
You can only decrypt data with a key with key purpose ASYMMETRIC_DECRYPT.
invalid parameter when decrypting on macOS
The version of OpenSSL installed on macOS doesn't support the flags that are used to decrypt data in this topic. To follow these steps on macOS, install OpenSSL from Homebrew.
data too large for key size
The maximum payload size for RSA decryption depends on the key size and padding algorithm. All RSA encryption formats used by Cloud KMS use OAEP, standardized in RFC 2437. As a quick reference, the following algorithms support the following maximum payload sizes.
| Algorithm | Parameters | Maximum message length (bytes) |
|---|---|---|
| RSA_DECRYPT_OAEP_2048_SHA256 | k = 256; hLen = 32; | 190 |
| RSA_DECRYPT_OAEP_3072_SHA256 | k = 384; hLen = 32; | 318 |
| RSA_DECRYPT_OAEP_4096_SHA256 | k = 512; hLen = 32; | 446 |
| RSA_DECRYPT_OAEP_4096_SHA512 | k = 512; hLen = 64; | 382 |
Asymmetric encryption is not recommended for messages of varying lengths that may be larger than these limits. Consider using hybrid encryption instead. Tink is a cryptographic library that uses this approach.