[{"data":1,"prerenderedAt":601},["ShallowReactive",2],{"navigation_docs":3,"-ciphers-sm4":344,"-ciphers-sm4-surround":596},[4,36],{"title":5,"path":6,"stem":7,"children":8,"icon":35},"Guide","\u002Fguide","1.guide\u002F01.index",[9,11,15,19,23,27,31],{"title":10,"path":6,"stem":7},"Getting Started",{"title":12,"path":13,"stem":14},"Encode and decode","\u002Fguide\u002Ftransform","1.guide\u002F02.transform",{"title":16,"path":17,"stem":18},"Analysis","\u002Fguide\u002Fanalysis","1.guide\u002F03.analysis",{"title":20,"path":21,"stem":22},"CLI","\u002Fguide\u002Fcli","1.guide\u002F04.cli",{"title":24,"path":25,"stem":26},"Agents","\u002Fguide\u002Fagents","1.guide\u002F05.agents",{"title":28,"path":29,"stem":30},"Custom ciphers","\u002Fguide\u002Fcustom","1.guide\u002F06.custom",{"title":32,"path":33,"stem":34},"Playground","\u002Fguide\u002Fplayground","1.guide\u002F07.playground","i-lucide-book-open",{"title":37,"path":38,"stem":39,"children":40,"icon":343},"Ciphers","\u002Fciphers","2.ciphers\u002F00.index",[41,43,47,51,55,59,63,67,71,75,79,83,87,91,95,99,103,107,111,115,119,123,127,131,135,139,143,147,151,155,159,163,167,171,175,179,183,187,191,195,199,203,207,211,215,219,223,227,231,235,239,243,247,251,255,259,263,267,271,275,279,283,287,291,295,299,303,307,311,315,319,323,327,331,335,339],{"title":42,"path":38,"stem":39},"Overview",{"title":44,"path":45,"stem":46},"Classical","\u002Fciphers\u002Fclassical","2.ciphers\u002F01.classical",{"title":48,"path":49,"stem":50},"Caesar","\u002Fciphers\u002Fcaesar","2.ciphers\u002F02.caesar",{"title":52,"path":53,"stem":54},"ROT-13","\u002Fciphers\u002Frot13","2.ciphers\u002F03.rot13",{"title":56,"path":57,"stem":58},"ROT-47","\u002Fciphers\u002Frot47","2.ciphers\u002F04.rot47",{"title":60,"path":61,"stem":62},"Atbash","\u002Fciphers\u002Fatbash","2.ciphers\u002F05.atbash",{"title":64,"path":65,"stem":66},"Affine","\u002Fciphers\u002Faffine","2.ciphers\u002F06.affine",{"title":68,"path":69,"stem":70},"Substitution","\u002Fciphers\u002Fsubstitution","2.ciphers\u002F07.substitution",{"title":72,"path":73,"stem":74},"Vigenère","\u002Fciphers\u002Fvigenere","2.ciphers\u002F08.vigenere",{"title":76,"path":77,"stem":78},"Gronsfeld","\u002Fciphers\u002Fgronsfeld","2.ciphers\u002F09.gronsfeld",{"title":80,"path":81,"stem":82},"Beaufort","\u002Fciphers\u002Fbeaufort","2.ciphers\u002F10.beaufort",{"title":84,"path":85,"stem":86},"Porta","\u002Fciphers\u002Fporta","2.ciphers\u002F11.porta",{"title":88,"path":89,"stem":90},"Autokey","\u002Fciphers\u002Fautokey","2.ciphers\u002F12.autokey",{"title":92,"path":93,"stem":94},"Running key","\u002Fciphers\u002Frunning-key","2.ciphers\u002F13.running-key",{"title":96,"path":97,"stem":98},"Trithemius","\u002Fciphers\u002Ftrithemius","2.ciphers\u002F14.trithemius",{"title":100,"path":101,"stem":102},"Alberti","\u002Fciphers\u002Falberti","2.ciphers\u002F15.alberti",{"title":104,"path":105,"stem":106},"Quagmire I","\u002Fciphers\u002Fquagmire-1","2.ciphers\u002F16.quagmire-1",{"title":108,"path":109,"stem":110},"Quagmire II","\u002Fciphers\u002Fquagmire-2","2.ciphers\u002F17.quagmire-2",{"title":112,"path":113,"stem":114},"Quagmire III","\u002Fciphers\u002Fquagmire-3","2.ciphers\u002F18.quagmire-3",{"title":116,"path":117,"stem":118},"Quagmire IV","\u002Fciphers\u002Fquagmire-4","2.ciphers\u002F19.quagmire-4",{"title":120,"path":121,"stem":122},"Chaocipher","\u002Fciphers\u002Fchaocipher","2.ciphers\u002F20.chaocipher",{"title":124,"path":125,"stem":126},"Playfair","\u002Fciphers\u002Fplayfair","2.ciphers\u002F21.playfair",{"title":128,"path":129,"stem":130},"Four-square","\u002Fciphers\u002Ffour-square","2.ciphers\u002F22.four-square",{"title":132,"path":133,"stem":134},"Two-square","\u002Fciphers\u002Ftwo-square","2.ciphers\u002F23.two-square",{"title":136,"path":137,"stem":138},"Hill","\u002Fciphers\u002Fhill","2.ciphers\u002F24.hill",{"title":140,"path":141,"stem":142},"Polybius square","\u002Fciphers\u002Fpolybius","2.ciphers\u002F25.polybius",{"title":144,"path":145,"stem":146},"Nihilist","\u002Fciphers\u002Fnihilist","2.ciphers\u002F26.nihilist",{"title":148,"path":149,"stem":150},"Morse code","\u002Fciphers\u002Fmorse","2.ciphers\u002F27.morse",{"title":152,"path":153,"stem":154},"Fractionated Morse","\u002Fciphers\u002Ffractionated-morse","2.ciphers\u002F28.fractionated-morse",{"title":156,"path":157,"stem":158},"Bacon's cipher","\u002Fciphers\u002Fbacon","2.ciphers\u002F29.bacon",{"title":160,"path":161,"stem":162},"Tap code","\u002Fciphers\u002Ftap-code","2.ciphers\u002F30.tap-code",{"title":164,"path":165,"stem":166},"A1Z26","\u002Fciphers\u002Fa1z26","2.ciphers\u002F31.a1z26",{"title":168,"path":169,"stem":170},"Book cipher","\u002Fciphers\u002Fbook","2.ciphers\u002F32.book",{"title":172,"path":173,"stem":174},"ADFGVX","\u002Fciphers\u002Fadfgvx","2.ciphers\u002F33.adfgvx",{"title":176,"path":177,"stem":178},"Bifid","\u002Fciphers\u002Fbifid","2.ciphers\u002F34.bifid",{"title":180,"path":181,"stem":182},"Straddling checkerboard","\u002Fciphers\u002Fstraddling-checkerboard","2.ciphers\u002F35.straddling-checkerboard",{"title":184,"path":185,"stem":186},"Rail fence","\u002Fciphers\u002Frail-fence","2.ciphers\u002F36.rail-fence",{"title":188,"path":189,"stem":190},"Columnar transposition","\u002Fciphers\u002Fcolumnar","2.ciphers\u002F37.columnar",{"title":192,"path":193,"stem":194},"Route transposition","\u002Fciphers\u002Froute","2.ciphers\u002F38.route",{"title":196,"path":197,"stem":198},"Enigma M3","\u002Fciphers\u002Fenigma","2.ciphers\u002F39.enigma",{"title":200,"path":201,"stem":202},"Block","\u002Fciphers\u002Fblock","2.ciphers\u002F40.block",{"title":204,"path":205,"stem":206},"AES (ECB)","\u002Fciphers\u002Faes","2.ciphers\u002F41.aes",{"title":208,"path":209,"stem":210},"AES (CBC)","\u002Fciphers\u002Faes-cbc","2.ciphers\u002F42.aes-cbc",{"title":212,"path":213,"stem":214},"AES (CFB)","\u002Fciphers\u002Faes-cfb","2.ciphers\u002F43.aes-cfb",{"title":216,"path":217,"stem":218},"AES (OFB)","\u002Fciphers\u002Faes-ofb","2.ciphers\u002F44.aes-ofb",{"title":220,"path":221,"stem":222},"AES (CTR)","\u002Fciphers\u002Faes-ctr","2.ciphers\u002F45.aes-ctr",{"title":224,"path":225,"stem":226},"AES (CCM)","\u002Fciphers\u002Faes-ccm","2.ciphers\u002F46.aes-ccm",{"title":228,"path":229,"stem":230},"AES (OCB)","\u002Fciphers\u002Faes-ocb","2.ciphers\u002F47.aes-ocb",{"title":232,"path":233,"stem":234},"AES (LRW)","\u002Fciphers\u002Faes-lrw","2.ciphers\u002F48.aes-lrw",{"title":236,"path":237,"stem":238},"AES (XTS)","\u002Fciphers\u002Faes-xts","2.ciphers\u002F49.aes-xts",{"title":240,"path":241,"stem":242},"AES (CBC-MAC)","\u002Fciphers\u002Faes-cbc-mac","2.ciphers\u002F50.aes-cbc-mac",{"title":244,"path":245,"stem":246},"AES (passphrase)","\u002Fciphers\u002Faes-passphrase","2.ciphers\u002F51.aes-passphrase",{"title":248,"path":249,"stem":250},"Rijndael (ECB)","\u002Fciphers\u002Frijndael","2.ciphers\u002F52.rijndael",{"title":252,"path":253,"stem":254},"DES (ECB)","\u002Fciphers\u002Fdes","2.ciphers\u002F53.des",{"title":256,"path":257,"stem":258},"DESX (ECB)","\u002Fciphers\u002Fdesx","2.ciphers\u002F54.desx",{"title":260,"path":261,"stem":262},"Triple DES (ECB)","\u002Fciphers\u002Ftriple-des","2.ciphers\u002F55.triple-des",{"title":264,"path":265,"stem":266},"Triple DES (CBC)","\u002Fciphers\u002Ftriple-des-cbc","2.ciphers\u002F56.triple-des-cbc",{"title":268,"path":269,"stem":270},"Blowfish (ECB)","\u002Fciphers\u002Fblowfish","2.ciphers\u002F57.blowfish",{"title":272,"path":273,"stem":274},"IDEA (ECB)","\u002Fciphers\u002Fidea","2.ciphers\u002F58.idea",{"title":276,"path":277,"stem":278},"Lucifer (ECB)","\u002Fciphers\u002Flucifer","2.ciphers\u002F59.lucifer",{"title":280,"path":281,"stem":282},"MARS (ECB)","\u002Fciphers\u002Fmars","2.ciphers\u002F60.mars",{"title":284,"path":285,"stem":286},"Serpent (ECB)","\u002Fciphers\u002Fserpent","2.ciphers\u002F61.serpent",{"title":288,"path":289,"stem":290},"CAST5 (ECB)","\u002Fciphers\u002Fcast5","2.ciphers\u002F62.cast5",{"title":292,"path":293,"stem":294},"Camellia (ECB)","\u002Fciphers\u002Fcamellia","2.ciphers\u002F63.camellia",{"title":296,"path":297,"stem":298},"ARIA (ECB)","\u002Fciphers\u002Faria","2.ciphers\u002F64.aria",{"title":300,"path":301,"stem":302},"SM4 (ECB)","\u002Fciphers\u002Fsm4","2.ciphers\u002F65.sm4",{"title":304,"path":305,"stem":306},"OpenPGP (passphrase)","\u002Fciphers\u002Fopenpgp","2.ciphers\u002F66.openpgp",{"title":308,"path":309,"stem":310},"Stream","\u002Fciphers\u002Fstream","2.ciphers\u002F67.stream",{"title":312,"path":313,"stem":314},"Rabbit","\u002Fciphers\u002Frabbit","2.ciphers\u002F68.rabbit",{"title":316,"path":317,"stem":318},"RC4","\u002Fciphers\u002Frc4","2.ciphers\u002F69.rc4",{"title":320,"path":321,"stem":322},"XOR","\u002Fciphers\u002Fxor","2.ciphers\u002F70.xor",{"title":324,"path":325,"stem":326},"Salsa20","\u002Fciphers\u002Fsalsa20","2.ciphers\u002F71.salsa20",{"title":328,"path":329,"stem":330},"XSalsa20","\u002Fciphers\u002Fxsalsa20","2.ciphers\u002F72.xsalsa20",{"title":332,"path":333,"stem":334},"ChaCha20","\u002Fciphers\u002Fchacha20","2.ciphers\u002F73.chacha20",{"title":336,"path":337,"stem":338},"XChaCha20","\u002Fciphers\u002Fxchacha20","2.ciphers\u002F74.xchacha20",{"title":340,"path":341,"stem":342},"ChaCha20-Poly1305","\u002Fciphers\u002Fchacha20-poly1305","2.ciphers\u002F75.chacha20-poly1305","i-lucide-library",{"id":345,"title":300,"body":346,"description":589,"extension":590,"links":591,"meta":592,"navigation":593,"path":301,"seo":594,"stem":302,"__hash__":595},"docs\u002F2.ciphers\u002F65.sm4.md",{"type":347,"value":348,"toc":583},"minimark",[349,353,362,373,467,472,475,478,481,485,488,495,498,502,509,513,516,523,552,555,576,579],[350,351],"cipher-facts",{"name":352},"sm4",[354,355,356,357,361],"p",{},"SM4 is China's block cipher. It started life as SMS4, the cipher inside WAPI, China's own Wi-Fi security standard from 2003. China pitched it for IEEE 802.11i too. It didn't get in. So it went its own way: published on its own in 2006, renamed SM4 in 2012, a national standard as GB\u002FT 32907-2016 and an ISO one a year later. TLS 1.3 has suites for it since RFC 8998, and OpenSSL ships ",[358,359,360],"code",{},"sm4-ecb",". One key size, 128 bits. One block size, 16 bytes. No menu to pick from.",[354,363,364,365,368,369,372],{},"Here it runs on its own, in ECB, like ",[358,366,367],{},"camellia"," and ",[358,370,371],{},"aria",". UTF-8 text in, PKCS#7 padding, hex out.",[374,375,380],"pre",{"className":376,"code":377,"language":378,"meta":379,"style":379},"language-ts shiki shiki-themes ciphers ciphers ciphers","const sm4 = create(\"sm4\");\nconst key = \"0123456789abcdeffedcba9876543210\";\nsm4.encode(\"ATTACK AT DAWN\", { key }).text; \u002F\u002F \"47dea04afc3c7e332fcac11987a2ec6e\"\nsm4.decode(\"47dea04afc3c7e332fcac11987a2ec6e\", { key }).text; \u002F\u002F \"ATTACK AT DAWN\"\n","ts","",[358,381,382,412,428,449],{"__ignoreMap":379},[383,384,387,391,395,398,402,405,409],"span",{"class":385,"line":386},"line",1,[383,388,390],{"class":389},"skH_V","const",[383,392,394],{"class":393},"s38Sx"," sm4 ",[383,396,397],{"class":389},"=",[383,399,401],{"class":400},"sK71F"," create",[383,403,404],{"class":393},"(",[383,406,408],{"class":407},"shU9J","\"sm4\"",[383,410,411],{"class":393},");\n",[383,413,415,417,420,422,425],{"class":385,"line":414},2,[383,416,390],{"class":389},[383,418,419],{"class":393}," key ",[383,421,397],{"class":389},[383,423,424],{"class":407}," \"0123456789abcdeffedcba9876543210\"",[383,426,427],{"class":393},";\n",[383,429,431,434,437,439,442,445],{"class":385,"line":430},3,[383,432,433],{"class":393},"sm4.",[383,435,436],{"class":400},"encode",[383,438,404],{"class":393},[383,440,441],{"class":407},"\"ATTACK AT DAWN\"",[383,443,444],{"class":393},", { key }).text; ",[383,446,448],{"class":447},"scIB-","\u002F\u002F \"47dea04afc3c7e332fcac11987a2ec6e\"\n",[383,450,452,454,457,459,462,464],{"class":385,"line":451},4,[383,453,433],{"class":393},[383,455,456],{"class":400},"decode",[383,458,404],{"class":393},[383,460,461],{"class":407},"\"47dea04afc3c7e332fcac11987a2ec6e\"",[383,463,444],{"class":393},[383,465,466],{"class":447},"\u002F\u002F \"ATTACK AT DAWN\"\n",[468,469,471],"h2",{"id":470},"four-words-in-a-queue","Four words in a queue",[354,473,474],{},"Forget halves. SM4 cuts the block into four 32-bit words and lines them up. Each round takes the three newest, XORs them together with a round key and pushes the result through T. That result, XORed with the oldest word, joins the queue at the back. The oldest one leaves. Feistel, just unbalanced: three words drive one.",[354,476,477],{},"T is two steps. First tau, the S-box on each of the four bytes. Then L, which XORs the word with copies of itself rotated by 2, 10, 18 and 24 bits. That's the whole round. No tables beyond one S-box, no multiplication.",[354,479,480],{},"After 32 rounds the last four words come out in reverse order. Why reverse them? Because then decryption is the same 32 rounds with the round keys backwards. Same code, both ways.",[468,482,484],{"id":483},"the-key-schedule-is-the-cipher-again","The key schedule is the cipher again",[354,486,487],{},"The key goes in as four words too, each XORed with a family constant FK first. Then the same queue runs 32 times. Two things change. L gets lighter, rotations by 13 and 23 only. And a constant CK joins the XOR every round. Every new word it makes is a round key.",[354,489,490,491,494],{},"CK looks like a table of 32 random words. It isn't. Byte j of CK_i is ",[358,492,493],{},"(4i + j) × 7 mod 256",", so the file computes it on the spot. Thirty-two words nobody has to copy by hand.",[354,496,497],{},"The S-box is the one thing that's stored, as the standard prints it. The draft says Lagrange interpolation turns it into a polynomial of degree 254 with 255 terms. Good luck solving that one by hand.",[468,499,501],{"id":500},"checked-against","Checked against",[354,503,504,505,508],{},"GB\u002FT 32907-2016 example 1 both ways, and examples 4 and 5 of the draft-ribose-cfrg-sm4 appendix. Example 2 of the standard too: one block encrypted a million times under the same key. That takes about a second here, so it runs with the rest of the suite. The ECB examples from the draft, two blocks each, check whole messages. Then every single-bit vector, one bit set in the key over a zero block or in the block under a zero key. That's 256 ciphertexts, checked by digest against OpenSSL. UTF-8 text with padding comes from ",[358,506,507],{},"openssl enc -sm4-ecb",".",[468,510,512],{"id":511},"how-strong-is-it","How strong is it",[354,514,515],{},"The draft lists a decade of cryptanalysis and no practical attack on the full cipher. The best ones in its survey reach 24 of the 32 rounds. The block is 128 bits, so Sweet32 doesn't apply either.",[354,517,518,519,522],{},"ECB is still ECB, though. Thirty-two ",[358,520,521],{},"A","s are two equal blocks, and they come out as two equal blocks:",[374,524,526],{"className":376,"code":525,"language":378,"meta":379,"style":379},"sm4.encode(\"A\".repeat(32), { key }).text;\n\u002F\u002F 99ce75c0ca2949d3eb87bd2d831f3510 99ce75c0ca2949d3eb87bd2d831f3510 002a8a4efa863ccad024ac0300bb40d2\n",[358,527,528,547],{"__ignoreMap":379},[383,529,530,532,534,536,539,541,544],{"class":385,"line":386},[383,531,433],{"class":393},[383,533,436],{"class":400},[383,535,404],{"class":393},[383,537,538],{"class":407},"\"A\"",[383,540,508],{"class":393},[383,542,543],{"class":400},"repeat",[383,545,546],{"class":393},"(32), { key }).text;\n",[383,548,549],{"class":385,"line":414},[383,550,551],{"class":447},"\u002F\u002F 99ce75c0ca2949d3eb87bd2d831f3510 99ce75c0ca2949d3eb87bd2d831f3510 002a8a4efa863ccad024ac0300bb40d2\n",[354,553,554],{},"Spaces added to show the blocks. The last one is only padding.",[354,556,557,558,561,562,565,566,568,569,572,573,575],{},"A key that isn't 32 hex digits is an ",[358,559,560],{},"InvalidOptionError",", a missing one a ",[358,563,564],{},"MissingOptionError",". On ",[358,567,456],{},", ciphertext that isn't whole 16-byte blocks of hex is a ",[358,570,571],{},"CipherError",". A wrong key almost always breaks the padding, and that's a ",[358,574,571],{}," too. So are decrypted bytes that aren't UTF-8.",[354,577,578],{},"Plain TypeScript, not constant time. Puzzles and learning, not secrets.",[580,581,582],"style",{},"html pre.shiki code .skH_V, html code.shiki .skH_V{--shiki-light:var(--shiki-token-keyword);--shiki-default:var(--shiki-token-keyword);--shiki-dark:var(--shiki-token-keyword)}html pre.shiki code .s38Sx, html code.shiki .s38Sx{--shiki-light:var(--ui-text-highlighted);--shiki-default:var(--ui-text-highlighted);--shiki-dark:var(--ui-text-highlighted)}html pre.shiki code .sK71F, html code.shiki .sK71F{--shiki-light:var(--shiki-token-function);--shiki-default:var(--shiki-token-function);--shiki-dark:var(--shiki-token-function)}html pre.shiki code .shU9J, html code.shiki .shU9J{--shiki-light:var(--shiki-token-string);--shiki-default:var(--shiki-token-string);--shiki-dark:var(--shiki-token-string)}html pre.shiki code .scIB-, html code.shiki .scIB-{--shiki-light:var(--shiki-token-comment);--shiki-default:var(--shiki-token-comment);--shiki-dark:var(--shiki-token-comment)}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":379,"searchDepth":414,"depth":414,"links":584},[585,586,587,588],{"id":470,"depth":414,"text":471},{"id":483,"depth":414,"text":484},{"id":500,"depth":414,"text":501},{"id":511,"depth":414,"text":512},"China's standard block cipher from GB\u002FT 32907-2016. Thirty-two rounds over four 32-bit words and a 128-bit key. ECB with PKCS#7 padding.","md",null,{},true,{"title":300,"description":589},"TI7bDRg1BscuSgqFPKStgn7ZC3-bBpBlNgs__HLEX1M",[597,599],{"title":296,"path":297,"stem":298,"description":598,"children":-1},"South Korea's standard block cipher from RFC 5794. S-box rounds and a byte mixer on 16-byte blocks under the key sizes of AES. ECB with PKCS#7 padding.",{"title":304,"path":305,"stem":306,"description":600,"children":-1},"What gpg --symmetric writes. A PGP MESSAGE block opened with a passphrase, from IDEA to AES and Camellia, with ZIP, ZLIB or BZip2 inside.",1792051956710]