{"id":22294,"date":"2024-11-21T08:40:32","date_gmt":"2024-11-21T00:40:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22294\/"},"modified":"2024-11-21T08:40:32","modified_gmt":"2024-11-21T00:40:32","slug":"thinkphp5%e4%b8%80%e8%88%ac%e5%8a%a0%e5%af%86%e5%93%aa%e4%ba%9b%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22294\/","title":{"rendered":"thinkphp5\u4e00\u822c\u52a0\u5bc6\u54ea\u4e9b\u6587\u4ef6"},"content":{"rendered":"<p>\u5728\u8fdb\u884c thinkphp5 \u5e94\u7528\u90e8\u7f72\u65f6\uff0c\u4e3a\u4e86\u589e\u52a0\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff0c\u6211\u4eec\u4e00\u822c\u4f1a\u5bf9\u67d0\u4e9b\u654f\u611f\u6587\u4ef6\u8fdb\u884c\u52a0\u5bc6\u3002\u672c\u6587\u5c06\u4ecb\u7ecd thinkphp5 \u4e2d\u4e00\u822c\u9700\u8981\u52a0\u5bc6\u7684\u6587\u4ef6\uff0c\u4ee5\u53ca\u52a0\u5bc6\u65b9\u6cd5\u3002<\/p>\n<p>\u4e00\u822c\u9700\u8981\u52a0\u5bc6\u7684\u6587\u4ef6<\/p>\n<ol>\n<li>\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u914d\u7f6e\u6587\u4ef6\u4e2d\u5305\u542b\u4e86\u6570\u636e\u5e93\u5bc6\u7801\u7b49\u91cd\u8981\u4fe1\u606f\uff0c\u4e0d\u52a0\u5bc6\u5bb9\u6613\u88ab\u4ed6\u4eba\u83b7\u53d6\uff0c\u9020\u6210\u6570\u636e\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<ol>\n<li>\u63a7\u5236\u5668\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u63a7\u5236\u5668\u6587\u4ef6\u5305\u542b\u4e86\u7a0b\u5e8f\u7684\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\uff0c\u4e0d\u52a0\u5bc6\u5bb9\u6613\u88ab\u7ade\u4e89\u5bf9\u624b\u83b7\u53d6\uff0c\u5bfc\u81f4\u4e1a\u52a1\u6cc4\u9732\u3002<\/p>\n<ol>\n<li>\u6a21\u578b\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u6a21\u578b\u6587\u4ef6\u5305\u542b\u4e86\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u4ee3\u7801\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u5bf9\u6570\u636e\u5e93\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\uff0c\u4e0d\u52a0\u5bc6\u5bb9\u6613\u88ab\u4ed6\u4eba\u83b7\u53d6\uff0c\u5bfc\u81f4\u6570\u636e\u5e93\u6570\u636e\u6cc4\u9732\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u89c6\u56fe\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u89c6\u56fe\u6587\u4ef6\u4e2d\u5305\u542b\u4e86 HTML \u6a21\u677f\u4ee3\u7801\uff0c\u4e0d\u52a0\u5bc6\u5bb9\u6613\u88ab\u4ed6\u4eba\u83b7\u53d6\uff0c\u4ea7\u751f\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<p>\u52a0\u5bc6\u65b9\u5f0f<\/p>\n<ol>\n<li>\u4f7f\u7528\u52a0\u5bc6\u5668<\/li>\n<\/ol>\n<p>\u5728 ThinkPHP5 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Swoole \u63d0\u4f9b\u7684\u52a0\u5bc6\u5668\uff0c\u5bf9\u6307\u5b9a\u7684\u6587\u4ef6\u8fdb\u884c\u52a0\u5bc6\u3002<\/p>\n<p>\u52a0\u5bc6\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>use SwooleProcess;\n\n$encrypt_files = [\n    __DIR__ . '\/..\/application\/config.php',\n    __DIR__ . '\/..\/application\/database.php',\n    __DIR__ . '\/..\/application\/admin\/controller\/User.php',\n    __DIR__ . '\/..\/application\/admin\/model\/User.php',\n];\n\n\/\/ \u52a0\u5bc6\u5bc6\u94a5\n$key = \"1234567890\";\n\n\/\/ \u547d\u4ee4\u884c\u53c2\u6570\n$argv = [\n    'swoole_encryption', \/\/ \u7a0b\u5e8f\u540d\n    'password', \/\/ \u7528\u6237\u5bc6\u7801\n    'backend', \/\/ \u7528\u6237\u89d2\u8272\n];\n\nforeach ($encrypt_files as $file) {\n    $process = new Process(function () use ($file, $key, $argv) {\n        \/\/ \u6267\u884c\u52a0\u5bc6\u64cd\u4f5c\n        $encrypted = SwooleEncryptionEncrypt::setKey($key)\n            -&gt;encrypt(file_get_contents($file));\n\n        \/\/ \u5c06\u52a0\u5bc6\u7684\u5185\u5bb9\u5199\u5165\u5230\u539f\u59cb\u6587\u4ef6\u4e2d\n        file_put_contents($file, $encrypted);\n\n        \/\/ \u6267\u884c\u547d\u4ee4\u884c\u547d\u4ee4\n        $cmd = implode(' ', $argv);\n        exec($cmd);\n    });\n\n    $process-&gt;start();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u81ea\u5b9a\u4e49\u52a0\u5bc6\u65b9\u5f0f<\/li>\n<\/ol>\n<p>\u9664\u4e86\u4f7f\u7528\u52a0\u5bc6\u5668\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u52a0\u5bc6\u65b9\u5f0f\u3002\u53ef\u4ee5\u5bf9\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c MD5 \u7b7e\u540d\uff0c\u7136\u540e\u5c06\u7b7e\u540d\u540e\u7684\u5185\u5bb9\u548c\u539f\u59cb\u5185\u5bb9\u4e00\u8d77\u5199\u5165\u5230\u6587\u4ef6\u4e2d\u3002\u5728\u8fd0\u884c\u65f6\uff0c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5bf9\u6bd4\u7b7e\u540d\u548c\u539f\u59cb\u5185\u5bb9\u662f\u5426\u4e00\u81f4\uff0c\u4ece\u800c\u9a8c\u8bc1\u6587\u4ef6\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u52a0\u5bc6\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>\/**\n * \u52a0\u5bc6\u6587\u4ef6\n *\n * @param string $file \u6587\u4ef6\u8def\u5f84\n * @param string $key \u52a0\u5bc6\u5bc6\u94a5\n *\/\nfunction encryptFile($file, $key) {\n    $content = file_get_contents($file);\n    $signature = md5($content . $key);\n    $encrypted_content = $signature . $content;\n\n    file_put_contents($file, $encrypted_content);\n}\n\n\/**\n * \u89e3\u5bc6\u6587\u4ef6\n *\n * @param string $file \u6587\u4ef6\u8def\u5f84\n * @param string $key \u52a0\u5bc6\u5bc6\u94a5\n *\n * @return boolean\n *\/\nfunction decryptFile($file, $key) {\n    $content = file_get_contents($file);\n    $signature = substr($content, 0, 32);\n    $data = substr($content, 32);\n    $md5 = md5($data . $key);\n\n    if ($md5 == $signature) {\n        file_put_contents($file, $data);\n        return true;\n    } else {\n        return false;\n    }\n}\n\n\/\/ \u5f85\u52a0\u5bc6\u7684\u6587\u4ef6\u5217\u8868\n$files = [\n    \"\/path\/to\/config.php\",\n    \"\/path\/to\/controller\/User.php\",\n    \"\/path\/to\/model\/User.php\",\n];\n\n$key = \"1234567890\";\n\n\/\/ \u5bf9\u6bcf\u4e2a\u6587\u4ef6\u8fdb\u884c\u52a0\u5bc6\nforeach ($files as $file) {\n    encryptFile($file, $key);\n}\n\n\/\/ \u5bf9\u6bcf\u4e2a\u6587\u4ef6\u8fdb\u884c\u89e3\u5bc6\nforeach ($files as $file) {\n    decryptFile($file, $key);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u901a\u8fc7\u5bf9\u654f\u611f\u6587\u4ef6\u8fdb\u884c\u52a0\u5bc6\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff0c\u9632\u6b62\u6570\u636e\u6cc4\u9732\u3001\u4ee3\u7801\u7ade\u4e89\u7b49\u95ee\u9898\u3002\u5728\u52a0\u5bc6\u6587\u4ef6\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u52a0\u5bc6\u5668\uff0c\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u52a0\u5bc6\u65b9\u5f0f\u3002\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u9700\u8981\u52a0\u5bc6\u5bc6\u94a5\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp5\u4e00\u822c\u52a0\u5bc6\u54ea\u4e9b\u6587\u4ef6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u8fdb\u884c thinkphp5 \u5e94\u7528\u90e8\u7f72\u65f6\uff0c\u4e3a\u4e86\u589e\u52a0\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff0c\u6211\u4eec\u4e00\u822c\u4f1a\u5bf9\u67d0\u4e9b\u654f\u611f\u6587\u4ef6\u8fdb\u884c\u52a0\u5bc6\u3002\u672c\u6587\u5c06\u4ecb\u7ecd thinkphp5 \u4e2d\u4e00\u822c\u9700\u8981\u52a0\u5bc6\u7684\u6587\u4ef6\uff0c\u4ee5\u53ca\u52a0\u5bc6\u65b9\u6cd5\u3002 \u4e00\u822c\u9700\u8981\u52a0\u5bc6\u7684\u6587\u4ef6 \u914d\u7f6e\u6587\u4ef6 \u914d\u7f6e\u6587\u4ef6\u4e2d\u5305\u542b\u4e86\u6570\u636e\u5e93\u5bc6\u7801\u7b49\u91cd\u8981\u4fe1\u606f\uff0c\u4e0d\u52a0\u5bc6\u5bb9\u6613\u88ab\u4ed6\u4eba\u83b7\u53d6\uff0c\u9020\u6210\u6570\u636e\u5b89\u5168\u95ee\u9898\u3002 \u63a7\u5236\u5668\u6587\u4ef6 \u63a7\u5236\u5668\u6587\u4ef6\u5305\u542b\u4e86\u7a0b\u5e8f\u7684\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\uff0c\u4e0d\u52a0\u5bc6\u5bb9\u6613\u88ab\u7ade\u4e89\u5bf9\u624b\u83b7\u53d6\uff0c\u5bfc\u81f4\u4e1a\u52a1\u6cc4\u9732\u3002 \u6a21\u578b\u6587\u4ef6 \u6a21\u578b\u6587\u4ef6\u5305\u542b\u4e86\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u4ee3\u7801\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u5bf9\u6570\u636e\u5e93\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\uff0c\u4e0d\u52a0\u5bc6\u5bb9\u6613\u88ab\u4ed6\u4eba\u83b7\u53d6\uff0c\u5bfc\u81f4\u6570\u636e\u5e93\u6570\u636e\u6cc4\u9732\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u89c6\u56fe\u6587\u4ef6 \u89c6\u56fe\u6587\u4ef6\u4e2d\u5305\u542b\u4e86 HTML \u6a21\u677f\u4ee3\u7801\uff0c\u4e0d\u52a0\u5bc6\u5bb9\u6613\u88ab\u4ed6\u4eba\u83b7\u53d6\uff0c\u4ea7\u751f\u5b89\u5168\u95ee\u9898\u3002 \u52a0\u5bc6\u65b9\u5f0f \u4f7f\u7528\u52a0\u5bc6\u5668 \u5728 ThinkPHP5 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Swoole \u63d0\u4f9b\u7684\u52a0\u5bc6\u5668\uff0c\u5bf9\u6307\u5b9a\u7684\u6587\u4ef6\u8fdb\u884c\u52a0\u5bc6\u3002 \u52a0\u5bc6\u4ee3\u7801\u793a\u4f8b\uff1a use SwooleProcess; $encrypt_files = [ __DIR__ . &#8216;\/..\/application\/config.php&#8217;, __DIR__ . &#8216;\/..\/application\/database.php&#8217;, __DIR__ . &#8216;\/..\/application\/admin\/controller\/User.php&#8217;, __DIR__ . &#8216;\/..\/application\/admin\/model\/User.php&#8217;, ]; \/\/ \u52a0\u5bc6\u5bc6\u94a5 $key = &#8220;1234567890&#8221;; \/\/ \u547d\u4ee4\u884c\u53c2\u6570 $argv = [ &#8216;swoole_encryption&#8217;, \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22294","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22294"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22294\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}