{"id":610,"date":"2023-02-26T10:47:07","date_gmt":"2023-02-26T02:47:07","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=610"},"modified":"2023-04-29T20:23:04","modified_gmt":"2023-04-29T12:23:04","slug":"php-implement-3des-encryption-and-decryption","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/php-implement-3des-encryption-and-decryption\/","title":{"rendered":"PHP\u5b9e\u73b03DES\u52a0\u89e3\u5bc6"},"content":{"rendered":"<p><code>CBC<\/code>\u6a21\u5f0f\u4e00\u5b9a\u8981\u6307\u5b9aIV\u52a0\u5bc6\u5411\u91cf<br \/>\n<code>ECB<\/code>\u6a21\u5f0f\u4e0d\u9700\u8981\u6307\u5b9aIV\u52a0\u5bc6\u5411\u91cf<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-php\">class TripleDes {\n    \/**\n     * @var string\n     *\/\n    var $key = &#039;&#039;;\n\n    \/**\n     * @var string\n     *\/\n    var $iv = &#039;&#039;;\n\n    \/**\n     * Des3 constructor.\n     * @param string|null $key\n     * @param string|null $iv\n     *\/\n    public function __construct(string $key=null, string $iv=null)\n    {\n        if (!$key &amp;&amp; strlen($key) != 24) {\n            throw new \\Exception(&quot;3DES_KEY\u957f\u5ea6\u9519\u8bef\uff0c\u957f\u5ea6\u4e3a24&quot;);\n        }\n        if (!$iv &amp;&amp; strlen($iv) != 8) {\n            throw new \\Exception(&quot;3DES_IV\u957f\u5ea6\u9519\u8bef\uff0c\u957f\u5ea6\u4e3a8&quot;);\n        }\n        $this-&gt;key = $key;\n        $this-&gt;iv = $iv;\n    }\n\n    public function encrypt($input, $key=&#039;&#039;, $iv=&#039;&#039;) {\n        $this-&gt;key = $key ? $key : $this-&gt;key;\n        $this-&gt;iv = $iv ? $iv : $this-&gt;iv;\n        \/\/return base64_encode(openssl_encrypt($input, &quot;des-ede3-cbc&quot;, $this-&gt;key, OPENSSL_RAW_DATA, $this-&gt;iv));\n        return base64_encode(openssl_encrypt($input, &quot;des-ede3&quot;, $this-&gt;key, OPENSSL_RAW_DATA));\n    }\n\n    public function decrypt($encrypted, $key=&#039;&#039;, $iv=&#039;&#039;) {\n        $this-&gt;key = $key ? $key : $this-&gt;key;\n        $this-&gt;iv = $iv ? $iv : $this-&gt;iv;\n        \/\/return openssl_decrypt(base64_decode($encrypted), &#039;des-ede3-cbc&#039;, $this-&gt;key, OPENSSL_RAW_DATA, $this-&gt;iv);\n        return openssl_decrypt(base64_decode($encrypted), &#039;des-ede3&#039;, $this-&gt;key, OPENSSL_RAW_DATA);\n    }\n}<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\uff081\uff093DES\u5bc6\u94a5\u7684\u957f\u5ea6\u5fc5\u987b\u662f8\u7684\u500d\u6570\uff0c\u53ef\u53d624\u4f4d\u621632\u4f4d\uff1b\uff082\uff09\u52a0\u5bc6\u7ed3\u679c\u7684byte\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4e00\u822c\u91c7\u7528\u4e24\u79cd\u65b9\u5f0f\uff1aBase64\u5904\u7406\u6216\u5341\u516d\u8fdb\u5236\u5904\u7406\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>CBC\u6a21\u5f0f\u4e00\u5b9a\u8981\u6307\u5b9aIV\u52a0\u5bc6\u5411\u91cf ECB\u6a21\u5f0f\u4e0d\u9700\u8981\u6307\u5b9aIV\u52a0\u5bc6\u5411\u91cf class TripleDes { \/** [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[56],"class_list":["post-610","post","type-post","status-publish","format-standard","hentry","category-php-basic","tag-des"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=610"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/610\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}