{"id":23568,"date":"2024-11-21T11:31:20","date_gmt":"2024-11-21T03:31:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23568\/"},"modified":"2024-11-21T11:31:20","modified_gmt":"2024-11-21T03:31:20","slug":"laravel%e5%bc%80%e5%8f%91%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8laravel-encryption%e5%8a%a0%e5%af%86%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23568\/","title":{"rendered":"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Encryption\u52a0\u5bc6\u6570\u636e\uff1f"},"content":{"rendered":"<p>\u662f\u4e00\u4e2a\u4f18\u79c0\u7684php\u6846\u67b6\uff0c\u5b83\u7684\u8bbe\u8ba1\u7406\u5ff5\u662f\u7b80\u5355\u3001\u4f18\u96c5\uff0c\u540c\u65f6\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u6613\u7528\u7684api\u63a5\u53e3\uff0c\u5e7f\u53d7\u5f00\u53d1\u8005\u7684\u6b22\u8fce\u3002\u5728laravel\u6846\u67b6\u4e2d\uff0c\u4f7f\u7528laravel encryption\u52a0\u5bc6\u6570\u636e\u975e\u5e38\u65b9\u4fbf\uff0c\u672c\u6587\u5c06\u4ecb\u7ecdlaravel encryption\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<h2>\u4ec0\u4e48\u662fLaravel Encryption\uff1f<\/h2>\n<p>Laravel Encryption\u662fLaravel\u6846\u67b6\u63d0\u4f9b\u7684\u4e00\u79cd\u52a0\u5bc6\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u5b83\u80fd\u591f\u5feb\u901f\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\uff0c\u540c\u65f6\u4f7f\u7528\u7b80\u5355\uff0c\u975e\u5e38\u9002\u5408\u5728Laravel\u5f00\u53d1\u4e2d\u5e94\u7528\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u9700\u8981\u5b58\u50a8\u654f\u611f\u6570\u636e\u65f6\uff0c\u5982\u5bc6\u7801\u3001\u4fe1\u7528\u5361\u53f7\u7b49\uff0c\u4e3a\u4e86\u907f\u514d\u76f4\u63a5\u5b58\u50a8\u660e\u6587\uff0c\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\uff0c\u9632\u6b62\u654f\u611f\u6570\u636e\u6cc4\u9732\u5e26\u6765\u7684\u5b89\u5168\u95ee\u9898\u3002Laravel Encryption\u63d0\u4f9b\u4e86\u5b89\u5168\u7684AES-256-CBC\u52a0\u5bc6\u7b97\u6cd5\u6765\u4fdd\u969c\u6570\u636e\u7684\u5b89\u5168\u6027\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528Laravel Encryption\uff1f<\/h2>\n<p>Laravel Encryption\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5373\u53ef\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\u64cd\u4f5c\u3002<\/p>\n<h3>\u6b65\u9aa4\u4e00\uff1a\u751f\u6210\u5bc6\u94a5<\/h3>\n<p>\u5728Laravel Encryption\u4e2d\uff0c\u9700\u8981\u4e00\u4e2a\u5bc6\u94a5\uff08$key\uff09\u6765\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\u64cd\u4f5c\u3002\u751f\u6210\u5bc6\u94a5\u7684\u65b9\u6cd5\u662f\u5728.env\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2aAPP_KEY\u53d8\u91cf\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u5bc6\u94a5\uff1a<\/p>\n<pre>php artisan key:generate<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u751f\u6210\u7684\u5bc6\u94a5\u4f1a\u81ea\u52a8\u5b58\u50a8\u5728.env\u6587\u4ef6\u7684APP_KEY\u53d8\u91cf\u4e2d\u3002<\/p>\n<h3>\u6b65\u9aa4\u4e8c\uff1a\u52a0\u5bc6\u6570\u636e<\/h3>\n<p>\u5728\u4f7f\u7528Laravel Encryption\u52a0\u5bc6\u6570\u636e\u524d\uff0c\u6211\u4eec\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5f15\u5165Laravel Crypt\u5e93\uff1a<\/p>\n<pre>use IlluminateSupportFacadesCrypt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>Laravel Encryption\u63d0\u4f9b\u4e86encrypt()\u65b9\u6cd5\u5feb\u901f\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\uff1a<\/p>\n<pre>$data = '\u9700\u8981\u52a0\u5bc6\u7684\u6570\u636e';\n$encrypted_data = Crypt::encryptString($data);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u6b65\u9aa4\u4e09\uff1a\u89e3\u5bc6\u6570\u636e<\/h3>\n<p>Laravel Encryption\u63d0\u4f9b\u4e86decrypt()\u65b9\u6cd5\u5feb\u901f\u5bf9\u6570\u636e\u8fdb\u884c\u89e3\u5bc6\uff1a<\/p>\n<pre>$decrypted_data = Crypt::decryptString($encrypted_data);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u5b9e\u9645\u5e94\u7528<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u529f\u80fd\u4e3a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528Laravel Encryption\u52a0\u5bc6\u654f\u611f\u6570\u636e\u3002<\/p>\n<p>\u7528\u6237\u6ce8\u518c\u65f6\uff0c\u9700\u8981\u5b58\u50a8\u7528\u6237\u7684\u5bc6\u7801\uff0c\u4f46\u4e3a\u4e86\u9632\u6b62\u7528\u6237\u5bc6\u7801\u6cc4\u9732\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\u3002\u5728\u7528\u6237\u767b\u5f55\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u4e0e\u6570\u636e\u5e93\u4e2d\u7684\u5bc6\u7801\u8fdb\u884c\u6bd4\u5bf9\uff0c\u8fd9\u5c31\u9700\u8981\u5c06\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u540c\u6837\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\uff0c\u7136\u540e\u518d\u8fdb\u884c\u6bd4\u5bf9\u3002<\/p>\n<h4>\u6ce8\u518c\u529f\u80fd<\/h4>\n<p>\u5728\u7528\u6237\u6ce8\u518c\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>use IlluminateSupportFacadesCrypt;\nuse AppUser;\n\n$user = new User;\n$user-&amp;gt;name = '\u7528\u6237\u540d';\n$user-&amp;gt;password = Crypt::encryptString('\u5bc6\u7801');\n$user-&amp;gt;save();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4>\u767b\u5f55\u529f\u80fd<\/h4>\n<p>\u5728\u7528\u6237\u767b\u5f55\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\uff0c\u7136\u540e\u4e0e\u6570\u636e\u5e93\u4e2d\u7684\u5bc6\u7801\u8fdb\u884c\u6bd4\u5bf9\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>use IlluminateSupportFacadesCrypt;\nuse IlluminateHttpRequest;\nuse AppUser;\n\npublic function login(Request $request)\n{\n    $user = User::where('name', $request-&amp;gt;input('name'))-&amp;gt;first();\n    if (!$user || $user-&amp;gt;password != Crypt::encryptString($request-&amp;gt;input('password'))) {\n        return '\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef';\n    }\n    \/\/ \u767b\u5f55\u6210\u529f\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>Laravel Encryption\u662fLaravel\u6846\u67b6\u63d0\u4f9b\u7684\u4e00\u79cd\u7b80\u5355\u6613\u7528\u7684\u6570\u636e\u52a0\u5bc6\u65b9\u5f0f\uff0c\u80fd\u591f\u6709\u6548\u4fdd\u969c\u654f\u611f\u6570\u636e\u7684\u5b89\u5168\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ea\u9700\u6309\u7167\u4e0a\u8ff0\u6b65\u9aa4\u5373\u53ef\u5b8c\u6210\u52a0\u5bc6\u548c\u89e3\u5bc6\u64cd\u4f5c\uff0c\u4ee3\u7801\u6e05\u6670\u7b80\u6d01\uff0c\u975e\u5e38\u9002\u5408\u5728Laravel\u5f00\u53d1\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLaravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Encryption\u52a0\u5bc6\u6570\u636e\uff1f\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>\u662f\u4e00\u4e2a\u4f18\u79c0\u7684php\u6846\u67b6\uff0c\u5b83\u7684\u8bbe\u8ba1\u7406\u5ff5\u662f\u7b80\u5355\u3001\u4f18\u96c5\uff0c\u540c\u65f6\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u6613\u7528\u7684api\u63a5\u53e3\uff0c\u5e7f\u53d7\u5f00\u53d1\u8005\u7684\u6b22\u8fce\u3002\u5728laravel\u6846\u67b6\u4e2d\uff0c\u4f7f\u7528laravel encryption\u52a0\u5bc6\u6570\u636e\u975e\u5e38\u65b9\u4fbf\uff0c\u672c\u6587\u5c06\u4ecb\u7ecdlaravel encryption\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u3002 \u4ec0\u4e48\u662fLaravel Encryption\uff1f Laravel Encryption\u662fLaravel\u6846\u67b6\u63d0\u4f9b\u7684\u4e00\u79cd\u52a0\u5bc6\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u5b83\u80fd\u591f\u5feb\u901f\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\uff0c\u540c\u65f6\u4f7f\u7528\u7b80\u5355\uff0c\u975e\u5e38\u9002\u5408\u5728Laravel\u5f00\u53d1\u4e2d\u5e94\u7528\u3002 \u5f53\u6211\u4eec\u9700\u8981\u5b58\u50a8\u654f\u611f\u6570\u636e\u65f6\uff0c\u5982\u5bc6\u7801\u3001\u4fe1\u7528\u5361\u53f7\u7b49\uff0c\u4e3a\u4e86\u907f\u514d\u76f4\u63a5\u5b58\u50a8\u660e\u6587\uff0c\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\uff0c\u9632\u6b62\u654f\u611f\u6570\u636e\u6cc4\u9732\u5e26\u6765\u7684\u5b89\u5168\u95ee\u9898\u3002Laravel Encryption\u63d0\u4f9b\u4e86\u5b89\u5168\u7684AES-256-CBC\u52a0\u5bc6\u7b97\u6cd5\u6765\u4fdd\u969c\u6570\u636e\u7684\u5b89\u5168\u6027\u3002 \u5982\u4f55\u4f7f\u7528Laravel Encryption\uff1f Laravel Encryption\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5373\u53ef\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\u64cd\u4f5c\u3002 \u6b65\u9aa4\u4e00\uff1a\u751f\u6210\u5bc6\u94a5 \u5728Laravel Encryption\u4e2d\uff0c\u9700\u8981\u4e00\u4e2a\u5bc6\u94a5\uff08$key\uff09\u6765\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\u64cd\u4f5c\u3002\u751f\u6210\u5bc6\u94a5\u7684\u65b9\u6cd5\u662f\u5728.env\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2aAPP_KEY\u53d8\u91cf\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u5bc6\u94a5\uff1a php artisan key:generate \u767b\u5f55\u540e\u590d\u5236 \u751f\u6210\u7684\u5bc6\u94a5\u4f1a\u81ea\u52a8\u5b58\u50a8\u5728.env\u6587\u4ef6\u7684APP_KEY\u53d8\u91cf\u4e2d\u3002 \u6b65\u9aa4\u4e8c\uff1a\u52a0\u5bc6\u6570\u636e \u5728\u4f7f\u7528Laravel Encryption\u52a0\u5bc6\u6570\u636e\u524d\uff0c\u6211\u4eec\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5f15\u5165Laravel Crypt\u5e93\uff1a use IlluminateSupportFacadesCrypt; \u767b\u5f55\u540e\u590d\u5236 Laravel Encryption\u63d0\u4f9b\u4e86encrypt()\u65b9\u6cd5\u5feb\u901f\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\uff1a $data = &#8216;\u9700\u8981\u52a0\u5bc6\u7684\u6570\u636e&#8217;; $encrypted_data = Crypt::encryptString($data); \u767b\u5f55\u540e\u590d\u5236 \u6b65\u9aa4\u4e09\uff1a\u89e3\u5bc6\u6570\u636e Laravel Encryption\u63d0\u4f9b\u4e86decrypt()\u65b9\u6cd5\u5feb\u901f\u5bf9\u6570\u636e\u8fdb\u884c\u89e3\u5bc6\uff1a $decrypted_data = Crypt::decryptString($encrypted_data); \u767b\u5f55\u540e\u590d\u5236 \u5b9e\u9645\u5e94\u7528 \u4e0b\u9762\u6211\u4eec\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u529f\u80fd\u4e3a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528Laravel Encryption\u52a0\u5bc6\u654f\u611f\u6570\u636e\u3002 \u7528\u6237\u6ce8\u518c\u65f6\uff0c\u9700\u8981\u5b58\u50a8\u7528\u6237\u7684\u5bc6\u7801\uff0c\u4f46\u4e3a\u4e86\u9632\u6b62\u7528\u6237\u5bc6\u7801\u6cc4\u9732\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\u3002\u5728\u7528\u6237\u767b\u5f55\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u4e0e\u6570\u636e\u5e93\u4e2d\u7684\u5bc6\u7801\u8fdb\u884c\u6bd4\u5bf9\uff0c\u8fd9\u5c31\u9700\u8981\u5c06\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u540c\u6837\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\uff0c\u7136\u540e\u518d\u8fdb\u884c\u6bd4\u5bf9\u3002 \u6ce8\u518c\u529f\u80fd \u5728\u7528\u6237\u6ce8\u518c\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a use IlluminateSupportFacadesCrypt; use AppUser; $user [&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-23568","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23568","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=23568"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23568\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}