AES / SM4 Encryption
Symmetric encryption using AES (128/192/256) in CBC, GCM, ECB modes and SM4-CBC Chinese national standard. All computation in-browser via Web Crypto API.
AES encryption uses the browser's built-in Web Crypto API (hardware-accelerated where available). SM4 uses a pure JavaScript implementation per GMT 0002-2012.
Encryption output is Base64-encoded. For GCM mode, the 16-byte authentication tag is appended to the ciphertext before Base64 encoding. ECB mode does not use an IV.
How to Use
Encrypt: Select algorithm and key size, enter a hex key and IV (or click "Random Key + IV"), type plaintext, click Encrypt. The output is Base64-encoded ciphertext.
Decrypt: Paste Base64-encoded ciphertext into the input field, enter the same key and IV used for encryption, click Decrypt.
Key Format: AES-128 = 32 hex chars, AES-192 = 48 hex chars, AES-256 = 64 hex chars. IV = 32 hex chars for CBC/GCM. SM4 key = 32 hex chars.
Example Use Cases
- Secure messaging — Encrypt messages before transport using AES-256-GCM for authenticated encryption
- Configuration encryption — Encrypt config files or API keys at rest
- Chinese compliance — Use SM4-CBC where Chinese national cryptographic standards are required
- CTF challenges — Quickly test decryption with known keys during security competitions
- Interoperability testing — Verify your backend encryption matches using the same key/IV/plaintext
Understanding Symmetric Encryption
Symmetric encryption uses a single secret key for both encryption and decryption. Unlike asymmetric cryptography (RSA, ECC), where each party has a key pair, symmetric ciphers are fast and can process gigabytes of data per second on modern hardware. AES (Advanced Encryption Standard, FIPS 197) is the global standard, adopted by NIST in 2001 and mandated by the US government. SM4 (GMT 0002-2012) is China's national standard, required for cryptographic compliance in Chinese financial and government systems.
Block Cipher Modes of Operation
AES and SM4 are block ciphers — they encrypt 16-byte blocks at a time. To encrypt data longer than one block, you need a mode of operation. ECB (Electronic Codebook) is the simplest: each block is encrypted independently. This is catastrophically insecure because identical plaintext blocks produce identical ciphertext blocks, leaking patterns. The classic example is the ECB penguin — encrypting an image of a penguin with ECB still reveals the penguin's shape in the ciphertext. ECB must never be used for multi-block data.
CBC (Cipher Block Chaining) XORs each plaintext block with the previous ciphertext block before encryption. The first block uses an Initialization Vector (IV). CBC provides confidentiality but not integrity — an attacker can flip bits in the ciphertext to produce predictable changes in the plaintext (bit-flipping attack). CBC requires an unpredictable IV, ideally random, for each encryption.
GCM (Galois/Counter Mode) is the modern gold standard. It combines CTR mode encryption with a Galois-field MAC for authenticated encryption — it guarantees both confidentiality and integrity. If a single bit of the ciphertext is modified, decryption fails. GCM uses a 12-byte nonce (not a full 16-byte IV) and produces a 16-byte authentication tag appended to the ciphertext. NIST SP 800-38D recommends GCM for all new systems. TLS 1.3 mandates AEAD modes (GCM or ChaCha20-Poly1305) and explicitly forbids CBC.
AES Key Sizes: 128 vs 192 vs 256
AES supports three key sizes: 128, 192, and 256 bits. All three use a 128-bit block size (16 bytes). The number of encryption rounds increases with key size: 10 rounds for AES-128, 12 for AES-192, and 14 for AES-256. NIST rates AES-128 at 128 bits of security (secure through 2030+), AES-192 at 192 bits, and AES-256 at 256 bits. In practice, AES-128 is sufficient for nearly all applications — there is no known practical attack that breaks AES-128. AES-256 is recommended for top-secret government data (NSA Suite B) and for long-term protection (50+ years). The performance difference between AES-128 and AES-256 is about 40% on software without hardware acceleration.
SM4: The Chinese Standard
SM4 was published by the State Cryptography Administration of China in 2012 as part of the GM/T 0002 standard. It uses a 128-bit key with a 128-bit block, 32 rounds of unkeyed round transformations. SM4-CBC is the most common mode, though SM4-GCM and SM4-CTR are also defined. In Chinese financial systems (UnionPay, Alipay domestic transfers), SM4 is mandatory for data at rest. The GM/T standard also defines SM4 key wrapping and SM4 in TLS (specified in RFC 8998 for TLS 1.3). If you work with Chinese smart cards, EMVCo compliance for China, or government IT systems, SM4 is non-optional.
IV and Nonce: Critical Security Parameters
The Initialization Vector (IV) in CBC and the nonce in GCM are not secret — they can be transmitted in plaintext alongside the ciphertext. But they have strict requirements. In CBC, the IV must be unpredictable (random) for each encryption; reusing an IV with the same key enables chosen-plaintext attacks. In GCM, the nonce must be unique for each encryption under the same key — reusing a nonce catastrophically breaks GCM, allowing the attacker to recover the authentication key and forge messages. A 12-byte random nonce gives a collision probability of less than one in 2^48 after 2^32 encryptions, which is safe for most systems. For high-volume systems, a counter-based nonce is more robust.
Key Management Best Practices
Never hardcode encryption keys in source code. Use a key management service (AWS KMS, Azure Key Vault, HashiCorp Vault) or derive keys at runtime from a master key using a KDF (HKDF, PBKDF2). Rotate keys periodically — annually at minimum. Never reuse the same key across different encryption algorithms or modes. When encrypting large data, use envelope encryption: encrypt the data with a random data key (AES-GCM), then encrypt the data key with a master key stored in a KMS. This pattern allows key rotation without re-encrypting all data.