{"id":1070152,"date":"2025-01-08T10:55:33","date_gmt":"2025-01-08T02:55:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1070152.html"},"modified":"2025-01-08T10:55:35","modified_gmt":"2025-01-08T02:55:35","slug":"%e8%bf%94%e5%9b%9e%e6%8e%a5%e5%8f%a3%e6%95%b0%e6%8d%ae%e5%a6%82%e4%bd%95%e5%9c%a8python%e8%a7%a3%e5%af%86des-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1070152.html","title":{"rendered":"\u8fd4\u56de\u63a5\u53e3\u6570\u636e\u5982\u4f55\u5728python\u89e3\u5bc6DES"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100956\/c856ff77-2444-4be0-a6ec-b5a41e1cbf8c.webp\" alt=\"\u8fd4\u56de\u63a5\u53e3\u6570\u636e\u5982\u4f55\u5728python\u89e3\u5bc6DES\" \/><\/p>\n<p><p> <strong>\u8fd4\u56de\u63a5\u53e3\u6570\u636e\u5982\u4f55\u5728Python\u89e3\u5bc6DES<\/strong> <\/p>\n<\/p>\n<p><p><strong>\u8fd4\u56de\u63a5\u53e3\u6570\u636e\u5728Python\u4e2d\u89e3\u5bc6DES\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u83b7\u53d6\u5bc6\u94a5\u548cIV\u3001\u4f7f\u7528PyCryptodome\u5e93\u8fdb\u884c\u89e3\u5bc6\u3001\u5904\u7406\u89e3\u5bc6\u540e\u7684\u6570\u636e\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528PyCryptodome\u5e93\u8fdb\u884c\u89e3\u5bc6<\/strong> \u662f\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00\u3002PyCryptodome\u5e93\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u52a0\u5bc6\u5e93\uff0c\u652f\u6301\u591a\u79cd\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5305\u62ecDES\u3002\u4f60\u53ef\u4ee5\u5229\u7528\u5b83\u6765\u89e3\u5bc6DES\u52a0\u5bc6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u83b7\u53d6\u5bc6\u94a5\u548cIV<\/h3>\n<\/p>\n<p><p>\u5728\u89e3\u5bc6\u6570\u636e\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u83b7\u53d6\u5bc6\u94a5\uff08key\uff09\u548c\u521d\u59cb\u5316\u5411\u91cf\uff08IV\uff09\u3002\u8fd9\u4e24\u4e2a\u53c2\u6570\u5bf9\u4e8eDES\u89e3\u5bc6\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5bc6\u94a5\u548cIV\u7684\u957f\u5ea6\u5206\u522b\u4e3a8\u5b57\u8282\uff0864\u4f4d\uff09\uff0c\u5b83\u4eec\u9700\u8981\u4e0e\u52a0\u5bc6\u65f6\u4f7f\u7528\u7684\u5bc6\u94a5\u548cIV\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">key = b&#39;your_key_here&#39;  # 8\u5b57\u8282\u5bc6\u94a5<\/p>\n<p>iv = b&#39;your_iv_here&#39;  # 8\u5b57\u8282IV<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PyCryptodome\u5e93\u8fdb\u884c\u89e3\u5bc6<\/h3>\n<\/p>\n<p><p>PyCryptodome\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u52a0\u5bc6\u5e93\uff0c\u652f\u6301\u591a\u79cd\u52a0\u5bc6\u548c\u89e3\u5bc6\u7b97\u6cd5\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5PyCryptodome\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pycryptodome<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6765\u89e3\u5bc6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from Crypto.Cipher import DES<\/p>\n<p>from Crypto.Util.Padding import unpad<\/p>\n<p>def des_decrypt(ciphertext, key, iv):<\/p>\n<p>    des = DES.new(key, DES.MODE_CBC, iv)<\/p>\n<p>    pl<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ntext = des.decrypt(ciphertext)<\/p>\n<p>    return unpad(plaintext, DES.block_size)<\/p>\n<p>ciphertext = b&#39;your_encrypted_data_here&#39;<\/p>\n<p>decrypted_data = des_decrypt(ciphertext, key, iv)<\/p>\n<p>print(decrypted_data.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86DES\u7684CBC\u6a21\u5f0f\uff0c\u5e76\u4f7f\u7528<code>unpad<\/code>\u51fd\u6570\u6765\u5904\u7406\u89e3\u5bc6\u540e\u7684\u6570\u636e\uff0c\u56e0\u4e3aDES\u52a0\u5bc6\u65f6\u53ef\u80fd\u4f1a\u6dfb\u52a0\u586b\u5145\uff08padding\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u89e3\u5bc6\u540e\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u89e3\u5bc6\u540e\u7684\u6570\u636e\u901a\u5e38\u662f\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u793a\u4f8b\u5df2\u7ecf\u5305\u62ec\u4e86\u89e3\u5bc6\u540e\u7684\u6570\u636e\u8f6c\u6362\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from Crypto.Cipher import DES<\/p>\n<p>from Crypto.Util.Padding import unpad<\/p>\n<p>def des_decrypt(ciphertext, key, iv):<\/p>\n<p>    des = DES.new(key, DES.MODE_CBC, iv)<\/p>\n<p>    plaintext = des.decrypt(ciphertext)<\/p>\n<p>    return unpad(plaintext, DES.block_size)<\/p>\n<h2><strong>\u793a\u4f8b\u5bc6\u94a5\u548cIV<\/strong><\/h2>\n<p>key = b&#39;12345678&#39;  # 8\u5b57\u8282\u5bc6\u94a5<\/p>\n<p>iv = b&#39;abcdefgh&#39;   # 8\u5b57\u8282IV<\/p>\n<h2><strong>\u793a\u4f8b\u52a0\u5bc6\u6570\u636e<\/strong><\/h2>\n<p>ciphertext = b&#39;\\x8c\\xaa\\x1c\\xfa\\xeb\\x11\\xd5\\x89&#39;  # \u793a\u4f8b\u52a0\u5bc6\u6570\u636e<\/p>\n<h2><strong>\u89e3\u5bc6\u6570\u636e<\/strong><\/h2>\n<p>decrypted_data = des_decrypt(ciphertext, key, iv)<\/p>\n<p>print(decrypted_data.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5bc6\u94a5\u548cIV\u7684\u5b89\u5168\u6027<\/strong>\uff1a\u5bc6\u94a5\u548cIV\u5fc5\u987b\u4fdd\u5bc6\uff0c\u4e0d\u80fd\u6cc4\u9732\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u52a0\u5bc6\u6570\u636e\u7684\u4e0d\u5b89\u5168\u3002<\/li>\n<li><strong>\u586b\u5145\u65b9\u5f0f<\/strong>\uff1aDES\u52a0\u5bc6\u65f6\u53ef\u80fd\u4f1a\u6dfb\u52a0\u586b\u5145\uff0c\u89e3\u5bc6\u65f6\u9700\u8981\u6b63\u786e\u5904\u7406\u586b\u5145\u6570\u636e\u3002PyCryptodome\u5e93\u4e2d\u7684<code>unpad<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u586b\u5145\u3002<\/li>\n<li><strong>\u6570\u636e\u683c\u5f0f<\/strong>\uff1a\u786e\u4fdd\u8f93\u5165\u7684\u6570\u636e\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u89e3\u5bc6\u540e\u7684\u6570\u636e\u9700\u8981\u6839\u636e\u9700\u8981\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u683c\u5f0f\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u6210\u529f\u5b9e\u73b0Python\u4e2dDES\u52a0\u5bc6\u6570\u636e\u7684\u89e3\u5bc6\u3002\u5173\u952e\u5728\u4e8e\u6b63\u786e\u83b7\u53d6\u5bc6\u94a5\u548cIV\uff0c\u5e76\u4f7f\u7528PyCryptodome\u5e93\u8fdb\u884c\u89e3\u5bc6\u3002\u89e3\u5bc6\u540e\u7684\u6570\u636e\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u987a\u5229\u89e3\u5bc6DES\u52a0\u5bc6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528DES\u7b97\u6cd5\u8fdb\u884c\u89e3\u5bc6\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u4f7f\u7528DES\u7b97\u6cd5\u8fdb\u884c\u89e3\u5bc6\u901a\u5e38\u4f9d\u8d56\u4e8e<code>pycryptodome<\/code>\u5e93\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install pycryptodome<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6765\u8fdb\u884c\u89e3\u5bc6\uff1a<\/p>\n<pre><code class=\"language-python\">from Crypto.Cipher import DES\nfrom Crypto.Util.Padding import unpad\n\nkey = b&#39;yourkey12&#39;  # DES\u5bc6\u94a5\u5fc5\u987b\u662f8\u4e2a\u5b57\u8282\nciphertext = b&#39;yourciphertext&#39;  # \u9700\u8981\u89e3\u5bc6\u7684\u5bc6\u6587\n\n# \u521b\u5efaDES\u89e3\u5bc6\u5668\ncipher = DES.new(key, DES.MODE_CBC, iv=b&#39;youriv123&#39;)  # iv\u4e3a\u521d\u59cb\u5316\u5411\u91cf\n\n# \u89e3\u5bc6\u5e76\u53bb\u9664\u586b\u5145\ndecrypted_data = unpad(cipher.decrypt(ciphertext), DES.block_size)\nprint(decrypted_data.decode(&#39;utf-8&#39;))\n<\/code><\/pre>\n<p>\u8bf7\u786e\u4fdd\u66ff\u6362<code>key<\/code>\u548c<code>iv<\/code>\u4e3a\u60a8\u7684\u5b9e\u9645\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf\uff0c\u540c\u65f6<code>ciphertext<\/code>\u5e94\u4e3a\u60a8\u8981\u89e3\u5bc6\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u89e3\u5bc6\u8fc7\u7a0b\u4e2d\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><\/p>\n<p>\u89e3\u5bc6\u65f6\u9700\u786e\u4fdd\u5bc6\u94a5\u548c\u521d\u59cb\u5316\u5411\u91cf\uff08IV\uff09\u4e0e\u52a0\u5bc6\u65f6\u4f7f\u7528\u7684\u76f8\u540c\u3002DES\u7b97\u6cd5\u7684\u5bc6\u94a5\u957f\u5ea6\u56fa\u5b9a\u4e3a8\u5b57\u8282\uff0c\u56e0\u6b64\u9700\u786e\u4fdd\u63d0\u4f9b\u7684\u5bc6\u94a5\u7b26\u5408\u8fd9\u4e00\u8981\u6c42\u3002\u540c\u65f6\uff0c\u89e3\u5bc6\u7684\u6570\u636e\u53ef\u80fd\u542b\u6709\u586b\u5145\u5185\u5bb9\uff0c\u4f7f\u7528\u5408\u9002\u7684\u586b\u5145\u65b9\u5f0f\uff08\u5982PKCS7\uff09\u53ef\u4ee5\u5e2e\u52a9\u6b63\u786e\u53bb\u9664\u8fd9\u4e9b\u586b\u5145\u3002\u82e5\u89e3\u5bc6\u7ed3\u679c\u4e0d\u6b63\u786e\uff0c\u9700\u68c0\u67e5\u52a0\u5bc6\u8fc7\u7a0b\u4e2d\u7684\u53c2\u6570\u662f\u5426\u4e00\u81f4\u3002<\/p>\n<p><strong>DES\u7b97\u6cd5\u662f\u5426\u5b89\u5168\uff0c\u662f\u5426\u6709\u66f4\u597d\u7684\u66ff\u4ee3\u65b9\u6848\uff1f<\/strong><\/p>\n<p>DES\u7b97\u6cd5\u7531\u4e8e\u5176\u8f83\u77ed\u7684\u5bc6\u94a5\u957f\u5ea6\uff08\u4ec58\u5b57\u8282\uff09\u5728\u73b0\u4ee3\u5b89\u5168\u6807\u51c6\u4e0b\u88ab\u8ba4\u4e3a\u4e0d\u591f\u5b89\u5168\uff0c\u5bb9\u6613\u53d7\u5230\u66b4\u529b\u7834\u89e3\u653b\u51fb\u3002\u63a8\u8350\u4f7f\u7528\u66f4\u5b89\u5168\u7684\u7b97\u6cd5\uff0c\u5982AES\uff08\u9ad8\u7ea7\u52a0\u5bc6\u6807\u51c6\uff09\uff0c\u5176\u652f\u6301\u591a\u79cd\u5bc6\u94a5\u957f\u5ea6\uff08\u5982128\u3001192\u548c256\u4f4d\uff09\uff0c\u63d0\u4f9b\u66f4\u5f3a\u7684\u5b89\u5168\u6027\u3002\u5728\u9009\u62e9\u52a0\u5bc6\u65b9\u6848\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u7b26\u5408\u5f53\u524d\u5b89\u5168\u6807\u51c6\u7684\u7b97\u6cd5\u548c\u8db3\u591f\u5f3a\u7684\u5bc6\u94a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8fd4\u56de\u63a5\u53e3\u6570\u636e\u5982\u4f55\u5728Python\u89e3\u5bc6DES \u8fd4\u56de\u63a5\u53e3\u6570\u636e\u5728Python\u4e2d\u89e3\u5bc6DES\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u83b7\u53d6\u5bc6\u94a5\u548cIV\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1070161,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070152"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1070152"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070152\/revisions"}],"predecessor-version":[{"id":1070162,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070152\/revisions\/1070162"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1070161"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1070152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1070152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1070152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}