{"id":1270,"date":"2026-01-06T17:31:15","date_gmt":"2026-01-06T09:31:15","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1270"},"modified":"2026-01-06T17:31:15","modified_gmt":"2026-01-06T09:31:15","slug":"php-sm4%e5%8a%a0%e5%af%86%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/01\/06\/php-sm4%e5%8a%a0%e5%af%86%e7%9a%84%e5%ae%9e%e7%8e%b0\/","title":{"rendered":"PHP\u00a0SM4\u52a0\u5bc6\u7684\u5b9e\u73b0"},"content":{"rendered":"\n<p><strong>SM4<\/strong>\uff08\u5546\u7528\u5bc6\u7801\u7b97\u6cd5\uff09\u662f\u4e2d\u56fd\u56fd\u5bb6\u5bc6\u7801\u7b97\u6cd5\u6807\u51c6\u4e4b\u4e00\uff0c\u5e7f\u6cdb\u7528\u4e8e\u5bf9\u79f0\u52a0\u5bc6\u3002PHP \u9ed8\u8ba4\u6ca1\u6709\u5185\u5efa\u652f\u6301 SM4 \u52a0\u5bc6\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528 <strong>\u6269\u5c55\u5e93<\/strong> \u6216 <strong>\u624b\u52a8\u5b9e\u73b0<\/strong> \u6765\u8fdb\u884c\u52a0\u5bc6\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0 <strong>SM4 \u52a0\u5bc6<\/strong> \u5728 PHP \u4e2d\u7684\u5e94\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001\u4f7f\u7528 <strong>SM4<\/strong> \u52a0\u5bc6\u5e93 (\u63a8\u8350\u65b9\u5f0f)<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u5b89\u88c5 <code>sm4<\/code> \u6269\u5c55<\/h4>\n\n\n\n<p>\u5728 PHP \u4e2d\uff0c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u73b0\u6210\u7684 <strong>SM4 \u52a0\u5bc6\u5e93<\/strong>\u3002\u53ef\u4ee5\u4f7f\u7528 <strong><code>php-sm4<\/code><\/strong> \u8fd9\u4e2a\u6269\u5c55\uff0c\u5b83\u4e3a PHP \u63d0\u4f9b\u4e86\u5bf9 SM4 \u52a0\u5bc6\u7b97\u6cd5\u7684\u652f\u6301\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7 <strong>Composer<\/strong> \u5b89\u88c5\u5b83\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncomposer require &quot;sm4\/sm4&quot;\n\n<\/pre><\/div>\n\n\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0 SM4 \u52a0\u5bc6\u548c\u89e3\u5bc6\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u4f7f\u7528 <code>php-sm4<\/code> \u5e93\u793a\u4f8b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\n\nrequire &#039;vendor\/autoload.php&#039;;\n\nuse Sm4\\Sm4;\n\n$key = &quot;1234567890abcdef&quot;;  \/\/ 128 \u4f4d\u5bc6\u94a5\uff0c16 \u5b57\u8282\n$plaintext = &quot;Hello, SM4 Encryption!&quot;;\n\n\/\/ SM4 \u52a0\u5bc6\n$sm4 = new Sm4();\n$ciphertext = $sm4-&gt;encrypt($plaintext, $key);\necho &quot;Ciphertext (Base64): &quot; . base64_encode($ciphertext) . &quot;\\n&quot;;\n\n\/\/ SM4 \u89e3\u5bc6\n$decrypted = $sm4-&gt;decrypt($ciphertext, $key);\necho &quot;Decrypted text: &quot; . $decrypted . &quot;\\n&quot;;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u624b\u52a8\u5b9e\u73b0 SM4 \u52a0\u5bc6<\/h3>\n\n\n\n<p>\u5982\u679c\u4e0d\u60f3\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u53c2\u8003 <strong>SM4 \u7684\u7b97\u6cd5\u89c4\u8303<\/strong> \u6765\u624b\u52a8\u5b9e\u73b0\u3002\u4e0d\u8fc7\uff0c\u8fd9\u6837\u4f1a\u6bd4\u8f83\u7e41\u7410\uff0c\u9700\u8981\u6309\u7167 SM4 \u6807\u51c6\u8fdb\u884c\u8be6\u7ec6\u7f16\u7801\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. SM4 \u7b97\u6cd5\u7b80\u8ff0<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SM4 \u4f7f\u7528 128 \u4f4d\uff0816 \u5b57\u8282\uff09\u5bc6\u94a5\uff0c\u5206\u7ec4\u5927\u5c0f\u4e3a 128 \u4f4d\uff0816 \u5b57\u8282\uff09\u3002<\/li>\n\n\n\n<li>SM4 \u52a0\u5bc6\u8fc7\u7a0b\u5206\u4e3a\u591a\u4e2a\u8f6e\u6b21\uff0c\u6bcf\u8f6e\u7684\u5bc6\u94a5\u901a\u8fc7 <strong>\u6269\u5c55\u7b97\u6cd5<\/strong> \u5f97\u5230\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. SM4 \u52a0\u5bc6\u8fc7\u7a0b<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5bc6\u94a5\u6269\u5c55<\/strong>\uff1a\u5c06 128 \u4f4d\u7684\u5bc6\u94a5\u901a\u8fc7\u7279\u5b9a\u7684\u7b97\u6cd5\u8f6c\u6362\u4e3a\u591a\u4e2a\u5b50\u5bc6\u94a5\u3002<\/li>\n\n\n\n<li><strong>\u52a0\u5bc6\u8fc7\u7a0b<\/strong>\uff1a\u6bcf\u4e2a\u6570\u636e\u5757\u4e0e\u8f6e\u5b50\u5bc6\u94a5\u8fdb\u884c\u8fd0\u7b97\uff0c\u9010\u6b65\u52a0\u5bc6\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u57fa\u4e8e PHP \u624b\u52a8\u5b9e\u73b0\u7684 SM4 \u52a0\u5bc6\u793a\u4f8b<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\n\nclass SM4 {\n    \/\/ SM4 \u76f8\u5173\u5e38\u91cf\u548c S \u76d2\u8868\n    private static $SBox = &#x5B;\n        \/\/ S-box \u8868\n    ];\n\n    private static $FK = &#x5B;0xA3B1BAC6, 0x56AA3350, 0x677D9197, 0xB27022DC];  \/\/ \u56fa\u5b9a\u5e38\u91cf\n    private static $CK = &#x5B;\n        \/\/ \u5e38\u91cf Ck\n    ];\n\n    public static function encrypt($data, $key) {\n        \/\/ \u6570\u636e\u586b\u5145\n        $data = self::pad($data);\n        \n        \/\/ \u5bc6\u94a5\u6269\u5c55\n        $roundKeys = self::keyExpansion($key);\n        \n        \/\/ \u521d\u59cb\u72b6\u6001\n        $state = self::convertToState($data);\n        \n        \/\/ \u8f6e\u6b21\u52a0\u5bc6\n        for ($i = 0; $i &amp;lt; 32; $i++) {\n            $state = self::roundFunction($state, $roundKeys&#x5B;$i]);\n        }\n\n        return self::convertToString($state);\n    }\n\n    \/\/ \u8865\u5168\u6570\u636e\u5230 16 \u5b57\u8282\n    private static function pad($data) {\n        $len = strlen($data);\n        $padLen = 16 - ($len % 16);\n        return $data . str_repeat(chr($padLen), $padLen);\n    }\n\n    \/\/ \u5bc6\u94a5\u6269\u5c55\n    private static function keyExpansion($key) {\n        \/\/ \u5c06\u5bc6\u94a5\u901a\u8fc7\u7279\u5b9a\u7b97\u6cd5\u8fdb\u884c\u6269\u5c55\uff0c\u5f97\u5230\u6bcf\u8f6e\u7684\u5bc6\u94a5\n        \/\/ \u8fd9\u91cc\u53ea\u662f\u793a\u610f\uff0c\u5177\u4f53\u7b97\u6cd5\u9700\u8981\u6839\u636e SM4 \u6807\u51c6\u6765\u6269\u5c55\n        return &#x5B;];\n    }\n\n    \/\/ SM4 \u8f6e\u6b21\u52a0\u5bc6\n    private static function roundFunction($state, $roundKey) {\n        \/\/ \u8f6e\u51fd\u6570\n        return $state;  \/\/ \u8fd9\u91cc\u53ea\u662f\u4e00\u4e2a\u5360\u4f4d\n    }\n\n    \/\/ \u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u72b6\u6001\u77e9\u9635\n    private static function convertToState($data) {\n        \/\/ \u8be5\u65b9\u6cd5\u5c06 128 \u4f4d\u7684\u6570\u636e\u8f6c\u6362\u4e3a 32 \u4f4d\u72b6\u6001\n        return &#x5B;];\n    }\n\n    \/\/ \u5c06\u72b6\u6001\u77e9\u9635\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\n    private static function convertToString($state) {\n        \/\/ \u5c06\u72b6\u6001\u77e9\u9635\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\n        return &#039;&#039;;\n    }\n}\n\n$key = &quot;1234567890abcdef&quot;;  \/\/ \u5bc6\u94a5\n$data = &quot;Hello, SM4&quot;;\n\n\/\/ \u52a0\u5bc6\n$encrypted = SM4::encrypt($data, $key);\necho &quot;Encrypted Data: &quot; . bin2hex($encrypted) . &quot;\\n&quot;;\n\n<\/pre><\/div>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u624b\u52a8\u5b9e\u73b0 SM4 \u52a0\u5bc6\u9700\u8981\u5b8c\u5168\u6309\u7167\u6807\u51c6\u8fdb\u884c\uff0c\u6d89\u53ca\u5bc6\u94a5\u6269\u5c55\u3001\u8f6e\u51fd\u6570\u5b9e\u73b0\u3001\u4ee5\u53ca S \u76d2\u3001P \u7f6e\u6362\u7b49\u590d\u6742\u64cd\u4f5c\uff0c\u63a8\u8350\u4f7f\u7528\u73b0\u6709\u7684\u5e93\u6765\u5b9e\u73b0\uff0c\u907f\u514d\u9519\u8bef\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001\u4f7f\u7528 <code>mcrypt<\/code> \u6269\u5c55\uff08\u4e0d\u63a8\u8350\uff09<\/h3>\n\n\n\n<p>\u867d\u7136 PHP \u7684 <code>mcrypt<\/code> \u6269\u5c55\u5df2\u7ecf\u5e9f\u5f03\uff0c\u4f46\u5b83\u4ecd\u7136\u652f\u6301\u4e00\u4e9b\u5e38\u89c1\u7684\u52a0\u5bc6\u7b97\u6cd5\uff08\u5305\u62ec SM4\uff09\u3002\u4e0d\u8fc7\uff0c\u5728 PHP 7.1 \u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\u4e0d\u518d\u63a8\u8350\u4f7f\u7528\u6b64\u6269\u5c55\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001\u63a8\u8350\u4f7f\u7528 <code>openssl<\/code>\uff08\u5982\u679c\u517c\u5bb9\uff09<\/h3>\n\n\n\n<p>\u76ee\u524d PHP \u672c\u8eab\u6ca1\u6709\u76f4\u63a5\u652f\u6301 SM4 \u7b97\u6cd5\u7684\u6269\u5c55\uff0c\u4f46\u5982\u679c\u4f60\u9700\u8981\u8fdb\u884c\u9ad8\u7ea7\u7684\u52a0\u5bc6\u64cd\u4f5c\uff08\u5982 <strong>SM2\u3001SM3\u3001SM4<\/strong>\uff09\uff0c\u53ef\u4ee5\u53c2\u8003 <strong>OpenSSL<\/strong> \u7b49\u4e13\u4e1a\u7684\u52a0\u5bc6\u5de5\u5177\u548c\u5e93\u3002<\/p>\n\n\n\n<p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c<code>php-sm4<\/code> \u5e93\u7684\u4f7f\u7528\u66f4\u4e3a\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e94\u3001\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u63a8\u8350\u4f7f\u7528\u73b0\u6709\u7684\u5e93<\/strong>\uff0c\u5982 <code>php-sm4<\/code>\uff0c\u6765\u5b9e\u73b0 SM4 \u52a0\u5bc6\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8981\u624b\u52a8\u5b9e\u73b0\uff0c\u5fc5\u987b\u5b8c\u5168\u6309\u7167 SM4 \u7b97\u6cd5\u89c4\u8303\u8fdb\u884c\uff0c\u4f46\u5b9e\u73b0\u8d77\u6765\u6bd4\u8f83\u590d\u6742\u3002<\/li>\n\n\n\n<li><strong>\u52a0\u5bc6\u5bc6\u94a5\u548c\u6570\u636e\u7684\u5904\u7406<\/strong>\u975e\u5e38\u91cd\u8981\uff0c\u8981\u786e\u4fdd\u5bc6\u94a5\u7684\u5b89\u5168\u5b58\u50a8\u548c\u6570\u636e\u7684\u6b63\u786e\u586b\u5145\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>SM4\uff08\u5546\u7528\u5bc6\u7801\u7b97\u6cd5\uff09\u662f\u4e2d\u56fd\u56fd\u5bb6\u5bc6\u7801\u7b97\u6cd5\u6807\u51c6\u4e4b\u4e00\uff0c\u5e7f\u6cdb\u7528\u4e8e\u5bf9\u79f0\u52a0\u5bc6\u3002PHP \u9ed8\u8ba4&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/01\/06\/php-sm4%e5%8a%a0%e5%af%86%e7%9a%84%e5%ae%9e%e7%8e%b0\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-1270","post","type-post","status-publish","format-standard","hentry","category-php"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1270"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1270\/revisions"}],"predecessor-version":[{"id":1271,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1270\/revisions\/1271"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}