Encryption
Client Request and Response Payload Encryption
When encryption is enabled, the client must encrypt the entire request payload before sending it to the API and decrypt the encrypted response payload upon receiving the API response.
The encryption mechanism uses AWS KMS-managed key management with key rotation support to securely manage encryption keys. The client uses the configured public key to protect the AES data encryption key (DEK), while the backend uses AWS KMS to recover the DEK and process the encrypted payload.
The client integration must support the request and response encryption format, including the nonce and authentication tag required for secure AES-GCM decryption.
Client Request Encryption
The client must encrypt the complete transaction request before sending it to the API.
Step 1: Configure the server's RSA public key
The client uses the public key provided by the server.
const serverPublicKey = `-----BEGIN PUBLIC KEY----- RSA_PUBLIC_KEY -----END PUBLIC KEY-----`;Step 2: Generate an AES-256 key
The client generates a new random AES key for every request.
const aesKey = await crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256
},
true,
["encrypt", "decrypt"]
);Here:
AES-GCM: Encryption algorithm.256: Key size in bits.true: Allows exporting the raw key.encrypt,decrypt: Permitted operations.
The generated AES key is unique for that request.
The client then exports the key:
Why export it?
Because RSA needs the raw AES key bytes to encrypt it using the public key.
Step 3: Generate a nonce
Nonce means a number used once.
AES-GCM requires a unique nonce for each encryption under a given key.
Here, the nonce is 12 bytes (96 bits).
Think of it as an additional random value that makes the encryption operation unique.
Important points:
Nonce is not secret.
It is sent along with the ciphertext.
It is required for decryption.
Never reuse the same nonce with the same AES key.
Step 4: Encrypt the complete JSON payload
First, convert the transaction JSON into bytes:
Step 5: Encrypt the AES key using RSA
At this point, the client has:
Original AES key.
Encrypted transaction payload.
Nonce.
But how does the backend obtain the AES key?
The client encrypts the AES key using the server's RSA public key.
First, convert the PEM public key into bytes:
Then import the RSA public key:
Now encrypt the AES key:
What happens?
The client can now safely send the encrypted AES key to the backend.
Step 6: Prepare the Encrypted Request
After encrypting the AES encryption key using the server's RSA public key and encrypting the JSON payload using AES-256-GCM, convert the resulting binary data into Base64 strings.
This is required because the API accepts JSON data, which cannot directly carry binary data.
1. Define the Base64 conversion function
2. Convert the encrypted data into Base64
Where:
encryptedKey: AES encryption key encrypted using the server's RSA public key.nonce: 12-byte nonce generated for AES-256-GCM encryption.ciphertext: Encrypted JSON payload, including the 128-bit GCM authentication tag appended to the ciphertext.
3. Prepare the encrypted request object
Create the request object using the Base64-encoded values.
4. Final API request payload
The request sent to the backend will have the following JSON structure:
keyVersion: Identifies the KMS key version corresponding to the public key used to encrypt the AES DEK.
encryptedKey: AES key encrypted using the server's RSA public key.
nonce: Nonce used for AES-GCM encryption.
ciphertext: Encrypted complete JSON payload, including the AES-GCM authentication tag.
Key rotation: The client must set keyVersion to the version associated with the public key it uses. During rotation, the backend can support both configured key versions and select the appropriate KMS key based on this field. Once the client migration is complete, the previous key version can be retired.
5. Send the encrypted request to the server
JavaScript Encryption Example
Client Response Decryption
The client now receives the encrypted response.
It must decrypt it before accessing the risk result.
Step 1: Retain the recovered AES key
After decrypting the incoming request, the backend already has the AES key.
It should retain it temporarily until the response has been encrypted.
Step 2: Read the encrypted response
Extract the response nonce, ciphertext, and authentication tag according to the agreed response format. These values are required to decrypt and authenticate the response.
Step 3: Prepare the encrypted bytes
Convert the Base64 values into bytes and arrange the ciphertext and authentication tag in the format expected by the client's cryptography library.
The Web Crypto API expects the AES-GCM authentication tag to be appended to the ciphertext. If the API returns the tag separately, the client must combine them before decryption.
Step 4: Decrypt the response
Use AES-256-GCM with the retained AES key and the response nonce.
This recovers the original response and verifies its integrity. Decryption must fail if authentication fails; the client must not use an unauthenticated response.
Step 5: Convert the response into JSON
Convert the decrypted bytes into text and parse the JSON.
The client can now access the transaction response as a normal JSON object for its application logic.
Sample API Request
Sample API Response
Error Response
If the encrypted request payload is missing, invalid, or cannot be decrypted using the configured AWS KMS key and AES key, the API returns an error response.
Possible causes:
Incorrect RSA public key used to encrypt the AES key.
AWS KMS key configuration or decryption failure.
Invalid or mismatched AES key.
Incorrect nonce or modified ciphertext.
Invalid AES-GCM authentication tag.
Plaintext payload sent instead of the encrypted request envelope.
Last updated