Encrypts plaintext data using the specified encryption key and returns a base64-encoded envelope containing the ciphertext and metadata.
Request
The unique identifier of the encryption key to use. Key must be active.
The plaintext string to encrypt. Will be converted to UTF-8 bytes before encryption.
Response
Base64-encoded binary envelope containing the encrypted data and metadata. This envelope is self-describing and includes:
- Version (u8): Envelope format version (currently
1)
- Algorithm (u8): Encryption algorithm identifier
- Key ID (Uuid): Reference to the encryption key
- Nonce (bytes): Random nonce for AES-256-GCM
- Ciphertext (bytes): Encrypted payload
- Tag (bytes): Authentication tag for integrity verification
Example
KEY_ID="550e8400-e29b-41d4-a716-446655440000"
curl -X POST http://localhost:8080/v1/security/encrypt \
-H 'Content-Type: application/json' \
-d "{
\"key_id\": \"${KEY_ID}\",
\"input\": \"hello world\"
}"
{
"envelope": "AQEgVQ6EAOKbQdSnFkRmVUQAAAwAAABhYmNkZWZnaGlqa2wQAAAA8j+9X..."
}
The envelope is a deterministic binary format that can be safely stored or transmitted. It includes all information needed for decryption:
- Version byte: Format compatibility (must be
1)
- Algorithm byte: Crypto algorithm used
- Key ID: 16 bytes (UUID)
- Nonce length + nonce: Variable length (12 bytes for AES-256-GCM)
- Ciphertext length + ciphertext: Variable length
- Tag length + tag: Variable length (16 bytes for AES-256-GCM)
The envelope is then base64-encoded for safe transport in JSON.
Error Responses
Error message describing what went wrong
| Status Code | Error Type | Description |
|---|
| 200 | - | Encryption successful |
| 404 | KeyNotFound | The specified key_id does not exist |
| 400 | KeyInactive | The key exists but is inactive (use active key or rotate) |
| 400 | Serialization | Failed to serialize the envelope |
| 500 | Encryption | Cryptographic operation failed |
Example Error
{
"error": "key not found: 550e8400-e29b-41d4-a716-446655440000"
}
{
"error": "key is inactive: 550e8400-e29b-41d4-a716-446655440000"
}
Only active keys can be used for encryption. If you receive a KeyInactive error, you must rotate the key or use a different active key. This design ensures that rotated keys cannot be used for new encryption operations.
Algorithm Details
The platform uses AES-256-GCM as the default encryption algorithm:
- Mode: Galois/Counter Mode (AEAD)
- Key size: 256 bits (32 bytes)
- Nonce size: 96 bits (12 bytes)
- Tag size: 128 bits (16 bytes)
- Properties: Authenticated encryption with associated data
Next Steps