China's SM2 and SM3: The Two Locks Every Gateway Request Must Pass
Western developers integrating with Chinese platforms (Alipay, WeChat Pay, government services) must handle SM2 and SM3 or fail at the gateway. Mixing up encryption and signing, or signing plaintext instead of ciphertext, blocks the request. Knowing the correct order and the serialization details saves hours of joint-debugging.
Chinese government and enterprise platforms require SM2 encryption and SM3 signing on every API request, but integration failures are common. SM2 is asymmetric encryption for confidentiality, SM3 is a one-way hash for integrity. They act like a safe and a tamper-evident seal: SM2's public key locks data, its private key unlocks; SM3 produces a fixed 64-character digest that the gateway recomputes to detect any alteration.
Common integration mistakes include signing plaintext instead of ciphertext, sorting parameters incorrectly, including null values or headers in the signature, mishandling the '04' prefix, and using the outdated C1C2C3 ciphertext order instead of C1C3C2. The correct sequence is non-negotiable: encrypt the payload first, then sign the resulting ciphertext. Gateways verify the signature before attempting decryption, so any other order fails immediately at verification. The article closes with a pragmatic debugging checklist for both "signature verification failed" and "decryption error" scenarios.
Developers routinely confuse encryption with signing, but the two solve different problems: confidentiality versus integrity. That conceptual confusion, not the cryptographic math, causes most integration failures.
The 'encrypt then sign' order is not a style choice; gateways recompute the hash from the ciphertext and reject the request if the signature does not match the recomputed digest, so signing first is structurally impossible.
SM2 and SM3 follow international ECC and hash conventions, but the serialization details (the '04' prefix, C1C3C2 ordering) are where implementations diverge, making interoperability the real integration challenge.