| Bytes | Field | Value |
| 1 | Version | 1 |
| 1 | Mode | 1 GCM, 2 CBC, 3 CTR |
| 1 | Key type | 0 raw key, 1 passphrase |
| 4 | Iterations | PBKDF2 rounds, big-endian. Passphrase messages only. |
| 16 | Salt | Random. Passphrase messages only. |
| 12 or 16 | IV | 12-byte nonce for GCM; 16 bytes for CBC and CTR |
| n | Ciphertext | CBC adds PKCS#7 padding. GCM appends a 16-byte tag. |
GCM authenticates every byte before the IV as additional data. CTR counts with the full 128-bit block, the same as OpenSSL. Passphrases are encoded as UTF-8 exactly as typed. Here is a GCM passphrase message decrypted in Python with the cryptography package:
import base64, struct
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
def decrypt(message: str, passphrase: str) -> str:
data = base64.b64decode(message)
assert data[:3] == bytes([1, 1, 1]), "expects version 1, GCM, passphrase"
iterations = struct.unpack(">I", data[3:7])[0]
salt, nonce, body = data[7:23], data[23:35], data[35:]
key = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32,
salt=salt, iterations=iterations).derive(passphrase.encode())
return AESGCM(key).decrypt(nonce, body, data[:23]).decode()