{"id":996,"date":"2023-03-12T08:12:10","date_gmt":"2023-03-12T00:12:10","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=996"},"modified":"2023-04-29T13:20:09","modified_gmt":"2023-04-29T05:20:09","slug":"frontend-crypto-js-3des-encryption-and-decryption","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/frontend-crypto-js-3des-encryption-and-decryption\/","title":{"rendered":"\u524d\u7aef crypto-js 3DES \u52a0\u89e3\u5bc6"},"content":{"rendered":"<p>3DES\uff08\u53c8\u53ebTriple DES\uff09\u662f\u4e09\u91cd\u6570\u636e\u52a0\u5bc6\u7b97\u6cd5\uff08TDEA\uff0cTriple Data Encryption Algorithm\uff09\u5757\u5bc6\u7801\u7684\u901a\u79f0\u3002\u5b83\u76f8\u5f53\u4e8e\u662f\u5bf9\u6bcf\u4e2a\u6570\u636e\u5757\u5e94\u7528\u4e09\u6b21DES\u52a0\u5bc6\u7b97\u6cd5\u3002\u5bc6\u94a5\u957f\u5ea6\u662f128\u4f4d\uff0c192\u4f4d(bit)\uff0c\u5982\u679c\u5bc6\u7801\u4f4d\u6570\u5c11\u4e8e\u7b49\u4e8e64\u4f4d\uff0c\u52a0\u5bc6\u7ed3\u679c\u4e0eDES\u76f8\u540c\u3002\u539f\u7248DES\u5bb9\u6613\u88ab\u7834\u89e3\uff0c\u65b0\u76843DES\u51fa\u73b0\uff0c\u589e\u52a0\u4e86\u52a0\u5bc6\u5b89\u5168\u6027,\u907f\u514d\u88ab\u66b4\u529b\u7834\u89e3\u3002\u5b83\u540c\u6837\u662f\u5bf9\u79f0\u6027\u52a0\u5bc6\uff0c\u540c\u6837\u6d89\u53ca\u5230\u52a0\u5bc6\u7f16\u7801\u65b9\u5f0f\uff0c\u53ca\u586b\u5145\u65b9\u5f0f\u3002\u5305\u62ec<code>3DES-ECB<\/code>\u3001<code>3DES-CBC<\/code>\u3001<code>3DES-CTR<\/code>\u3001<code>3DES-OFB<\/code>\u3001<code>3DES-CFB<\/code>\u3002<\/p>\n<p><!-- more --><\/p>\n<p>JavaScript\u52a0\u5bc6\u5e93<code>Crypto-JS<\/code>\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/brix\/crypto-js\">https:\/\/github.com\/brix\/crypto-js<\/a><\/p>\n<p>CryptoJS (crypto.js) \u4e3a JavaScript \u63d0\u4f9b\u4e86\u5404\u79cd\u5404\u6837\u7684\u52a0\u5bc6\u7b97\u6cd5\u3002<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=UTF-8&quot; \/&gt;\n    &lt;title&gt;&lt;\/title&gt;\n\n    &lt;script src=&quot;.\/crypto-js\/src\/core.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/evpkdf.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/cipher-core.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/md5.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/aes.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/tripledes.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/enc-base64.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/enc-utf16.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/format-hex.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/hmac.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/lib-typedarrays.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/mode-cfb.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/mode-ctr.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/mode-ctr-gladman.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/mode-ecb.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/mode-ofb.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/pad-ansix923.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/pad-iso10126.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/pad-iso97971.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/pad-nopadding.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;.\/crypto-js\/src\/pad-zeropadding.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;https:\/\/cdn.staticfile.org\/jquery\/3.2.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;button id=&quot;md5&quot;&gt;MD5&lt;\/button&gt;\n&lt;button id=&quot;tripledes&quot;&gt;3DES&lt;\/button&gt;\n\n&lt;script&gt;\n\n$(function() {\n    $(&quot;#md5&quot;).click(function(){\n        console.log(&quot;\u6d4b\u8bd5&quot;);\n        \/\/MD5\n        var md5Str = CryptoJS.MD5(&quot;key&quot;).toString(CryptoJS.enc.Hex).toUpperCase();\n        console.log(&quot;MD5\u540e\u5f97\u5230\u7684\u5b57\u7b26\u4e32\uff1a%s&quot;, md5Str)\n        return false;\n    });\n\n    $(&quot;#tripledes&quot;).click(function(){\n        \/\/3DES\n        \/\/ Encrypt\n        var encrypted = CryptoJS.TripleDES.encrypt(&#039;AppBlog.CN&#039;, CryptoJS.enc.Utf8.parse(&#039;123456&#039;), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }).ciphertext\n        encrypted = CryptoJS.enc.Base64.stringify(encrypted);\n        console.log(&quot;3DES\u52a0\u5bc6\u5f97\u5230\u7684\u5b57\u7b26\u4e32\uff1a%s&quot;, encrypted)\n\n        \/\/ Decrypt\n        var decrypted = CryptoJS.DES.decrypt({\n                ciphertext: CryptoJS.enc.Base64.parse(encrypted)\n            }, CryptoJS.enc.Utf8.parse(&#039;123456&#039;), {\n                mode: CryptoJS.mode.ECB,\n                padding: CryptoJS.pad.Pkcs7\n            });\n        console.log(&quot;3DES\u89e3\u5bc6\u5f97\u5230\u7684\u5b57\u7b26\u4e32\uff1a%s&quot;, decrypted.toString(CryptoJS.enc.Utf8))\n\n        return false;\n    });\n});\n&lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>\u6ce8\uff1a\u5728\u4f7f\u7528crypto-js\u8fdb\u884c\u89e3\u5bc6\u7684\u65f6\u5019\uff0c\u53ef\u80fd\u4f1a\u62a5<code>Malformed UTF-8 data<\/code>\u9519\u8bef<\/p>\n<p>\u62a5\u9519\u539f\u56e0\uff1ades\u89e3\u5bc6\u65f6\uff0c\u5982\u679c\u52a0\u5bc6\u6570\u636e\u4e0d\u662f8\u7684\u6574\u6570\u500d\u5c31\u4f1a\u62a5\u4e0a\u8ff0\u9519\u8bef<br \/>\n\u89e3\u51b3\u529e\u6cd5\uff1a\u5c06\u6570\u636e\u52a0\u5bc6\u540e\uff0c\u518d\u8fdb\u884cbase64\u8fdb\u884c\u52a0\u5bc6\uff0c\u89e3\u5bc6\u65f6\u9996\u5148\u901a\u8fc7base64\u8fdb\u884c\u89e3\u5bc6\uff0c\u7136\u540e\u518d\u8fdb\u884cdes\u89e3\u5bc6\u5373\u53ef<\/p>\n","protected":false},"excerpt":{"rendered":"<p>3DES\uff08\u53c8\u53ebTriple DES\uff09\u662f\u4e09\u91cd\u6570\u636e\u52a0\u5bc6\u7b97\u6cd5\uff08TDEA\uff0cTriple Data Encryption [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[277,276],"class_list":["post-996","post","type-post","status-publish","format-standard","hentry","category-frontend-basic","tag-3des","tag-cryptojs"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/996","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=996"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/996\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}