{"id":26019,"date":"2024-11-24T14:13:32","date_gmt":"2024-11-24T06:13:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26019\/"},"modified":"2024-11-24T14:13:32","modified_gmt":"2024-11-24T06:13:32","slug":"lithe-crypt%ef%bc%9a%e7%ae%80%e5%8c%96-php-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b8%ad%e7%9a%84%e5%8a%a0%e5%af%86%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26019\/","title":{"rendered":"Lithe Crypt\uff1a\u7b80\u5316 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u52a0\u5bc6\u6280\u672f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173077582553646.jpg\" class=\"aligncenter\" title=\"Lithe Crypt\uff1a\u7b80\u5316 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u52a0\u5bc6\u6280\u672f\u63d2\u56fe\" alt=\"Lithe Crypt\uff1a\u7b80\u5316 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u52a0\u5bc6\u6280\u672f\u63d2\u56fe\" \/><\/p>\n<p><strong>lithe crypt<\/strong> \u662f php \u4e2d\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u52a0\u5bc6\u548c\u89e3\u5bc6\u5b9e\u7528\u7a0b\u5e8f\uff0c\u65e8\u5728\u4e0e lithe \u6846\u67b6\u914d\u5408\u4f7f\u7528\u3002\u5b83\u4f7f\u7528 aes-256-cbc \u7b97\u6cd5\u8fdb\u884c\u5b89\u5168\u6570\u636e\u5904\u7406\u3002<\/p>\n<h2> \u5b89\u88c5 <\/h2>\n<p>\u8981\u5b89\u88c5 lithe crypt \u8f6f\u4ef6\u5305\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 \u3002\u5982\u679c\u60a8\u5c1a\u672a\u5b89\u88c5\uff0c\u8bf7\u786e\u4fdd composer \u5728\u60a8\u7684\u7cfb\u7edf\u4e0a\u53ef\u7528\u3002\u7136\u540e\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>composer require lithemod\/crypt\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u8981\u6c42 <\/h3>\n<ul>\n<li>php 8 \u6216\u66f4\u9ad8\u7248\u672c<\/li>\n<li>\u5728 php \u5b89\u88c5\u4e2d\u542f\u7528 openssl \u6269\u5c55<\/li>\n<\/ul>\n<h2> \u4f7f\u7528 <\/h2>\n<h3> \u52a0\u8f7d\u73af\u5883\u53d8\u91cf <\/h3>\n<p>\u5728\u4f7f\u7528 crypt \u7c7b\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u52a0\u8f7d\u73af\u5883\u53d8\u91cf\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u60a8\u7684 .env \u6587\u4ef6\uff1a<\/p>\n<pre>use lithesupportenv;\n\n\/\/ carregar vari\u00e1veis de ambiente\nenv::load(__dir__); \/\/ ajuste o caminho conforme necess\u00e1rio\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u8bbe\u7f6eapp_key <\/h3>\n<p>\u786e\u4fdd\u5df2\u8bbe\u7f6e app_key \u73af\u5883\u53d8\u91cf\u3002\u6b64\u5bc6\u94a5\u5fc5\u987b\u662f 32 \u5b57\u8282\u7684 base64 \u7f16\u7801\u5b57\u7b26\u4e32\u3002\u60a8\u53ef\u4ee5\u5728 .env \u6587\u4ef6\u4e2d\u914d\u7f6e\u5b83\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728\u670d\u52a1\u5668\u73af\u5883\u4e2d\u914d\u7f6e\u5b83\u3002<\/p>\n<p>\u6709\u6548\u7684 base64 \u5bc6\u94a5\u793a\u4f8b\uff1a<\/p>\n<pre>yxnkzmfnc2rhc2rmywdlcybhc2rmywdlcybhyxnkzmfnc2q=\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u52a0\u5bc6\u6570\u636e <\/h3>\n<p>\u8981\u52a0\u5bc6\u6570\u636e\uff0c\u8bf7\u4f7f\u7528 crypt \u7c7b\u7684 encrypt \u65b9\u6cd5\u3002\u60a8\u8fd8\u53ef\u4ee5\u6307\u5b9a\u662f\u5426\u8981\u4f7f\u7528\u56fa\u5b9aiv\uff08\u521d\u59cb\u5316\u5411\u91cf\uff09\u8fdb\u884c\u52a0\u5bc6\uff1a<\/p>\n<pre>use lithesupportsecuritycrypt;\n\n$data = \"dados sens\u00edveis\";\n\n\/\/ criptografar sem iv fixo\n$encrypted = crypt::encrypt($data);\necho \"dados criptografados: \" . $encrypted;\n\n\/\/ criptografar com iv fixo (\u00fatil para valores \u00fanicos como e-mails)\n$encryptedwithsameiv = crypt::encrypt($data, true);\necho \"dados criptografados com iv fixo: \" . $encryptedwithsameiv;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u89e3\u5bc6\u6570\u636e <\/h3>\n<p>\u8981\u89e3\u5bc6\u4e4b\u524d\u52a0\u5bc6\u7684\u6570\u636e\uff0c\u8bf7\u4f7f\u7528decrypt\u65b9\u6cd5\u3002\u60a8\u5fc5\u987b\u6307\u5b9a\u4e0e\u52a0\u5bc6\u65f6\u4f7f\u7528\u7684\u53c2\u6570\u76f8\u540c\u7684\u53c2\u6570\uff0c\u4ee5\u786e\u4fdd\u6b63\u786e\u89e3\u5bc6\uff1a<\/p>\n<pre>use lithesupportsecuritycrypt;\n\n\/\/ descriptografar sem iv fixo\n$decrypted = crypt::decrypt($encrypted);\necho \"dados descriptografados: \" . $decrypted;\n\n\/\/ descriptografar com iv fixo\n$decryptedwithsameiv = crypt::decrypt($encryptedwithsameiv, true, $data);\necho \"dados descriptografados com iv fixo: \" . $decryptedwithsameiv;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u5f02\u5e38\u5904\u7406 <\/h3>\n<p>\u5982\u679c app_key \u672a\u5b9a\u4e49\u6216\u65e0\u6548\uff0ccrypt \u7c7b\u5c06\u629b\u51fa cryptexception\u3002\u5fc5\u987b\u5728\u4ee3\u7801\u4e2d\u5904\u7406\u6b64\u5f02\u5e38\u4ee5\u907f\u514d\u610f\u5916\u9519\u8bef\uff1a<\/p>\n<pre>use LitheExceptionsEncryptionCryptException;\n\ntry {\n    $encrypted = Crypt::encrypt($data);\n    \/\/ Descriptografar sem IV fixo\n    $decrypted = Crypt::decrypt($encrypted);\n} catch (CryptException $e) {\n    echo \"Erro de Criptografia: \" . $e-&gt;getMessage();\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u6700\u540e\u7684\u8003\u8651\u56e0\u7d20 <\/h2>\n<p><strong>lithe crypt<\/strong> \u63d0\u4f9b\u4e86\u4e00\u79cd\u5b9e\u7528\u4e14\u5b89\u5168\u7684\u65b9\u6cd5\u6765\u5904\u7406 php \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u6570\u636e\u52a0\u5bc6\u548c\u89e3\u5bc6\u3002\u901a\u8fc7\u5b9e\u65bd aes-256-cbc \u7b97\u6cd5\u4ee5\u53ca\u4e0e lithe \u6846\u67b6\u7684\u8f7b\u677e\u96c6\u6210\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u4fdd\u62a4\u60a8\u7684\u6570\u636e\u3002\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u770b\u770b\u5b83\u5982\u4f55\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff01<\/p>\n<p>\u5982\u679c\u60a8\u6709\u4efb\u4f55\u7591\u95ee\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u5728\u4e0b\u9762\u8bc4\u8bba\uff01<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLithe Crypt\uff1a\u7b80\u5316 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u52a0\u5bc6\u6280\u672f\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>lithe crypt \u662f php \u4e2d\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u52a0\u5bc6\u548c\u89e3\u5bc6\u5b9e\u7528\u7a0b\u5e8f\uff0c\u65e8\u5728\u4e0e lithe \u6846\u67b6\u914d\u5408\u4f7f\u7528\u3002\u5b83\u4f7f\u7528 aes-256-cbc \u7b97\u6cd5\u8fdb\u884c\u5b89\u5168\u6570\u636e\u5904\u7406\u3002 \u5b89\u88c5 \u8981\u5b89\u88c5 lithe crypt \u8f6f\u4ef6\u5305\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 \u3002\u5982\u679c\u60a8\u5c1a\u672a\u5b89\u88c5\uff0c\u8bf7\u786e\u4fdd composer \u5728\u60a8\u7684\u7cfb\u7edf\u4e0a\u53ef\u7528\u3002\u7136\u540e\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a composer require lithemod\/crypt \u767b\u5f55\u540e\u590d\u5236 \u8981\u6c42 php 8 \u6216\u66f4\u9ad8\u7248\u672c \u5728 php \u5b89\u88c5\u4e2d\u542f\u7528 openssl \u6269\u5c55 \u4f7f\u7528 \u52a0\u8f7d\u73af\u5883\u53d8\u91cf \u5728\u4f7f\u7528 crypt \u7c7b\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u52a0\u8f7d\u73af\u5883\u53d8\u91cf\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u60a8\u7684 .env \u6587\u4ef6\uff1a use lithesupportenv; \/\/ carregar vari\u00e1veis de ambiente env::load(__dir__); \/\/ ajuste o caminho conforme necess\u00e1rio \u767b\u5f55\u540e\u590d\u5236 \u8bbe\u7f6eapp_key \u786e\u4fdd\u5df2\u8bbe\u7f6e app_key [&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-26019","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26019","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=26019"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26019\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}