K018 / Symmetric ciphers (block and stream)
SM4
SM4 is a symmetric block cipher using 128-bit blocks and keys.
CRYPTOGRAPHY LAB / K018
Cryptography Lab · SM4
Run real computations and inspect intermediate states step by step.
Educational only. Never enter real passwords or secret keys. Computation stays in your browser; input and trace values are not sent to a server. Do not use this implementation in production.
SM4 (single block)
See the scope above for fixed examples, reduced parameters and supported variants. Standard operation names and formulas are retained.Output
Previous state
- Definition and overview
- SM4 is a symmetric block cipher using 128-bit blocks and keys. Source document ↗
- Classification and reversibility
- A symmetric block cipher that decrypts by applying round keys in reverse order. Source document ↗
- Key or secret requirements
- Requires a 128-bit secret symmetric key. Source document ↗
- Operating principle and structure
- Processes rounds using an unbalanced Feistel structure consisting of four 32-bit words. Source document ↗
- Key size, block size and parameters
- 128-bit block, 128-bit key, 32 rounds. Source document ↗
- Security strength and recommendation
- No practical attacks on all 32 rounds of SM4 are currently known. Source document ↗
- Known attacks and vulnerabilities
- Smart cards and cryptographic devices face side-channel threats, requiring countermeasures in implementation. Source document ↗
- Standards and certification
- Recognized in ISO/IEC 18033-3 and specified for TLS 1.3 in RFC 8998. Source document ↗
- Typical uses
- Primarily used for data encryption in Chinese commercial cryptography applications. Source document ↗
- Implementations, libraries and tools
- Multilingual implementation libraries such as SM4Kit are available. Source document ↗
- Decryption or decoding
- Decrypt by applying round keys in reverse order using the same SM4 operations. Source document ↗
- Advantages
- Rated similarly to AES-128 in security and efficiency. Source document ↗
- Limitations and cautions
- In ECB, identical plaintext blocks produce identical ciphertext blocks; unsuitable outside tests and legacy uses. Source document ↗
- Distinguishing encryption, encoding and hashing
- SM4 is reversible keyed encryption, not Base64 encoding or hashing. Source document ↗
Practical examples
Example
Prepare plaintext and a 128-bit secret key; produce ciphertext with SM4 and a safe mode such as GCM or CBC with IV. Recover plaintext using the same key, mode, IV/tag.
Procedure
Encryption: plaintext, padding/mode processing, SM4 encryption, output including IV/tag. Decryption: use key and IV/tag, remove padding, restore plaintext. Encoding/decoding is keyless representation conversion.
Scenario
In commercial systems with Chinese interoperability or regulatory requirements, use TLS 1.3 SM4 suites or storage encryption. Design key, IV and tag management; evaluate AES-GCM alternatives in general environments depending on interoperability and regulation.
Pitfalls and alternatives
Avoid ECB, IV reuse, hardcoded keys and log exposure. For integrity use AEAD such as GCM/CCM and vetted libraries. Distinguish encryption from Base64 and hashing.
References
- SM4: sizes, standards and code examples | NextSSL ↗
- SM4: China's national block cipher, from Wi-Fi to TLS 1.3 ↗
- draft-ribose-cfrg-sm4-04 ↗
- draft-guo-ipsecme-ikev2-using-shangmi-03 ↗
- SM4 — secure-crypto.org ↗
- packages/ts/src/crypto/sm4/index.ts ↗
- yxpil/SM4Kit ↗
- SM4 - 2025.2 English ↗
- SM4 — block cipher | sm-crypto-wasm ↗