SM4 Encrypt/Decrypt Online - Chinese Block Cipher (ECB/CBC)
Online SM4 block cipher tool: ECB/CBC modes, PKCS#7 padding, random key generation with visibility toggle. 128-bit blocks per GB/T 32907, the Chinese national alternative to AES-128. Fully local, nothing uploaded.
TL;DR: Online SM4 block cipher: ECB/CBC modes, key generation, the Chinese alternative to AES-128.
For local tools, open DevTools → Network and search your input — it should not appear in requests.
Usage Guide
SM4 is the block cipher of the Chinese national cryptography family (the analogue of AES), with 128-bit keys and blocks. Data encryption in national-crypto retrofits usually specifies it. This tool encrypts and decrypts locally in the browser — plaintext and key never leave the machine. Built for fast ciphertext verification during integration.
1. How to use
- Enter plaintext and the SM4 key (16 bytes, commonly 32 hex chars; 0x/spaces OK).
- Pick the mode (ECB / CBC; CBC requires a 16-byte IV — missing IV errors out).
- Pick ciphertext encoding (hex or Base64) to match the peer, then encrypt / decrypt.
2. Debugging order when outputs mismatch
- Key first: length (16 bytes), format (hex vs raw utf-8), stray spaces / 0x.
- Mode and IV second: ECB has no IV; CBC requires the same 16-byte IV on both ends (this page rejects a missing IV).
- Encodings last: this page can emit hex or Base64 — all three parties must agree.
3. A note on modes
ECB leaks structure (equal plaintext blocks produce equal ciphertext blocks) and belongs only in connectivity tests; real traffic should use CBC or better with a fresh random IV each time. Mismatched ciphertexts during integration are the norm — check key, then mode/IV, then encoding; that order resolves most failures. SM4 and AES-128 are comparable in strength; the choice is compliance-driven.
4. Where SM4 is mandatory (and AES still works)
One criterion decides: what the counterparty or the regulator demands. Chinese compliance regimes (MLPS, commercial cryptography assessment, sector rules for government and finance) name the national algorithm family explicitly — data encryption there means SM4 (GB/T 32907), and AES-128 stays unusable no matter how strong it is. Outside mandated contexts, AES remains the lowest-common-denominator for mixed international systems; forcing SM4 onto a link nobody requires it for only shrinks your client ecosystem. The common grey-zone pattern — one product shipping both to China and elsewhere — is algorithm configurability: same code, SM4 or AES selected per deployment, with an isomorphic key-management layer.
5. Interop checklist for an SM4 endpoint
- Key format: 16 bytes — is the value you got 32 hex chars, or base64 (24 chars, decodes to 18 bytes — note: not 16)? Confirm before parsing.
- Mode and IV: ECB has no IV; CBC needs a 16-byte IV identical on both ends, and some implementations reuse a fixed IV (effectively ECB).
- Padding: PKCS#7 is the default convention, but some specs require the sender to pad manually to a 16-byte boundary (padding=None) — one byte off and everything mismatches.
- Ciphertext encoding: hex (32 chars per block) or base64 — visually obvious, but treat it as interface contract, not a guess.
- First integration run: fixed key + fixed plaintext through a standard test vector, encrypted on both ends, ciphertexts compared. If the vector mismatches, the problem is in the format agreement layer — never the algorithm.
6. Which standard number applies: GM/T 0002, GB/T 32907, RFC 8998
SM4’s identity evolution is the canonical path for national algorithms: published in 2006, named as the industry standard GM/T 0002-2012, elevated to the national standard GB/T 32907-2016, and included in the international standard ISO/IEC 18033-3:2017 (alongside AES in the block-cipher appendix) in 2017. All three numbers describe the same algorithm. On the protocol side, RFC 8998 (2021) defines ShangMi TLS 1.3 suites — SM2 in the handshake, SM3 in key derivation, SM4-GCM/CCM for records. What you verify here is algorithm-layer ECB/CBC (keys and encodings), not ShangMi record-layer GCM, and not a TLCP handshake. Full number table: [standard-number decoder](/en/blog/sm-standard-numbers-explained); selection: [Chinese cryptography guide](/en/blog/guomi-algorithms-guide). Key exchange and signatures: [SM2](/en/tools/sm2-encrypt); digests: [SM3](/en/tools/sm3-hash).
7. Mode boundary on this page: ECB/CBC, not GCM
The tool supports ECB and CBC (CBC requires a 16-byte IV) with PKCS#7 padding and hex/Base64 ciphertext encoding. RFC 8998 suites TLS_SM4_GCM_SM3 / CCM and many "GM HTTPS" gateways encrypt records with authenticated GCM/CCM — those ciphertexts do not interoperate with CBC from this page. Use this page for application-layer SM4-CBC interop; for ShangMi TLS acceptance, inspect the negotiated suite in a handshake capture (the [HAR viewer](/en/tools/har-viewer) helps) instead of treating page ciphertext as suite evidence. The fuller interop checklist is in [SM2/SM3/SM4 in practice](/en/blog/sm-crypto-practical-guide).
FAQ
Same role: block ciphers. SM4 uses fixed 128-bit blocks and keys (comparable to AES-128); AES offers key-size options. The choice is dictated by your peer and compliance: MLPS/等保, crypto evaluation, and government-finance rules that name Guomi modules require SM4; without such requirements AES remains the common denominator, and products sold into both markets often make the algorithm configurable. Hardware-accelerated AES is faster; SM4 has acceleration in certified Chinese crypto hardware.
Prefer CBC with a fresh random IV for real data: ECB encrypts identical blocks identically, leaking patterns (the penguin classic), so keep it for connectivity tests. Also watch for peers that hard-code the IV — that is ECB in disguise. A missing CBC IV fails with a clear error here; the tool never silently uses an empty IV.
A 32-hex-character string for the 128-bit key (e.g. 0123456789abcdeffedcba9876543210). CBC also needs a 32-hex-character IV — a missing IV fails loudly. Use the generate button for keys from the browser's secure random source; a leading 0x or internal spaces are normalized before use. When interop fails, debug in order — key, then mode/IV, then encoding; that sequence clears most mismatches.
The page emits hex (default) or Base64; both ends and the peer must agree on one. With the same plaintext and key, the two encodings are just different spellings of the same bytes — feeding hex into a Base64 decrypt (or the reverse) fails. Padding is another contract to confirm: PKCS#7 is the default convention, but some implementations require manually padded 16-byte plaintext (padding=None), and one wrong byte breaks the whole message.
This page offers ECB/CBC + PKCS#7 for application-layer interop. RFC 8998 defines the ShangMi suites for TLS 1.3 — SM2 handshake, SM3 key derivation, SM4-GCM/CCM record layer — which does not interoperate with CBC here, so verify handshakes in a Guomi TLS stack. On standard numbers: SM4 was published as the industry standard GM/T 0002-2012, upgraded to the national standard GB/T 32907-2016, and entered ISO/IEC 18033-3:2017 — three numbers, one algorithm.
Encryption and decryption run entirely in your browser; plaintext and keys never leave your machine. The page targets learning and short-payload interop (a few KB) — for large files, chunk the data or use a dedicated crypto library, since very long inputs will jank the UI.
Related Tools
Related Articles
SM2/SM3/SM4 in Practice: Choosing, Integrating, and Debugging Chinese National Cryptography
A field guide to Chinese national crypto: SM4 vs SM3 vs SM2, plus the integration traps that dominate real work — C1C3C2 vs C1C2C3, hex vs Base64, CBC IVs — with browser-local tools for known-answer checks.
Chinese National Cryptography (SM2/SM3/SM4/SM9/ZUC): A Developer's Introduction
Introduction to China's commercial cryptography: what SM2, SM3, SM4, SM9 and ZUC each do, how they map to RSA/SHA/AES, a selection decision tree, TLCP vs RFC 8998 boundaries, compliance drivers, and what browser tools can and cannot verify.
GB/T 32907, GM/T 0004, RFC 8998: Decoding the Standard Numbers Behind SM2/SM3/SM4
A decoder ring for Chinese national crypto standard numbers: GM/T vs GB/T vintages, ISO/IEC adoption, RFC 8998 ShangMi suites versus the TLCP (GB/T 38636) track, and which number each project document should cite.