1:HL["/_next/static/css/275839517c59c532.css",{"as":"style"}] 2:HL["/_next/static/css/bdb880d990e879b6.css",{"as":"style"}] 0:[[["",{"children":["post",{"children":[["slug","sm4-implementing","d"],{"children":["__PAGE__?{\"slug\":\"sm4-implementing\"}",{}]}]}]},"$undefined","$undefined",true],"$L3",[[["$","link","0",{"rel":"stylesheet","href":"/_next/static/css/275839517c59c532.css","precedence":"next"}],["$","link","1",{"rel":"stylesheet","href":"/_next/static/css/bdb880d990e879b6.css","precedence":"next"}]],["$L4",null]]]] 5:HL["/_next/static/css/95c7fb627fba8423.css",{"as":"style"}] 6:HL["/_next/static/css/477df780fc5cb593.css",{"as":"style"}] 7:HL["/_next/static/css/c40a92e7f996f910.css",{"as":"style"}] 3:["$L8",null] 4:[["$","meta","0",{"charSet":"utf-8"}],["$","title","1",{"children":"SM4 对称加密算法与其 CBC 模式实现"}],["$","meta","2",{"name":"description","content":"SM4 对称加密算法与其 CBC 模式实现"}],["$","link","3",{"rel":"manifest","href":"/manifest.json"}],["$","meta","4",{"name":"generator","content":"Hexo.js & Next.js"}],["$","meta","5",{"name":"viewport","content":"width=device-width, initial-scale=1"}],["$","meta","6",{"property":"og:title","content":"SM4 对称加密算法与其 CBC 模式实现"}],["$","meta","7",{"property":"og:description","content":"SM4 对称加密算法与其 CBC 模式实现"}],["$","meta","8",{"name":"twitter:card","content":"summary"}],["$","meta","9",{"name":"twitter:title","content":"SM4 对称加密算法与其 CBC 模式实现"}],["$","meta","10",{"name":"twitter:description","content":"SM4 对称加密算法与其 CBC 模式实现"}]] 9:I{"id":"7477","chunks":["194:static/chunks/194-26e3c21be498c0ce.js","859:static/chunks/859-ea023633456a13f8.js","355:static/chunks/app/tags/[slug]/page-257dc97429efd72a.js"],"name":"","async":false} a:I{"id":"92","chunks":["194:static/chunks/194-26e3c21be498c0ce.js","92:static/chunks/92-371a458fbe090447.js","284:static/chunks/284-b1d21b691d3eabee.js","605:static/chunks/app/post/[slug]/page-0339b76e369b6af8.js"],"name":"","async":false} b:I{"id":"2449","chunks":["194:static/chunks/194-26e3c21be498c0ce.js","92:static/chunks/92-371a458fbe090447.js","185:static/chunks/app/layout-4eab34e1c4d9af8d.js"],"name":"","async":false} c:I{"id":"3211","chunks":["272:static/chunks/webpack-7471fa70de6bdb29.js","253:static/chunks/bce60fc1-2413e66000a5dd8f.js","769:static/chunks/769-2bf088c0a421e73d.js"],"name":"","async":false} d:I{"id":"5767","chunks":["272:static/chunks/webpack-7471fa70de6bdb29.js","253:static/chunks/bce60fc1-2413e66000a5dd8f.js","769:static/chunks/769-2bf088c0a421e73d.js"],"name":"","async":false} f:I{"id":"6424","chunks":["194:static/chunks/194-26e3c21be498c0ce.js","92:static/chunks/92-371a458fbe090447.js","185:static/chunks/app/layout-4eab34e1c4d9af8d.js"],"name":"GaLite","async":false} 10:I{"id":"9869","chunks":["194:static/chunks/194-26e3c21be498c0ce.js","92:static/chunks/92-371a458fbe090447.js","185:static/chunks/app/layout-4eab34e1c4d9af8d.js"],"name":"SpeedInsights","async":false} 11:I{"id":"7148","chunks":["194:static/chunks/194-26e3c21be498c0ce.js","92:static/chunks/92-371a458fbe090447.js","185:static/chunks/app/layout-4eab34e1c4d9af8d.js"],"name":"Analytics","async":false} 8:["$","html",null,{"lang":"zh-CN","children":[["$","link",null,{"rel":"icon","href":"/images/icons/icon-72x72.png","type":"image/x-icon"}],["$","link",null,{"rel":"preconnect","href":"https://vip2.loli.io"}],["$","link",null,{"rel":"dns-prefetch","href":"https://vip2.loli.io"}],["$","link",null,{"rel":"alternate","type":"application/atom+xml","href":"/atom.xml"}],["$","body",null,{"children":["$","div",null,{"className":"kbCXHY jdraHW eqrBPF kEFtPS bNzOWQ juexza kXMrYr ","children":[["$","header",null,{"className":"doNOqr WhAZY cRUUAa cwMEsi dpJmjl bsTuZj iRietU JCsMI fONtwf eEsPgn gWUoqV kazZiE fsKTUV dkPCxO gdGTeM ","children":[["$","div",null,{"className":"doNOqr WhAZY hrtgtE iYRJzs iJGxaV jlwzhw ","children":[["$","$L9",null,{"className":"icyDkI gSBWlu foGVKH IVbXa kooHYa JxWnH cVJMrm hyoqRt jlijat kUpitc gdtkYW iDPWLw kayxZK hCkclF cneMsd gYPNzh ","href":"/","children":[["$","div",null,{"className":"eSltVp cpOcAb caItCN cyerGB dSxtaa lbEyiT kUPESX Pmecg ldtSOY ","children":["$","$La",null,{"src":"https://vip2.loli.io/2023/03/09/2tAMcy694lE3IZX.jpg","blurDataURL":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAW0lEQVR4nAFQAK//AAAAAA0MCD08JoiyXkqWNQBPSDHtwYf/86ehuWNAeS4Aep9O+bt9/9aWxbtzETMSAClqHld9MqambcuqdgMOAwBCaS1Mdzd/hFE4PCMEEAS4ex049PWXOAAAAABJRU5ErkJggg==","alt":"avatar","width":200,"height":200,"layout":"responsive","className":"jWjrEQ eKtERL BRobm iovjFN ","placeholder":"blur","priority":true}]}],["$","h1",null,{"className":"evYdWj cpOcAb XEVlt huiurs ","children":"fengkx's Blog"}]]}],["$","h2",null,{"id":"name","className":"hrtgtE fcXWHl ","children":"fengkx"}],["$","h3",null,{"id":"title","className":"hrtgtE fcXWHl ","children":"Student & Coder"}],["$","div",null,{"className":"fONtwf fcXWHl foGVKH IVbXa dPVLzs fkrGAA cvCecb jJGIjV ","children":[["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","children":["$undefined",[["$","path","0",{"d":"M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z","children":"$undefined"}],["$","circle","1",{"cx":"12","cy":"10","r":"3","children":"$undefined"}]]],"className":"$undefined","style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}],["$","span",null,{"className":"gUpJEt ","children":"Guangzhou, China"}]]}],["$","nav",null,{"className":"hrtgtE jlwzhw gSBWlu IVbXa kKRHCo jzaqKj ehqwGF ","children":[["$","div",null,{"className":"kooHYa ","children":["$","$L9",null,{"className":"gSBWlu foGVKH fPWmiY JxWnH cVJMrm OqOoD jJbtJp ihIJmy bgUfpT AsNjI kwISoH gdPTUr eLDTYY dmKgnC dPFrWx bmQfsF krqYva kXurrt ","href":"/","children":[["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","className":"jJhMtm fcXWHl gdPTUr dCiVRS TTRIX ","children":["$undefined",[["$","path","0",{"d":"M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z","children":"$undefined"}],["$","polyline","1",{"points":"9 22 9 12 15 12 15 22","children":"$undefined"}]]],"style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}],["$","span",null,{"className":"","children":"首页"}]]}]}],["$","div",null,{"className":"kooHYa ","children":["$","$L9",null,{"className":"gSBWlu foGVKH fPWmiY JxWnH cVJMrm OqOoD jJbtJp ihIJmy bgUfpT AsNjI kwISoH gdPTUr eLDTYY dmKgnC dPFrWx bmQfsF krqYva kXurrt ","href":"/archives","children":[["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","className":"jJhMtm fcXWHl gdPTUr dCiVRS TTRIX ","children":["$undefined",[["$","polyline","0",{"points":"21 8 21 21 3 21 3 8","children":"$undefined"}],["$","rect","1",{"x":"1","y":"3","width":"22","height":"5","children":"$undefined"}],["$","line","2",{"x1":"10","y1":"12","x2":"14","y2":"12","children":"$undefined"}]]],"style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}],["$","span",null,{"className":"","children":"归档"}]]}]}],["$","div",null,{"className":"kooHYa ","children":["$","$L9",null,{"className":"gSBWlu foGVKH fPWmiY JxWnH cVJMrm OqOoD jJbtJp ihIJmy bgUfpT AsNjI kwISoH gdPTUr eLDTYY dmKgnC dPFrWx bmQfsF krqYva kXurrt ","href":"/tags","children":[["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","className":"jJhMtm fcXWHl gdPTUr dCiVRS TTRIX ","children":["$undefined",[["$","path","0",{"d":"M20.59 13.41l-7.17 7.17a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z","children":"$undefined"}],["$","line","1",{"x1":"7","y1":"7","x2":"7.01","y2":"7","children":"$undefined"}]]],"style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}],["$","span",null,{"className":"","children":"标签"}]]}]}],["$","div",null,{"className":"kooHYa ","children":["$","$L9",null,{"className":"gSBWlu foGVKH fPWmiY JxWnH cVJMrm OqOoD jJbtJp ihIJmy bgUfpT AsNjI kwISoH gdPTUr eLDTYY dmKgnC dPFrWx bmQfsF krqYva kXurrt ","href":"/links","children":[["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","className":"jJhMtm fcXWHl gdPTUr dCiVRS TTRIX ","children":["$undefined",[["$","path","0",{"d":"M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2","children":"$undefined"}],["$","circle","1",{"cx":"9","cy":"7","r":"4","children":"$undefined"}],["$","path","2",{"d":"M23 21v-2a4 4 0 0 0-3-3.87","children":"$undefined"}],["$","path","3",{"d":"M16 3.13a4 4 0 0 1 0 7.75","children":"$undefined"}]]],"style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}],["$","span",null,{"className":"","children":"友链"}]]}]}],["$","div",null,{"className":"kooHYa ","children":["$","$L9",null,{"className":"gSBWlu foGVKH fPWmiY JxWnH cVJMrm OqOoD jJbtJp ihIJmy bgUfpT AsNjI kwISoH gdPTUr eLDTYY dmKgnC dPFrWx bmQfsF krqYva kXurrt ","href":"/about","children":[["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","className":"jJhMtm fcXWHl gdPTUr dCiVRS TTRIX ","children":["$undefined",[["$","path","0",{"d":"M18 8h1a4 4 0 0 1 0 8h-1","children":"$undefined"}],["$","path","1",{"d":"M2 8h16v9a4 4 0 0 1-4 4H6a4 4 0 0 1-4-4V8z","children":"$undefined"}],["$","line","2",{"x1":"6","y1":"1","x2":"6","y2":"4","children":"$undefined"}],["$","line","3",{"x1":"10","y1":"1","x2":"10","y2":"4","children":"$undefined"}],["$","line","4",{"x1":"14","y1":"1","x2":"14","y2":"4","children":"$undefined"}]]],"style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}],["$","span",null,{"className":"","children":"关于"}]]}]}],["$","div",null,{"className":"kooHYa ","children":["$","$L9",null,{"className":"gSBWlu foGVKH fPWmiY JxWnH cVJMrm OqOoD jJbtJp ihIJmy bgUfpT AsNjI kwISoH gdPTUr eLDTYY dmKgnC dPFrWx bmQfsF krqYva kXurrt ","href":"/search","children":[["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","className":"cpOcAb gdPTUr dCiVRS TTRIX ","children":["$undefined",[["$","circle","0",{"cx":"11","cy":"11","r":"8","children":"$undefined"}],["$","line","1",{"x1":"21","y1":"21","x2":"16.65","y2":"16.65","children":"$undefined"}]]],"style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}],["$","span",null,{"className":"jJhMtm fcXWHl ","children":"搜索"}]]}]}],["$","div",null,{"className":"kooHYa evYdWj ","children":["$","$L9",null,{"className":"gSBWlu foGVKH fPWmiY JxWnH cVJMrm OqOoD jJbtJp ihIJmy bgUfpT AsNjI kwISoH gdPTUr eLDTYY dmKgnC dPFrWx bmQfsF krqYva kXurrt ","href":"/atom.xml","prefetch":false,"children":[["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","className":"cpOcAb gdPTUr dCiVRS TTRIX ","children":["$undefined",[["$","path","0",{"d":"M4 11a9 9 0 0 1 9 9","children":"$undefined"}],["$","path","1",{"d":"M4 4a16 16 0 0 1 16 16","children":"$undefined"}],["$","circle","2",{"cx":"5","cy":"19","r":"1","children":"$undefined"}]]],"style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}],["$","span",null,{"className":"jJhMtm fcXWHl ","children":"RSS"}]]}]}]]}]]}],["$","div",null,{"className":"doNOqr hrtgtE fcXWHl iigETV bMSzLf XEVlt jmezSN izetJs kdrTtD bLIxaN ","children":[["$","div",null,{"className":"iLYBKc gSBWlu zEGrF evYWGf hDdCaA ","children":[["$","$L9",null,{"title":"fengkx's GitHub","href":"https://github.com/fengkx","prefetch":false,"children":["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","children":["$undefined",[["$","path","0",{"d":"M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22","children":"$undefined"}]]],"className":"$undefined","style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}]}],["$","$L9",null,{"title":"fengkx's Telegram","href":"https://t.me/fengkx","prefetch":false,"children":["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","children":["$undefined",[["$","line","0",{"x1":"22","y1":"2","x2":"11","y2":"13","children":"$undefined"}],["$","polygon","1",{"points":"22 2 15 22 11 13 2 9 22 2","children":"$undefined"}]]],"className":"$undefined","style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}]}],["$","$L9",null,{"href":"https://mstdn.social/@fengkx","rel":"me","prefetch":false,"children":["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","children":["$undefined",[["$","path","0",{"stroke":"none","d":"M0 0h24v24H0z","fill":"none","children":"$undefined"}],["$","path","1",{"d":"M18.648 15.254c-1.816 1.763 -6.648 1.626 -6.648 1.626a18.262 18.262 0 0 1 -3.288 -.256c1.127 1.985 4.12 2.81 8.982 2.475c-1.945 2.013 -13.598 5.257 -13.668 -7.636l-.026 -1.154c0 -3.036 .023 -4.115 1.352 -5.633c1.671 -1.91 6.648 -1.666 6.648 -1.666s4.977 -.243 6.648 1.667c1.329 1.518 1.352 2.597 1.352 5.633s-.456 4.074 -1.352 4.944z","children":"$undefined"}],["$","path","2",{"d":"M12 11.204v-2.926c0 -1.258 -.895 -2.278 -2 -2.278s-2 1.02 -2 2.278v4.722m4 -4.722c0 -1.258 .895 -2.278 2 -2.278s2 1.02 2 2.278v4.722","children":"$undefined"}]]],"className":"$undefined","style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}]}],["$","$L9",null,{"title":"RSS feed","href":"/atom.xml","prefetch":false,"children":["$","svg",null,{"stroke":"currentColor","fill":"none","strokeWidth":"2","viewBox":"0 0 24 24","strokeLinecap":"round","strokeLinejoin":"round","children":["$undefined",[["$","path","0",{"d":"M4 11a9 9 0 0 1 9 9","children":"$undefined"}],["$","path","1",{"d":"M4 4a16 16 0 0 1 16 16","children":"$undefined"}],["$","circle","2",{"cx":"5","cy":"19","r":"1","children":"$undefined"}]]],"className":"$undefined","style":{"color":"$undefined"},"height":"1em","width":"1em","xmlns":"http://www.w3.org/2000/svg"}]}]]}],["$","div",null,{"className":"hrtgtE fcXWHl cyerGB kKRHCo ","children":["Build with ",["$","$L9",null,{"title":"Hexo official site","rel":"noopener noreferrer external nofollow","href":"https://hexo.io","children":"Hexo"}]," ","and"," ",["$","$L9",null,{"title":"Next.js official site","href":"https://nextjs.org","rel":"noopener noreferrer external nofollow","children":"Next.js"}]]}]]}]]}],["$","$Lb",null,{"children":["$","$Lc",null,{"parallelRouterKey":"children","segmentPath":["children"],"error":"$undefined","errorStyles":"$undefined","loading":"$undefined","loadingStyles":"$undefined","hasLoading":false,"template":["$","$Ld",null,{}],"templateStyles":"$undefined","notFound":"$undefined","notFoundStyles":"$undefined","childProp":{"current":["$","$Lc",null,{"parallelRouterKey":"children","segmentPath":["children","post","children"],"error":"$undefined","errorStyles":"$undefined","loading":"$undefined","loadingStyles":"$undefined","hasLoading":false,"template":["$","$Ld",null,{}],"templateStyles":"$undefined","notFound":"$undefined","notFoundStyles":"$undefined","childProp":{"current":["$","$Lc",null,{"parallelRouterKey":"children","segmentPath":["children","post","children",["slug","sm4-implementing","d"],"children"],"error":"$undefined","errorStyles":"$undefined","loading":"$undefined","loadingStyles":"$undefined","hasLoading":false,"template":["$","$Ld",null,{}],"templateStyles":"$undefined","notFound":"$undefined","notFoundStyles":"$undefined","childProp":{"current":["$Le",null],"segment":"__PAGE__?{\"slug\":\"sm4-implementing\"}"},"styles":[["$","link","0",{"rel":"stylesheet","href":"/_next/static/css/275839517c59c532.css","precedence":"next"}],["$","link","1",{"rel":"stylesheet","href":"/_next/static/css/95c7fb627fba8423.css","precedence":"next"}],["$","link","2",{"rel":"stylesheet","href":"/_next/static/css/477df780fc5cb593.css","precedence":"next"}],["$","link","3",{"rel":"stylesheet","href":"/_next/static/css/c40a92e7f996f910.css","precedence":"next"}]]}],"segment":["slug","sm4-implementing","d"]},"styles":[]}],"segment":"post"},"styles":[]}]}],["$","$Lf",null,{"uaId":"UA-103237573-1"}],["$","$L10",null,{}],["$","$L11",null,{}]]}]}]]}] 13:I{"id":"5307","chunks":["194:static/chunks/194-26e3c21be498c0ce.js","92:static/chunks/92-371a458fbe090447.js","284:static/chunks/284-b1d21b691d3eabee.js","605:static/chunks/app/post/[slug]/page-0339b76e369b6af8.js"],"name":"TocTitle","async":false} e:[false,["$","main",null,{"className":"prose eUUCKp kTeytq jmezSN cjScYX eqrBPF jdraHW cRUUAa DOWJl hgvoZN iZwowi jGHqUK kwISoH AsNjI","children":[["$","h1",null,{"className":"EKhXX ","children":"SM4 对称加密算法与其 CBC 模式实现"}],"$L12"]}],["$","aside",null,{"className":"cwMEsi dpJmjl gWUoqV kazZiE fsKTUV dkPCxO fcXWHl hrtgtE icKiSN eeREmo fZMRmg hRjOno ","children":["$","div",null,{"className":"doNOqr gepZXl AsideContainer_asideContainer___FNWl","children":["$","div",null,{"className":"bKqOie lkcNSa doNOqr dNtEOi ","children":[["$","div",null,{"className":"hlBtvm ckBWJI XEVlt ","children":"文章目录"}],["$","div",null,{"className":"$undefined","children":["$","ol",null,{"className":"jOeduE ","children":[["$","li",null,{"children":[["$","$L13",null,{"id":"SM4-对称分组加密","text":"SM4 对称分组加密"}],false]}],["$","li",null,{"children":[["$","$L13",null,{"id":"具体实现","text":"具体实现"}],["$","ol",null,{"className":"jOeduE iDuqPI ","children":[["$","li",null,{"children":[["$","$L13",null,{"id":"生成轮秘钥","text":"生成轮秘钥"}],false]}]]}]]}],["$","li",null,{"children":[["$","$L13",null,{"id":"使用轮秘钥进行加密","text":"使用轮秘钥进行加密"}],false]}],["$","li",null,{"children":[["$","$L13",null,{"id":"解密算法","text":"解密算法"}],false]}],["$","li",null,{"children":[["$","$L13",null,{"id":"总结","text":"总结"}],false]}],["$","li",null,{"children":[["$","$L13",null,{"id":"实际应用-加密文件","text":"实际应用 - 加密文件"}],["$","ol",null,{"className":"jOeduE iDuqPI ","children":[["$","li",null,{"children":[["$","$L13",null,{"id":"Padding","text":"Padding"}],false]}]]}]]}],["$","li",null,{"children":[["$","$L13",null,{"id":"CBC","text":"CBC"}],false]}],["$","li",null,{"children":[["$","$L13",null,{"id":"具体实现-2","text":"具体实现"}],false]}],["$","li",null,{"children":[["$","$L13",null,{"id":"体会","text":"体会"}],false]}],["$","li",null,{"children":[["$","$L13",null,{"id":"参考文章","text":"参考文章"}],false]}]]}]}]]}]}]}]] 14:I{"id":"4998","chunks":["194:static/chunks/194-26e3c21be498c0ce.js","92:static/chunks/92-371a458fbe090447.js","284:static/chunks/284-b1d21b691d3eabee.js","605:static/chunks/app/post/[slug]/page-0339b76e369b6af8.js"],"name":"ArticleContentClient","async":false} 17:I{"id":"7974","chunks":["194:static/chunks/194-26e3c21be498c0ce.js","92:static/chunks/92-371a458fbe090447.js","284:static/chunks/284-b1d21b691d3eabee.js","605:static/chunks/app/post/[slug]/page-0339b76e369b6af8.js"],"name":"H1","async":false} 1a:I{"id":"7974","chunks":["194:static/chunks/194-26e3c21be498c0ce.js","92:static/chunks/92-371a458fbe090447.js","284:static/chunks/284-b1d21b691d3eabee.js","605:static/chunks/app/post/[slug]/page-0339b76e369b6af8.js"],"name":"H2","async":false} 12:["$","$L14",null,{"permalink":"https://www.fengkx.top/post/sm4-implementing/","dateString":"2019-11-30","comments":true,"aplayer":false,"showCopyright":true,"children":[["$","p","4318",{"children":["继","$L15","之后下一个要实现的算法是","$L16","。"]}],"\n",["$","$L17","4319",{"id":"SM4-对称分组加密","children":["SM4 对称分组加密"]}],"\n",["$","p","4320",{"children":["SM4 算法是一个对称分组加密算法,类似于 DES 或者 AES。对称加密也就是加密与解密的密钥是同一个。加密和解密时会将输入数据按固定的比特长度分块进行加密。不同于 AES 提供多个版本。SM4 的的分组长度为 128bit,密钥长度为 128bit。SM4 的一个有趣的地方在于,生成密钥的算法和加密算法本身是一致的。分组密码实际使用时,对于消息长度不是分组倍数长度的消息要进行 padding,以此也产生出了很多模式。"]}],"\n",["$","p","4323",{"children":["上一次","$L18","之后看了一下 OpenSSL 的实现,觉得自己有必要学习一下 OpenSSL 的函数接口组织。所以这一次先去看了看 OpenSSL 的实现并模仿了它的接口的设计,自己实现了","$L19","。"]}],"\n",["$","$L17","4324",{"id":"具体实现","children":["具体实现"]}],"\n",["$","pre","4561",{"className":"shiki github-light","style":{"backgroundColor":"#fff","color":"#24292e"},"tabindex":"0","children":[["$","code","4560",{"className":"language-c","children":[["$","span","4327",{"className":"line","children":[["$","span","4325",{"style":{"color":"#D73A49"},"children":["#ifndef"]}],["$","span","4326",{"style":{"color":"#6F42C1"},"children":[" _sm4_H_"]}]]}],"\n",["$","span","4330",{"className":"line","children":[["$","span","4328",{"style":{"color":"#D73A49"},"children":["#define"]}],["$","span","4329",{"style":{"color":"#6F42C1"},"children":[" _sm4_H_"]}]]}],"\n",["$","span","4331",{"className":"line","children":[]}],"\n",["$","span","4334",{"className":"line","children":[["$","span","4332",{"style":{"color":"#D73A49"},"children":["#include"]}],["$","span","4333",{"style":{"color":"#032F62"},"children":[" "]}]]}],"\n",["$","span","4337",{"className":"line","children":[["$","span","4335",{"style":{"color":"#D73A49"},"children":["#include"]}],["$","span","4336",{"style":{"color":"#032F62"},"children":[" "]}]]}],"\n",["$","span","4338",{"className":"line","children":[]}],"\n",["$","span","4342",{"className":"line","children":[["$","span","4339",{"style":{"color":"#D73A49"},"children":["#define"]}],["$","span","4340",{"style":{"color":"#6F42C1"},"children":[" SM4_DECRYPT"]}],["$","span","4341",{"style":{"color":"#005CC5"},"children":[" 0"]}]]}],"\n",["$","span","4346",{"className":"line","children":[["$","span","4343",{"style":{"color":"#D73A49"},"children":["#define"]}],["$","span","4344",{"style":{"color":"#6F42C1"},"children":[" SM4_ENCRYPT"]}],["$","span","4345",{"style":{"color":"#005CC5"},"children":[" 1"]}]]}],"\n",["$","span","4347",{"className":"line","children":[]}],"\n",["$","span","4351",{"className":"line","children":[["$","span","4348",{"style":{"color":"#D73A49"},"children":["#define"]}],["$","span","4349",{"style":{"color":"#6F42C1"},"children":[" SM4_BLOCK_SIZE"]}],["$","span","4350",{"style":{"color":"#005CC5"},"children":[" 16"]}]]}],"\n",["$","span","4355",{"className":"line","children":[["$","span","4352",{"style":{"color":"#D73A49"},"children":["#define"]}],["$","span","4353",{"style":{"color":"#6F42C1"},"children":[" SM4_KEY_SCHEDULE"]}],["$","span","4354",{"style":{"color":"#005CC5"},"children":[" 32"]}]]}],"\n",["$","span","4356",{"className":"line","children":[]}],"\n",["$","span","4360",{"className":"line","children":[["$","span","4357",{"style":{"color":"#D73A49"},"children":["typedef"]}],["$","span","4358",{"style":{"color":"#D73A49"},"children":[" struct"]}],["$","span","4359",{"style":{"color":"#24292E"},"children":[" {"]}]]}],"\n",["$","span","4364",{"className":"line","children":[["$","span","4361",{"style":{"color":"#D73A49"},"children":[" int"]}],["$","span","4362",{"style":{"color":"#24292E"},"children":[" mode;"]}],["$","span","4363",{"style":{"color":"#6A737D"},"children":[" // ENCRYPT OR DECRYPT"]}]]}],"\n",["$","span","4369",{"className":"line","children":[["$","span","4365",{"style":{"color":"#D73A49"},"children":[" uint32_t"]}],["$","span","4366",{"style":{"color":"#E36209"},"children":[" rk"]}],["$","span","4367",{"style":{"color":"#24292E"},"children":["[SM4_KEY_SCHEDULE];"]}],["$","span","4368",{"style":{"color":"#6A737D"},"children":[" // rotkey"]}]]}],"\n",["$","span","4370",{"className":"line","children":[]}],"\n",["$","span","4372",{"className":"line","children":[["$","span","4371",{"style":{"color":"#24292E"},"children":["}"]}]]}],"\n",["$","span","4374",{"className":"line","children":[["$","span","4373",{"style":{"color":"#24292E"},"children":["sm4_ctx;"]}]]}],"\n",["$","span","4376",{"className":"line","children":[["$","span","4375",{"style":{"color":"#6A737D"},"children":[" // 对外的接口"]}]]}],"\n",["$","span","4390",{"className":"line","children":[["$","span","4377",{"style":{"color":"#D73A49"},"children":[" int"]}],["$","span","4378",{"style":{"color":"#6F42C1"},"children":[" sm4_set_key"]}],["$","span","4379",{"style":{"color":"#24292E"},"children":["("]}],["$","span","4380",{"style":{"color":"#D73A49"},"children":["const"]}],["$","span","4381",{"style":{"color":"#D73A49"},"children":[" uint8_t"]}],["$","span","4382",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4383",{"style":{"color":"#E36209"},"children":["key"]}],["$","span","4384",{"style":{"color":"#24292E"},"children":[", sm4_ctx "]}],["$","span","4385",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4386",{"style":{"color":"#D73A49"},"children":[" const"]}],["$","span","4387",{"style":{"color":"#E36209"},"children":[" ctx"]}],["$","span","4388",{"style":{"color":"#24292E"},"children":[");"]}],["$","span","4389",{"style":{"color":"#6A737D"},"children":[" // key 128 bit len 16"]}]]}],"\n",["$","span","4408",{"className":"line","children":[["$","span","4391",{"style":{"color":"#D73A49"},"children":[" void"]}],["$","span","4392",{"style":{"color":"#6F42C1"},"children":[" sm4_encrypt"]}],["$","span","4393",{"style":{"color":"#24292E"},"children":["("]}],["$","span","4394",{"style":{"color":"#D73A49"},"children":["const"]}],["$","span","4395",{"style":{"color":"#D73A49"},"children":[" uint8_t"]}],["$","span","4396",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4397",{"style":{"color":"#E36209"},"children":["in"]}],["$","span","4398",{"style":{"color":"#24292E"},"children":[", "]}],["$","span","4399",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4400",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4401",{"style":{"color":"#E36209"},"children":["out"]}],["$","span","4402",{"style":{"color":"#24292E"},"children":[", "]}],["$","span","4403",{"style":{"color":"#D73A49"},"children":["const"]}],["$","span","4404",{"style":{"color":"#24292E"},"children":[" sm4_ctx "]}],["$","span","4405",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4406",{"style":{"color":"#E36209"},"children":["ctx"]}],["$","span","4407",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4426",{"className":"line","children":[["$","span","4409",{"style":{"color":"#D73A49"},"children":[" void"]}],["$","span","4410",{"style":{"color":"#6F42C1"},"children":[" sm4_decrypt"]}],["$","span","4411",{"style":{"color":"#24292E"},"children":["("]}],["$","span","4412",{"style":{"color":"#D73A49"},"children":["const"]}],["$","span","4413",{"style":{"color":"#D73A49"},"children":[" uint8_t"]}],["$","span","4414",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4415",{"style":{"color":"#E36209"},"children":["in"]}],["$","span","4416",{"style":{"color":"#24292E"},"children":[", "]}],["$","span","4417",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4418",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4419",{"style":{"color":"#E36209"},"children":["out"]}],["$","span","4420",{"style":{"color":"#24292E"},"children":[", "]}],["$","span","4421",{"style":{"color":"#D73A49"},"children":["const"]}],["$","span","4422",{"style":{"color":"#24292E"},"children":[" sm4_ctx "]}],["$","span","4423",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4424",{"style":{"color":"#E36209"},"children":["ctx"]}],["$","span","4425",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4427",{"className":"line","children":[]}],"\n",["$","span","4429",{"className":"line","children":[["$","span","4428",{"style":{"color":"#6A737D"},"children":[" // Util 类函数"]}]]}],"\n",["$","span","4443",{"className":"line","children":[["$","span","4430",{"style":{"color":"#D73A49"},"children":[" static"]}],["$","span","4431",{"style":{"color":"#D73A49"},"children":[" inline"]}],["$","span","4432",{"style":{"color":"#D73A49"},"children":[" uint32_t"]}],["$","span","4433",{"style":{"color":"#6F42C1"},"children":[" load_uint32_be"]}],["$","span","4434",{"style":{"color":"#24292E"},"children":["("]}],["$","span","4435",{"style":{"color":"#D73A49"},"children":["const"]}],["$","span","4436",{"style":{"color":"#D73A49"},"children":[" uint8_t"]}],["$","span","4437",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4438",{"style":{"color":"#E36209"},"children":["b"]}],["$","span","4439",{"style":{"color":"#24292E"},"children":[", "]}],["$","span","4440",{"style":{"color":"#D73A49"},"children":["int"]}],["$","span","4441",{"style":{"color":"#E36209"},"children":[" n"]}],["$","span","4442",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4457",{"className":"line","children":[["$","span","4444",{"style":{"color":"#D73A49"},"children":[" static"]}],["$","span","4445",{"style":{"color":"#D73A49"},"children":[" inline"]}],["$","span","4446",{"style":{"color":"#D73A49"},"children":[" void"]}],["$","span","4447",{"style":{"color":"#6F42C1"},"children":[" store_uint32_be"]}],["$","span","4448",{"style":{"color":"#24292E"},"children":["("]}],["$","span","4449",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4450",{"style":{"color":"#E36209"},"children":[" v"]}],["$","span","4451",{"style":{"color":"#24292E"},"children":[", "]}],["$","span","4452",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4453",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4454",{"style":{"color":"#D73A49"},"children":[" const"]}],["$","span","4455",{"style":{"color":"#E36209"},"children":[" b"]}],["$","span","4456",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4471",{"className":"line","children":[["$","span","4458",{"style":{"color":"#D73A49"},"children":[" static"]}],["$","span","4459",{"style":{"color":"#D73A49"},"children":[" inline"]}],["$","span","4460",{"style":{"color":"#D73A49"},"children":[" void"]}],["$","span","4461",{"style":{"color":"#6F42C1"},"children":[" xor_blk"]}],["$","span","4462",{"style":{"color":"#24292E"},"children":["("]}],["$","span","4463",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4464",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4465",{"style":{"color":"#E36209"},"children":["a"]}],["$","span","4466",{"style":{"color":"#24292E"},"children":[", "]}],["$","span","4467",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4468",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4469",{"style":{"color":"#E36209"},"children":["b"]}],["$","span","4470",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4472",{"className":"line","children":[]}],"\n",["$","span","4474",{"className":"line","children":[["$","span","4473",{"style":{"color":"#6A737D"},"children":[" // SM4 主要的置换函数"]}]]}],"\n",["$","span","4491",{"className":"line","children":[["$","span","4475",{"style":{"color":"#D73A49"},"children":[" static"]}],["$","span","4476",{"style":{"color":"#D73A49"},"children":[" inline"]}],["$","span","4477",{"style":{"color":"#D73A49"},"children":[" void"]}],["$","span","4478",{"style":{"color":"#6F42C1"},"children":[" SM4_F"]}],["$","span","4479",{"style":{"color":"#24292E"},"children":["("]}],["$","span","4480",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4481",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4482",{"style":{"color":"#D73A49"},"children":[" const"]}],["$","span","4483",{"style":{"color":"#E36209"},"children":[" blks"]}],["$","span","4484",{"style":{"color":"#24292E"},"children":[", "]}],["$","span","4485",{"style":{"color":"#D73A49"},"children":["const"]}],["$","span","4486",{"style":{"color":"#D73A49"},"children":[" uint32_t"]}],["$","span","4487",{"style":{"color":"#D73A49"},"children":[" *"]}],["$","span","4488",{"style":{"color":"#E36209"},"children":["rkg"]}],["$","span","4489",{"style":{"color":"#24292E"},"children":[");"]}],["$","span","4490",{"style":{"color":"#6A737D"},"children":[" // blks len should be 4 as 128bit"]}]]}],"\n",["$","span","4500",{"className":"line","children":[["$","span","4492",{"style":{"color":"#D73A49"},"children":[" static"]}],["$","span","4493",{"style":{"color":"#D73A49"},"children":[" inline"]}],["$","span","4494",{"style":{"color":"#D73A49"},"children":[" uint32_t"]}],["$","span","4495",{"style":{"color":"#6F42C1"},"children":[" SM4_T"]}],["$","span","4496",{"style":{"color":"#24292E"},"children":["("]}],["$","span","4497",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4498",{"style":{"color":"#E36209"},"children":[" X"]}],["$","span","4499",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4501",{"className":"line","children":[]}],"\n",["$","span","4503",{"className":"line","children":[["$","span","4502",{"style":{"color":"#6A737D"},"children":[" // 对外文件加密接口"]}]]}],"\n",["$","span","4516",{"className":"line","children":[["$","span","4504",{"style":{"color":"#D73A49"},"children":[" void"]}],["$","span","4505",{"style":{"color":"#6F42C1"},"children":[" sm4_encrypt_file"]}],["$","span","4506",{"style":{"color":"#24292E"},"children":["(FILE "]}],["$","span","4507",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4508",{"style":{"color":"#E36209"},"children":["in"]}],["$","span","4509",{"style":{"color":"#24292E"},"children":[", FILE "]}],["$","span","4510",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4511",{"style":{"color":"#E36209"},"children":["out"]}],["$","span","4512",{"style":{"color":"#24292E"},"children":[", sm4_ctx "]}],["$","span","4513",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4514",{"style":{"color":"#E36209"},"children":["ctx"]}],["$","span","4515",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4529",{"className":"line","children":[["$","span","4517",{"style":{"color":"#D73A49"},"children":[" void"]}],["$","span","4518",{"style":{"color":"#6F42C1"},"children":[" sm4_decrypt_file"]}],["$","span","4519",{"style":{"color":"#24292E"},"children":["(FILE "]}],["$","span","4520",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4521",{"style":{"color":"#E36209"},"children":["in"]}],["$","span","4522",{"style":{"color":"#24292E"},"children":[", FILE "]}],["$","span","4523",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4524",{"style":{"color":"#E36209"},"children":["out"]}],["$","span","4525",{"style":{"color":"#24292E"},"children":[", sm4_ctx "]}],["$","span","4526",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4527",{"style":{"color":"#E36209"},"children":["ctx"]}],["$","span","4528",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4542",{"className":"line","children":[["$","span","4530",{"style":{"color":"#D73A49"},"children":[" void"]}],["$","span","4531",{"style":{"color":"#6F42C1"},"children":[" sm4_cbc_encrypt_file"]}],["$","span","4532",{"style":{"color":"#24292E"},"children":["(FILE "]}],["$","span","4533",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4534",{"style":{"color":"#E36209"},"children":["in"]}],["$","span","4535",{"style":{"color":"#24292E"},"children":[", FILE "]}],["$","span","4536",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4537",{"style":{"color":"#E36209"},"children":["out"]}],["$","span","4538",{"style":{"color":"#24292E"},"children":[", sm4_ctx "]}],["$","span","4539",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4540",{"style":{"color":"#E36209"},"children":["ctx"]}],["$","span","4541",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4555",{"className":"line","children":[["$","span","4543",{"style":{"color":"#D73A49"},"children":[" void"]}],["$","span","4544",{"style":{"color":"#6F42C1"},"children":[" sm4_cbc_decrypt_file"]}],["$","span","4545",{"style":{"color":"#24292E"},"children":["(FILE "]}],["$","span","4546",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4547",{"style":{"color":"#E36209"},"children":["in"]}],["$","span","4548",{"style":{"color":"#24292E"},"children":[", FILE "]}],["$","span","4549",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4550",{"style":{"color":"#E36209"},"children":["out"]}],["$","span","4551",{"style":{"color":"#24292E"},"children":[", sm4_ctx "]}],["$","span","4552",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4553",{"style":{"color":"#E36209"},"children":["ctx"]}],["$","span","4554",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4556",{"className":"line","children":[]}],"\n",["$","span","4559",{"className":"line","children":[["$","span","4557",{"style":{"color":"#D73A49"},"children":["#endif"]}],["$","span","4558",{"style":{"color":"#6A737D"},"children":[" // _sm4_H_"]}]]}]]}]]}],"\n",["$","p","4563",{"children":["略过几个 util 类的函数,下面来分析一下算法的一些步骤。",["$","br","4562",{}],"\nSM4 的操作对象为 128bit 的分块,其中的运算都将 128bit 分为 32bit 的字进行。每个块 4 个字。"]}],"\n",["$","$L1a","4564",{"id":"生成轮秘钥","children":["生成轮秘钥"]}],"\n",["$","p","4565",{"children":["此类算法一般都会先生成轮秘钥,SM4 也不例外。SM4 生成轮秘钥的算法和加密算法是几乎一致的,只是中间的一个变换有点区别。按文档实现就好。"]}],"\n",["$","p","4566",{"children":["其中 SBox 的置换操作对于一个字来说是有四个 SBox 并排置换实现的"]}],"\n",["$","pre","4641",{"className":"shiki github-light","style":{"backgroundColor":"#fff","color":"#24292e"},"tabindex":"0","children":[["$","code","4640",{"className":"language-c","children":[["$","span","4582",{"className":"line","children":[["$","span","4567",{"style":{"color":"#24292E"},"children":["\t t "]}],["$","span","4568",{"style":{"color":"#D73A49"},"children":["|="]}],["$","span","4569",{"style":{"color":"#24292E"},"children":[" ("]}],["$","span","4570",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4571",{"style":{"color":"#24292E"},"children":[")("]}],["$","span","4572",{"style":{"color":"#E36209"},"children":["SM4_S"]}],["$","span","4573",{"style":{"color":"#24292E"},"children":["[("]}],["$","span","4574",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4575",{"style":{"color":"#24292E"},"children":[") (X"]}],["$","span","4576",{"style":{"color":"#D73A49"},"children":[">>"]}],["$","span","4577",{"style":{"color":"#005CC5"},"children":["24"]}],["$","span","4578",{"style":{"color":"#24292E"},"children":[")]) "]}],["$","span","4579",{"style":{"color":"#D73A49"},"children":["<<"]}],["$","span","4580",{"style":{"color":"#005CC5"},"children":[" 24"]}],["$","span","4581",{"style":{"color":"#24292E"},"children":[";"]}]]}],"\n",["$","span","4598",{"className":"line","children":[["$","span","4583",{"style":{"color":"#24292E"},"children":["\t t "]}],["$","span","4584",{"style":{"color":"#D73A49"},"children":["|="]}],["$","span","4585",{"style":{"color":"#24292E"},"children":[" ("]}],["$","span","4586",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4587",{"style":{"color":"#24292E"},"children":[")("]}],["$","span","4588",{"style":{"color":"#E36209"},"children":["SM4_S"]}],["$","span","4589",{"style":{"color":"#24292E"},"children":["[("]}],["$","span","4590",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4591",{"style":{"color":"#24292E"},"children":[") (X"]}],["$","span","4592",{"style":{"color":"#D73A49"},"children":[">>"]}],["$","span","4593",{"style":{"color":"#005CC5"},"children":["16"]}],["$","span","4594",{"style":{"color":"#24292E"},"children":[")]) "]}],["$","span","4595",{"style":{"color":"#D73A49"},"children":["<<"]}],["$","span","4596",{"style":{"color":"#005CC5"},"children":[" 16"]}],["$","span","4597",{"style":{"color":"#24292E"},"children":[";"]}]]}],"\n",["$","span","4614",{"className":"line","children":[["$","span","4599",{"style":{"color":"#24292E"},"children":["\t t "]}],["$","span","4600",{"style":{"color":"#D73A49"},"children":["|="]}],["$","span","4601",{"style":{"color":"#24292E"},"children":[" ("]}],["$","span","4602",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4603",{"style":{"color":"#24292E"},"children":[")("]}],["$","span","4604",{"style":{"color":"#E36209"},"children":["SM4_S"]}],["$","span","4605",{"style":{"color":"#24292E"},"children":["[("]}],["$","span","4606",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4607",{"style":{"color":"#24292E"},"children":[") (X"]}],["$","span","4608",{"style":{"color":"#D73A49"},"children":[">>"]}],["$","span","4609",{"style":{"color":"#005CC5"},"children":["8"]}],["$","span","4610",{"style":{"color":"#24292E"},"children":[")]) "]}],["$","span","4611",{"style":{"color":"#D73A49"},"children":["<<"]}],["$","span","4612",{"style":{"color":"#005CC5"},"children":[" 8"]}],["$","span","4613",{"style":{"color":"#24292E"},"children":[";"]}]]}],"\n",["$","span","4624",{"className":"line","children":[["$","span","4615",{"style":{"color":"#24292E"},"children":["\t t "]}],["$","span","4616",{"style":{"color":"#D73A49"},"children":["|="]}],["$","span","4617",{"style":{"color":"#24292E"},"children":[" ("]}],["$","span","4618",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4619",{"style":{"color":"#24292E"},"children":[")"]}],["$","span","4620",{"style":{"color":"#E36209"},"children":["SM4_S"]}],["$","span","4621",{"style":{"color":"#24292E"},"children":["[("]}],["$","span","4622",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4623",{"style":{"color":"#24292E"},"children":[") X];"]}]]}],"\n",["$","span","4625",{"className":"line","children":[]}],"\n",["$","span","4639",{"className":"line","children":[["$","span","4626",{"style":{"color":"#24292E"},"children":[" t "]}],["$","span","4627",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4628",{"style":{"color":"#24292E"},"children":[" t "]}],["$","span","4629",{"style":{"color":"#D73A49"},"children":["^"]}],["$","span","4630",{"style":{"color":"#6F42C1"},"children":[" ROT32L"]}],["$","span","4631",{"style":{"color":"#24292E"},"children":["(t, "]}],["$","span","4632",{"style":{"color":"#005CC5"},"children":["13"]}],["$","span","4633",{"style":{"color":"#24292E"},"children":[") "]}],["$","span","4634",{"style":{"color":"#D73A49"},"children":["^"]}],["$","span","4635",{"style":{"color":"#6F42C1"},"children":[" ROT32L"]}],["$","span","4636",{"style":{"color":"#24292E"},"children":["(t, "]}],["$","span","4637",{"style":{"color":"#005CC5"},"children":["23"]}],["$","span","4638",{"style":{"color":"#24292E"},"children":[");"]}]]}]]}]]}],"\n",["$","p","4642",{"children":["如果不是提前读了 OpenSSL 的源码,我这里可能会选择强转指针而不是移位加或运算。相对强转指针这样的算法可读性明显更好。"]}],"\n",["$","$L1a","4643",{"id":"使用轮秘钥进行加密","children":["使用轮秘钥进行加密"]}],"\n",["$","p","4644",{"children":["加密的只要算法就是 SBox 的置换和 L 变换,文档都有详细说明了。"]}],"\n",["$","pre","4747",{"className":"shiki github-light","style":{"backgroundColor":"#fff","color":"#24292e"},"tabindex":"0","children":[["$","code","4746",{"className":"language-c","children":[["$","span","4653",{"className":"line","children":[["$","span","4645",{"style":{"color":"#D73A49"},"children":["static"]}],["$","span","4646",{"style":{"color":"#D73A49"},"children":[" inline"]}],["$","span","4647",{"style":{"color":"#D73A49"},"children":[" uint32_t"]}],["$","span","4648",{"style":{"color":"#6F42C1"},"children":[" SM4_T"]}],["$","span","4649",{"style":{"color":"#24292E"},"children":["("]}],["$","span","4650",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4651",{"style":{"color":"#E36209"},"children":[" X"]}],["$","span","4652",{"style":{"color":"#24292E"},"children":[") {"]}]]}],"\n",["$","span","4659",{"className":"line","children":[["$","span","4654",{"style":{"color":"#D73A49"},"children":["\tuint32_t"]}],["$","span","4655",{"style":{"color":"#24292E"},"children":[" t "]}],["$","span","4656",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4657",{"style":{"color":"#005CC5"},"children":[" 0"]}],["$","span","4658",{"style":{"color":"#24292E"},"children":[";"]}]]}],"\n",["$","span","4675",{"className":"line","children":[["$","span","4660",{"style":{"color":"#24292E"},"children":["\tt "]}],["$","span","4661",{"style":{"color":"#D73A49"},"children":["|="]}],["$","span","4662",{"style":{"color":"#24292E"},"children":[" (("]}],["$","span","4663",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4664",{"style":{"color":"#24292E"},"children":[")"]}],["$","span","4665",{"style":{"color":"#E36209"},"children":["SM4_S"]}],["$","span","4666",{"style":{"color":"#24292E"},"children":["[("]}],["$","span","4667",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4668",{"style":{"color":"#24292E"},"children":[")(X"]}],["$","span","4669",{"style":{"color":"#D73A49"},"children":[">>"]}],["$","span","4670",{"style":{"color":"#005CC5"},"children":["24"]}],["$","span","4671",{"style":{"color":"#24292E"},"children":[")]) "]}],["$","span","4672",{"style":{"color":"#D73A49"},"children":["<<"]}],["$","span","4673",{"style":{"color":"#005CC5"},"children":[" 24"]}],["$","span","4674",{"style":{"color":"#24292E"},"children":[";"]}]]}],"\n",["$","span","4691",{"className":"line","children":[["$","span","4676",{"style":{"color":"#24292E"},"children":["\tt "]}],["$","span","4677",{"style":{"color":"#D73A49"},"children":["|="]}],["$","span","4678",{"style":{"color":"#24292E"},"children":[" (("]}],["$","span","4679",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4680",{"style":{"color":"#24292E"},"children":[")"]}],["$","span","4681",{"style":{"color":"#E36209"},"children":["SM4_S"]}],["$","span","4682",{"style":{"color":"#24292E"},"children":["[("]}],["$","span","4683",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4684",{"style":{"color":"#24292E"},"children":[")(X"]}],["$","span","4685",{"style":{"color":"#D73A49"},"children":[">>"]}],["$","span","4686",{"style":{"color":"#005CC5"},"children":["16"]}],["$","span","4687",{"style":{"color":"#24292E"},"children":[")]) "]}],["$","span","4688",{"style":{"color":"#D73A49"},"children":["<<"]}],["$","span","4689",{"style":{"color":"#005CC5"},"children":[" 16"]}],["$","span","4690",{"style":{"color":"#24292E"},"children":[";"]}]]}],"\n",["$","span","4707",{"className":"line","children":[["$","span","4692",{"style":{"color":"#24292E"},"children":["\tt "]}],["$","span","4693",{"style":{"color":"#D73A49"},"children":["|="]}],["$","span","4694",{"style":{"color":"#24292E"},"children":[" (("]}],["$","span","4695",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4696",{"style":{"color":"#24292E"},"children":[")"]}],["$","span","4697",{"style":{"color":"#E36209"},"children":["SM4_S"]}],["$","span","4698",{"style":{"color":"#24292E"},"children":["[("]}],["$","span","4699",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4700",{"style":{"color":"#24292E"},"children":[")(X"]}],["$","span","4701",{"style":{"color":"#D73A49"},"children":[">>"]}],["$","span","4702",{"style":{"color":"#005CC5"},"children":["8"]}],["$","span","4703",{"style":{"color":"#24292E"},"children":[")]) "]}],["$","span","4704",{"style":{"color":"#D73A49"},"children":["<<"]}],["$","span","4705",{"style":{"color":"#005CC5"},"children":[" 8"]}],["$","span","4706",{"style":{"color":"#24292E"},"children":[";"]}]]}],"\n",["$","span","4717",{"className":"line","children":[["$","span","4708",{"style":{"color":"#24292E"},"children":["\tt "]}],["$","span","4709",{"style":{"color":"#D73A49"},"children":["|="]}],["$","span","4710",{"style":{"color":"#24292E"},"children":[" (("]}],["$","span","4711",{"style":{"color":"#D73A49"},"children":["uint32_t"]}],["$","span","4712",{"style":{"color":"#24292E"},"children":[")"]}],["$","span","4713",{"style":{"color":"#E36209"},"children":["SM4_S"]}],["$","span","4714",{"style":{"color":"#24292E"},"children":["[("]}],["$","span","4715",{"style":{"color":"#D73A49"},"children":["uint8_t"]}],["$","span","4716",{"style":{"color":"#24292E"},"children":[")(X)]);"]}]]}],"\n",["$","span","4739",{"className":"line","children":[["$","span","4718",{"style":{"color":"#24292E"},"children":["\tt "]}],["$","span","4719",{"style":{"color":"#D73A49"},"children":["^="]}],["$","span","4720",{"style":{"color":"#6F42C1"},"children":[" ROT32L"]}],["$","span","4721",{"style":{"color":"#24292E"},"children":["(t, "]}],["$","span","4722",{"style":{"color":"#005CC5"},"children":["2"]}],["$","span","4723",{"style":{"color":"#24292E"},"children":[") "]}],["$","span","4724",{"style":{"color":"#D73A49"},"children":["^"]}],["$","span","4725",{"style":{"color":"#6F42C1"},"children":[" ROT32L"]}],["$","span","4726",{"style":{"color":"#24292E"},"children":["(t, "]}],["$","span","4727",{"style":{"color":"#005CC5"},"children":["10"]}],["$","span","4728",{"style":{"color":"#24292E"},"children":[") "]}],["$","span","4729",{"style":{"color":"#D73A49"},"children":["^"]}],["$","span","4730",{"style":{"color":"#6F42C1"},"children":[" ROT32L"]}],["$","span","4731",{"style":{"color":"#24292E"},"children":["(t, "]}],["$","span","4732",{"style":{"color":"#005CC5"},"children":["18"]}],["$","span","4733",{"style":{"color":"#24292E"},"children":[") "]}],["$","span","4734",{"style":{"color":"#D73A49"},"children":["^"]}],["$","span","4735",{"style":{"color":"#6F42C1"},"children":[" ROT32L"]}],["$","span","4736",{"style":{"color":"#24292E"},"children":["(t, "]}],["$","span","4737",{"style":{"color":"#005CC5"},"children":["24"]}],["$","span","4738",{"style":{"color":"#24292E"},"children":[");"]}]]}],"\n",["$","span","4742",{"className":"line","children":[["$","span","4740",{"style":{"color":"#D73A49"},"children":["\treturn"]}],["$","span","4741",{"style":{"color":"#24292E"},"children":[" t;"]}]]}],"\n",["$","span","4743",{"className":"line","children":[]}],"\n",["$","span","4745",{"className":"line","children":[["$","span","4744",{"style":{"color":"#24292E"},"children":["}"]}]]}]]}]]}],"\n",["$","p","4748",{"children":["可以看到算法与轮秘钥生成算法是一样的,只是最后的 L 变换有所区别。"]}],"\n",["$","$L1a","4749",{"id":"解密算法","children":["解密算法"]}],"\n",["$","p","4750",{"children":["SM4 解密算法与加密算法一致,只是使用轮秘钥的时候倒序使用"]}],"\n",["$","$L17","4751",{"id":"总结","children":["总结"]}],"\n",["$","p","4753",{"children":["算法实现不难。在如何提升速度方面 OpenSSL 给了一个很好的示范。它先把一个字 (32bit) 的 SBox 的置换和 L 置换的结果","$L1b","。"]}],"\n",["$","$L17","4754",{"id":"实际应用-加密文件","children":["实际应用 - 加密文件"]}],"\n",["$","$L1a","4755",{"id":"Padding","children":["Padding"]}],"\n",["$","p","4757",{"children":["实际应用过程中的 message 不一定是 128bit 整数倍的,所以需要进行 padding。padding 要考虑的东西主要是要让 padding 能够被识别出来。由于输入是二进制流所以不能通过 Magic bytes 的形式区分。在网上找相关的资料可以查到不少","$L1c","。"]}],"\n",["$","p","4758",{"children":["在这里选用 PKCS#5 算法。该算法就是将不足 16byte 的块填充成 16byte,其中填充的每一块的数值都是 padding 的长度。一个特例是,当填充恰好为 16byte 的块的时候仍需填充额外的 16byte。"]}],"\n",["$","p","4759",{"children":["举个例子。"]}],"\n",["$","pre","4771",{"className":"shiki github-light","style":{"backgroundColor":"#fff","color":"#24292e"},"tabindex":"0","children":[["$","code","4770",{"className":"language-text","children":[["$","span","4761",{"className":"line","children":[["$","span","4760",{"children":["// 加入最后一块为"]}]]}],"\n",["$","span","4763",{"className":"line","children":[["$","span","4762",{"children":["0x12 0x34 0x56"]}]]}],"\n",["$","span","4765",{"className":"line","children":[["$","span","4764",{"children":["则填充长度为 16-3=13 0x0d"]}]]}],"\n",["$","span","4767",{"className":"line","children":[["$","span","4766",{"children":["0x12 0x34 0x56 0x0d 0x0d 0x0d 0x0d 0x0d 0x0d 0x0d 0x0d 0x0d 0x0d 0x0d 0x0d 0x0d"]}]]}],"\n",["$","span","4769",{"className":"line","children":[["$","span","4768",{"children":["填充恰好 16byte 的块时,仍填充 16bytes 的 0x10"]}]]}]]}]]}],"\n",["$","p","4772",{"children":["额外填充的目的是确保解密后可以准确将 padding 移除而没有歧义。"]}],"\n",["$","$L1a","4773",{"id":"CBC","children":["CBC"]}],"\n",["$","p","4774",{"children":["CBC 从英文全称上去理解(Cipher Block Chaining)这种模式下每一个分组的明文经过 cipher 加密之前还会和前一个分组加密后的密文异或。第一个分组则会和一个随机生成的 IV 进行异或。"]}],"\n",["$","p","4776",{"children":[["$","$La",null,{"className":"m-img","id":"$undefined","alt":"cbc_enc","style":"$undefined","loading":"lazy","src":"https://vip2.loli.io/2023/03/08/svHPeIj52g7oYGt.png","width":865,"height":350,"placeholder":"blur","blurDataURL":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAACCAIAAAAfCIEKAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAKElEQVR4nGOorq62tLT08PDw9fUNCQlheP36taWlpaampqWlZW9vLwCutgsR7ZiQSQAAAABJRU5ErkJggg=="}]]}],"\n",["$","p","4777",{"children":["解密的时候则会将解密后的输出与前一个分组的密文或者 IV 进行异或得到明文。"]}],"\n",["$","p","4779",{"children":[["$","$La",null,{"className":"m-img","id":"$undefined","alt":"cbc_dec","style":"$undefined","loading":"lazy","src":"https://vip2.loli.io/2023/03/08/JvS9nBRUo6mZEzG.png","width":865,"height":320,"placeholder":"blur","blurDataURL":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAACCAIAAAAfCIEKAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAJUlEQVR4nGOorq728PCorq7OzMyMjIxk+P//v6ampjQYZGZmAgDHDQtWnr0D6AAAAABJRU5ErkJggg=="}]]}],"\n",["$","p","4780",{"children":["CBC 模式的优点在于对于重复的明文加密出来的块因为经过上一个块的异或得到的密文会不一样。这样输入与明文没有固定的关系。"]}],"\n",["$","$L1a","4781",{"id":"具体实现-2","children":["具体实现"]}],"\n",["$","p","4784",{"children":["首先我们生成的 IV 要是密码安全的 random bytes。Uinx 下应该读",["$","code","4782",{"children":["/dev/urandom"]}],"可以做到,但是为了能够 Protable 用了一个库","$L1d"]}],"\n",["$","p","4789",{"children":["下面看实现代码,之前有同学问我大文件怎么读。我的想法是只要获取了文件的长度,然后用流的方式一次进一部分就可以了。读到最后一块的时候再来 padding。读取文件长度可以通过",["$","code","4785",{"children":["fseek"]}],"+",["$","code","4786",{"children":["ftell"]}],"来实现。",["$","br","4787",{}],"\n需要注意的是大文件(实测 4G 左右)的时候","$L1e",",我暂时找不到很好的解决方式。有大佬知道的话还请指点一下。"]}],"\n",["$","pre","4974",{"className":"shiki github-light","style":{"backgroundColor":"#fff","color":"#24292e"},"tabindex":"0","children":[["$","code","4973",{"className":"language-c","children":[["$","span","4802",{"className":"line","children":[["$","span","4790",{"style":{"color":"#D73A49"},"children":["void"]}],["$","span","4791",{"style":{"color":"#6F42C1"},"children":[" sm4_cbc_encrypt_file"]}],["$","span","4792",{"style":{"color":"#24292E"},"children":["(FILE "]}],["$","span","4793",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4794",{"style":{"color":"#E36209"},"children":["in"]}],["$","span","4795",{"style":{"color":"#24292E"},"children":[", FILE "]}],["$","span","4796",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4797",{"style":{"color":"#E36209"},"children":["out"]}],["$","span","4798",{"style":{"color":"#24292E"},"children":[", sm4_ctx "]}],["$","span","4799",{"style":{"color":"#D73A49"},"children":["*"]}],["$","span","4800",{"style":{"color":"#E36209"},"children":["ctx"]}],["$","span","4801",{"style":{"color":"#24292E"},"children":[") {"]}]]}],"\n",["$","span","4806",{"className":"line","children":[["$","span","4803",{"style":{"color":"#D73A49"},"children":["\tuint8_t"]}],["$","span","4804",{"style":{"color":"#E36209"},"children":[" iv"]}],["$","span","4805",{"style":{"color":"#24292E"},"children":["[SM4_BLOCK_SIZE];"]}]]}],"\n",["$","span","4812",{"className":"line","children":[["$","span","4807",{"style":{"color":"#D73A49"},"children":["\tuint8_t"]}],["$","span","4808",{"style":{"color":"#E36209"},"children":[" buf"]}],["$","span","4809",{"style":{"color":"#24292E"},"children":["[SM4_BLOCK_SIZE], "]}],["$","span","4810",{"style":{"color":"#E36209"},"children":["out_buf"]}],["$","span","4811",{"style":{"color":"#24292E"},"children":["[SM4_BLOCK_SIZE];"]}]]}],"\n",["$","span","4813",{"className":"line","children":[]}],"\n",["$","span","4819",{"className":"line","children":[["$","span","4814",{"style":{"color":"#D73A49"},"children":["\tlong"]}],["$","span","4815",{"style":{"color":"#24292E"},"children":[" sz, nremain, nread"]}],["$","span","4816",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4817",{"style":{"color":"#005CC5"},"children":["0"]}],["$","span","4818",{"style":{"color":"#24292E"},"children":[", padding_byte_len;"]}]]}],"\n",["$","span","4824",{"className":"line","children":[["$","span","4820",{"style":{"color":"#6F42C1"},"children":["\tfseek"]}],["$","span","4821",{"style":{"color":"#24292E"},"children":["(in, "]}],["$","span","4822",{"style":{"color":"#005CC5"},"children":["0"]}],["$","span","4823",{"style":{"color":"#24292E"},"children":[", SEEK_END);"]}]]}],"\n",["$","span","4829",{"className":"line","children":[["$","span","4825",{"style":{"color":"#24292E"},"children":["\tsz "]}],["$","span","4826",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4827",{"style":{"color":"#6F42C1"},"children":[" ftell"]}],["$","span","4828",{"style":{"color":"#24292E"},"children":["(in);"]}]]}],"\n",["$","span","4833",{"className":"line","children":[["$","span","4830",{"style":{"color":"#24292E"},"children":["\tnremain "]}],["$","span","4831",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4832",{"style":{"color":"#24292E"},"children":[" sz;"]}]]}],"\n",["$","span","4836",{"className":"line","children":[["$","span","4834",{"style":{"color":"#6F42C1"},"children":["\trewind"]}],["$","span","4835",{"style":{"color":"#24292E"},"children":["(in);"]}]]}],"\n",["$","span","4837",{"className":"line","children":[]}],"\n",["$","span","4839",{"className":"line","children":[["$","span","4838",{"style":{"color":"#6A737D"},"children":[" // 将 IV 至于加密输出的文件开头 16byte"]}]]}],"\n",["$","span","4842",{"className":"line","children":[["$","span","4840",{"style":{"color":"#6F42C1"},"children":["\trandombytes"]}],["$","span","4841",{"style":{"color":"#24292E"},"children":["(iv, SM4_BLOCK_SIZE);"]}]]}],"\n",["$","span","4847",{"className":"line","children":[["$","span","4843",{"style":{"color":"#6F42C1"},"children":["\tfwrite"]}],["$","span","4844",{"style":{"color":"#24292E"},"children":["(iv, "]}],["$","span","4845",{"style":{"color":"#005CC5"},"children":["1"]}],["$","span","4846",{"style":{"color":"#24292E"},"children":[", SM4_BLOCK_SIZE, out);"]}]]}],"\n",["$","span","4848",{"className":"line","children":[]}],"\n",["$","span","4854",{"className":"line","children":[["$","span","4849",{"style":{"color":"#D73A49"},"children":["\tint"]}],["$","span","4850",{"style":{"color":"#24292E"},"children":[" n"]}],["$","span","4851",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4852",{"style":{"color":"#005CC5"},"children":["0"]}],["$","span","4853",{"style":{"color":"#24292E"},"children":[";"]}]]}],"\n",["$","span","4857",{"className":"line","children":[["$","span","4855",{"style":{"color":"#6F42C1"},"children":["\tmemcpy"]}],["$","span","4856",{"style":{"color":"#24292E"},"children":["(out_buf, iv, SM4_BLOCK_SIZE);"]}]]}],"\n",["$","span","4862",{"className":"line","children":[["$","span","4858",{"style":{"color":"#D73A49"},"children":["\twhile"]}],["$","span","4859",{"style":{"color":"#24292E"},"children":["(nremain "]}],["$","span","4860",{"style":{"color":"#D73A49"},"children":[">="]}],["$","span","4861",{"style":{"color":"#24292E"},"children":[" SM4_BLOCK_SIZE) {"]}]]}],"\n",["$","span","4869",{"className":"line","children":[["$","span","4863",{"style":{"color":"#24292E"},"children":["\t\tn "]}],["$","span","4864",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4865",{"style":{"color":"#6F42C1"},"children":[" fread"]}],["$","span","4866",{"style":{"color":"#24292E"},"children":["(buf, "]}],["$","span","4867",{"style":{"color":"#005CC5"},"children":["1"]}],["$","span","4868",{"style":{"color":"#24292E"},"children":[", SM4_BLOCK_SIZE, in);"]}]]}],"\n",["$","span","4873",{"className":"line","children":[["$","span","4870",{"style":{"color":"#24292E"},"children":["\t\tnremain "]}],["$","span","4871",{"style":{"color":"#D73A49"},"children":["-="]}],["$","span","4872",{"style":{"color":"#24292E"},"children":["n;"]}]]}],"\n",["$","span","4877",{"className":"line","children":[["$","span","4874",{"style":{"color":"#24292E"},"children":["\t\tnread "]}],["$","span","4875",{"style":{"color":"#D73A49"},"children":["+="]}],["$","span","4876",{"style":{"color":"#24292E"},"children":[" n;"]}]]}],"\n",["$","span","4879",{"className":"line","children":[["$","span","4878",{"style":{"color":"#6A737D"},"children":[" // SM4 CBC 模式加密"]}]]}],"\n",["$","span","4882",{"className":"line","children":[["$","span","4880",{"style":{"color":"#6F42C1"},"children":["\t\txor_blk"]}],["$","span","4881",{"style":{"color":"#24292E"},"children":["(buf, out_buf);"]}]]}],"\n",["$","span","4885",{"className":"line","children":[["$","span","4883",{"style":{"color":"#6F42C1"},"children":["\t\tsm4_encrypt"]}],["$","span","4884",{"style":{"color":"#24292E"},"children":["(buf, out_buf, ctx);"]}]]}],"\n",["$","span","4890",{"className":"line","children":[["$","span","4886",{"style":{"color":"#6F42C1"},"children":["\t\tfwrite"]}],["$","span","4887",{"style":{"color":"#24292E"},"children":["(out_buf, "]}],["$","span","4888",{"style":{"color":"#005CC5"},"children":["1"]}],["$","span","4889",{"style":{"color":"#24292E"},"children":[", SM4_BLOCK_SIZE, out);"]}]]}],"\n",["$","span","4892",{"className":"line","children":[["$","span","4891",{"style":{"color":"#24292E"},"children":["\t}"]}]]}],"\n",["$","span","4893",{"className":"line","children":[]}],"\n",["$","span","4899",{"className":"line","children":[["$","span","4894",{"style":{"color":"#24292E"},"children":["\tpadding_byte_len "]}],["$","span","4895",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4896",{"style":{"color":"#24292E"},"children":[" SM4_BLOCK_SIZE "]}],["$","span","4897",{"style":{"color":"#D73A49"},"children":["-"]}],["$","span","4898",{"style":{"color":"#24292E"},"children":[" nremain;"]}]]}],"\n",["$","span","4900",{"className":"line","children":[]}],"\n",["$","span","4905",{"className":"line","children":[["$","span","4901",{"style":{"color":"#6F42C1"},"children":["\tmemset"]}],["$","span","4902",{"style":{"color":"#24292E"},"children":["(buf, "]}],["$","span","4903",{"style":{"color":"#005CC5"},"children":["0"]}],["$","span","4904",{"style":{"color":"#24292E"},"children":[", SM4_BLOCK_SIZE);"]}]]}],"\n",["$","span","4912",{"className":"line","children":[["$","span","4906",{"style":{"color":"#24292E"},"children":["\tn "]}],["$","span","4907",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4908",{"style":{"color":"#6F42C1"},"children":[" fread"]}],["$","span","4909",{"style":{"color":"#24292E"},"children":["(buf, "]}],["$","span","4910",{"style":{"color":"#005CC5"},"children":["1"]}],["$","span","4911",{"style":{"color":"#24292E"},"children":[", nremain, in);"]}]]}],"\n",["$","span","4916",{"className":"line","children":[["$","span","4913",{"style":{"color":"#24292E"},"children":["\tnremain "]}],["$","span","4914",{"style":{"color":"#D73A49"},"children":["-="]}],["$","span","4915",{"style":{"color":"#24292E"},"children":[" n;"]}]]}],"\n",["$","span","4920",{"className":"line","children":[["$","span","4917",{"style":{"color":"#24292E"},"children":["\tnread "]}],["$","span","4918",{"style":{"color":"#D73A49"},"children":["+="]}],["$","span","4919",{"style":{"color":"#24292E"},"children":[" n;"]}]]}],"\n",["$","span","4921",{"className":"line","children":[]}],"\n",["$","span","4923",{"className":"line","children":[["$","span","4922",{"style":{"color":"#6A737D"},"children":[" // 最后一块 padding 并且加密输出"]}]]}],"\n",["$","span","4928",{"className":"line","children":[["$","span","4924",{"style":{"color":"#D73A49"},"children":["\tif"]}],["$","span","4925",{"style":{"color":"#24292E"},"children":["(padding_byte_len "]}],["$","span","4926",{"style":{"color":"#D73A49"},"children":["!="]}],["$","span","4927",{"style":{"color":"#24292E"},"children":[" SM4_BLOCK_SIZE) {"]}]]}],"\n",["$","span","4933",{"className":"line","children":[["$","span","4929",{"style":{"color":"#6F42C1"},"children":["\t\tfread"]}],["$","span","4930",{"style":{"color":"#24292E"},"children":["(buf, "]}],["$","span","4931",{"style":{"color":"#005CC5"},"children":["1"]}],["$","span","4932",{"style":{"color":"#24292E"},"children":[", nremain, in);"]}]]}],"\n",["$","span","4938",{"className":"line","children":[["$","span","4934",{"style":{"color":"#6F42C1"},"children":["\t\tmemset"]}],["$","span","4935",{"style":{"color":"#24292E"},"children":["(buf"]}],["$","span","4936",{"style":{"color":"#D73A49"},"children":["+"]}],["$","span","4937",{"style":{"color":"#24292E"},"children":["n, padding_byte_len, padding_byte_len);"]}]]}],"\n",["$","span","4941",{"className":"line","children":[["$","span","4939",{"style":{"color":"#6F42C1"},"children":["\t\txor_blk"]}],["$","span","4940",{"style":{"color":"#24292E"},"children":["(buf, out_buf);"]}]]}],"\n",["$","span","4944",{"className":"line","children":[["$","span","4942",{"style":{"color":"#6F42C1"},"children":["\t\tsm4_encrypt"]}],["$","span","4943",{"style":{"color":"#24292E"},"children":["(buf, out_buf, ctx);"]}]]}],"\n",["$","span","4949",{"className":"line","children":[["$","span","4945",{"style":{"color":"#6F42C1"},"children":["\t\tfwrite"]}],["$","span","4946",{"style":{"color":"#24292E"},"children":["(out_buf, "]}],["$","span","4947",{"style":{"color":"#005CC5"},"children":["1"]}],["$","span","4948",{"style":{"color":"#24292E"},"children":[", SM4_BLOCK_SIZE, out);"]}]]}],"\n",["$","span","4953",{"className":"line","children":[["$","span","4950",{"style":{"color":"#24292E"},"children":["\t} "]}],["$","span","4951",{"style":{"color":"#D73A49"},"children":["else"]}],["$","span","4952",{"style":{"color":"#24292E"},"children":[" {"]}]]}],"\n",["$","span","4956",{"className":"line","children":[["$","span","4954",{"style":{"color":"#6F42C1"},"children":["\t\tmemset"]}],["$","span","4955",{"style":{"color":"#24292E"},"children":["(buf, SM4_BLOCK_SIZE, SM4_BLOCK_SIZE);"]}]]}],"\n",["$","span","4959",{"className":"line","children":[["$","span","4957",{"style":{"color":"#6F42C1"},"children":["\t\txor_blk"]}],["$","span","4958",{"style":{"color":"#24292E"},"children":["(buf, out_buf);"]}]]}],"\n",["$","span","4962",{"className":"line","children":[["$","span","4960",{"style":{"color":"#6F42C1"},"children":["\t\tsm4_encrypt"]}],["$","span","4961",{"style":{"color":"#24292E"},"children":["(buf, out_buf, ctx);"]}]]}],"\n",["$","span","4967",{"className":"line","children":[["$","span","4963",{"style":{"color":"#6F42C1"},"children":["\t\tfwrite"]}],["$","span","4964",{"style":{"color":"#24292E"},"children":["(out_buf, "]}],["$","span","4965",{"style":{"color":"#005CC5"},"children":["1"]}],["$","span","4966",{"style":{"color":"#24292E"},"children":[", SM4_BLOCK_SIZE, out);"]}]]}],"\n",["$","span","4969",{"className":"line","children":[["$","span","4968",{"style":{"color":"#24292E"},"children":["\t}"]}]]}],"\n",["$","span","4970",{"className":"line","children":[]}],"\n",["$","span","4972",{"className":"line","children":[["$","span","4971",{"style":{"color":"#24292E"},"children":["}"]}]]}]]}]]}],"\n",["$","p","4975",{"children":["解密的操作类似,知道了文件头 16byte 为 IV,先读出来,然后依次读取并且解密。因为是经过 padding 的所以最后一块一定是有 pading,而且最后一 byte 一定是 padding 长度。然后输出非 padding 部分的明文即可"]}],"\n",["$","pre","5006",{"className":"shiki github-light","style":{"backgroundColor":"#fff","color":"#24292e"},"tabindex":"0","children":[["$","code","5005",{"className":"language-c","children":[["$","span","4982",{"className":"line","children":[["$","span","4976",{"style":{"color":"#24292E"},"children":["n "]}],["$","span","4977",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4978",{"style":{"color":"#6F42C1"},"children":[" fread"]}],["$","span","4979",{"style":{"color":"#24292E"},"children":["(buf, "]}],["$","span","4980",{"style":{"color":"#005CC5"},"children":["1"]}],["$","span","4981",{"style":{"color":"#24292E"},"children":[", SM4_BLOCK_SIZE, in);"]}]]}],"\n",["$","span","4985",{"className":"line","children":[["$","span","4983",{"style":{"color":"#6F42C1"},"children":["sm4_decrypt"]}],["$","span","4984",{"style":{"color":"#24292E"},"children":["(buf, out_buf, ctx);"]}]]}],"\n",["$","span","4988",{"className":"line","children":[["$","span","4986",{"style":{"color":"#6F42C1"},"children":["xor_blk"]}],["$","span","4987",{"style":{"color":"#24292E"},"children":["(out_buf, iv);"]}]]}],"\n",["$","span","4997",{"className":"line","children":[["$","span","4989",{"style":{"color":"#D73A49"},"children":["long"]}],["$","span","4990",{"style":{"color":"#24292E"},"children":[" padding_byte_len "]}],["$","span","4991",{"style":{"color":"#D73A49"},"children":["="]}],["$","span","4992",{"style":{"color":"#E36209"},"children":[" out_buf"]}],["$","span","4993",{"style":{"color":"#24292E"},"children":["[SM4_BLOCK_SIZE"]}],["$","span","4994",{"style":{"color":"#D73A49"},"children":["-"]}],["$","span","4995",{"style":{"color":"#005CC5"},"children":["1"]}],["$","span","4996",{"style":{"color":"#24292E"},"children":["];"]}]]}],"\n",["$","span","5004",{"className":"line","children":[["$","span","4998",{"style":{"color":"#6F42C1"},"children":["fwrite"]}],["$","span","4999",{"style":{"color":"#24292E"},"children":["(out_buf, "]}],["$","span","5000",{"style":{"color":"#005CC5"},"children":["1"]}],["$","span","5001",{"style":{"color":"#24292E"},"children":[", SM4_BLOCK_SIZE"]}],["$","span","5002",{"style":{"color":"#D73A49"},"children":["-"]}],["$","span","5003",{"style":{"color":"#24292E"},"children":["padding_byte_len, out);"]}]]}]]}]]}],"\n",["$","$L17","5007",{"id":"体会","children":["体会"]}],"\n",["$","p","5008",{"children":["学了点对称分组密码的基本结构还有具体实现吧。OpenSSL 为我节省了不少功夫少走的不少弯路。O (∩_∩) O 哈哈~"]}],"\n",["$","$L17","5009",{"id":"参考文章","children":["参考文章"]}],"\n",["$","ul","5018",{"children":["\n",["$","li","5011",{"children":["$L1f"]}],"\n",["$","li","5013",{"children":["$L20"]}],"\n",["$","li","5015",{"children":["$L21"]}],"\n",["$","li","5017",{"children":["$L22"]}],"\n"]}],"\n"]}] 15:["$","$L9",null,{"href":"https://www.fengkx.top/post/sm3-implementing/","children":["SM3"]}] 16:["$","a",null,{"href":"http://www.sca.gov.cn/sca/xwdt/2012-03/21/content_1002392.shtml","children":["SM4"],"target":"_blank","rel":"noopener noreferrer external nofollow"}] 18:["$","a",null,{"href":"https://github.com/fengkx/sm3","children":["实现了 SM3 算法"],"target":"_blank","rel":"noopener noreferrer external nofollow"}] 19:["$","a",null,{"href":"https://github.com/fengkx/sm4","children":["SM4 算法"],"target":"_blank","rel":"noopener noreferrer external nofollow"}] 1b:["$","a",null,{"href":"https://github.com/openssl/openssl/blob/master/crypto/sm4/sm4.c#L43-L86","children":["提前算了出来"],"target":"_blank","rel":"noopener noreferrer external nofollow"}] 1c:["$","a",null,{"href":"http://www.demonk.cn/2018/07/02/aes-padding-native-java/","children":["padding 的算法"],"target":"_blank","rel":"noopener noreferrer external nofollow"}] 1d:["$","a",null,{"href":"https://github.com/dsprenkels/randombytes/","children":["randombytes"],"target":"_blank","rel":"noopener noreferrer external nofollow"}] 1e:["$","a",null,{"href":"https://stackoverflow.com/questions/24399015/fseek-with-seek-end-returns-a-invalid-argument-error-to-manage-large-data7gb?tdsourcetag=s_pctim_aiomsg","children":["MinGW 编译的 fseek 会出错"],"target":"_blank","rel":"noopener noreferrer external nofollow"}] 1f:["$","a",null,{"href":"https://github.com/fengkx/sm4/blob/master/SM4.pdf","children":["SM4 文档"],"target":"_blank","rel":"noopener noreferrer external nofollow"}] 20:["$","a",null,{"href":"https://neuqzxy.github.io/2017/06/15/%E6%AC%A3%E4%BB%94%E5%B8%A6%E4%BD%A0%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%85%A5%E9%97%A8SM4%E5%8A%A0%E5%AF%86%E7%AE%97%E6%B3%95/","children":["欣仔带你零基础入门 SM4 加密算法"],"target":"_blank","rel":"noopener noreferrer external nofollow"}] 21:["$","a",null,{"href":"http://www.demonk.cn/2018/07/02/aes-padding-native-java/","children":["AES 加密简介 —— 填充算法"],"target":"_blank","rel":"noopener noreferrer external nofollow"}] 22:["$","a",null,{"href":"https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#CBC","children":["CBC wiki"],"target":"_blank","rel":"noopener noreferrer external nofollow"}]