{"id":919,"date":"2024-11-07T14:14:05","date_gmt":"2024-11-07T06:14:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/919\/"},"modified":"2024-11-07T14:14:05","modified_gmt":"2024-11-07T06:14:05","slug":"python%e4%b8%8ejava-aes%e5%8a%a0%e5%af%86%e5%b7%ae%e5%bc%82%ef%bc%9a%e5%af%86%e9%92%a5%e9%95%bf%e5%ba%a6%e4%b8%8d%e8%b6%b316%e5%ad%97%e8%8a%82%e6%97%b6%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/919\/","title":{"rendered":"Python\u4e0eJava AES\u52a0\u5bc6\u5dee\u5f02\uff1a\u5bc6\u94a5\u957f\u5ea6\u4e0d\u8db316\u5b57\u8282\u65f6\u5982\u4f55\u5904\u7406\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-985\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173069973864987.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173069973864987.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173069973864987-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173069973864987-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173069973864987-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Python\u4e0eJava AES\u52a0\u5bc6\u5dee\u5f02\uff1a\u5bc6\u94a5\u957f\u5ea6\u4e0d\u8db316\u5b57\u8282\u65f6\u5982\u4f55\u5904\u7406\uff1f\u63d2\u56fe\" alt=\"Python\u4e0eJava AES\u52a0\u5bc6\u5dee\u5f02\uff1a\u5bc6\u94a5\u957f\u5ea6\u4e0d\u8db316\u5b57\u8282\u65f6\u5982\u4f55\u5904\u7406\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python\u4e0b\u7684aes\u52a0\u5bc6\u4e0ejava\u7684\u5dee\u5f02<\/strong><\/p>\n<p>\u5728java\u4ee3\u7801\u4e2d\uff0caes\u5bc6\u94a5\u957f\u5ea6\u4e0d\u8db316\u4e2a\u5b57\u8282\u65f6\uff0c\u4f1a\u91c7\u7528\u8865\u4f4d\u65b9\u5f0f\u8865\u5145\u523016\u4e2a\u5b57\u8282\u3002\u800c\u5728python\u4e2d\uff0cpycryptodome\u6a21\u5757\u5bf9\u52a0\u5bc6\u5185\u5bb9\u6709\u8981\u6c42\uff0c\u5373\u52a0\u5bc6\u5185\u5bb9\u8981\u4e3a16\u7684\u500d\u6570\u3002<\/p>\n<p><strong>\u539f\u56e0\u5206\u6790<\/strong><\/p>\n<p>python\u7684pycryptodome\u6a21\u5757\u91c7\u7528\u4e86aes\u7684ecb\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u8981\u6c42\u52a0\u5bc6\u5185\u5bb9\u630916\u5b57\u8282\u5757\u8fdb\u884c\u52a0\u5bc6\u3002\u56e0\u6b64\uff0c\u5982\u679c\u52a0\u5bc6\u5185\u5bb9\u4e0d\u662f16\u7684\u500d\u6570\uff0c\u9700\u8981\u8865\u8db3\u523016\u7684\u500d\u6570\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cJava\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u4f7fpython\u4e0b\u7684aes\u52a0\u5bc6\u7ed3\u679c\u4e0ejava\u4e00\u81f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u6cd5\u8865\u8db3\u52a0\u5bc6\u5185\u5bb9\uff1a<\/p>\n<pre>import binascii\nimport os\n\n# \u751f\u621016\u5b57\u8282\u7684\u968f\u673a\u5bc6\u94a5\nkey = os.urandom(16)\n\n# \u8865\u8db3\u52a0\u5bc6\u5185\u5bb9\u523016\u7684\u500d\u6570\nplaintext = \"aaaaaa\"\nplaintext_bytes = plaintext.encode('utf-8')\npadding_length = 16 - (len(plaintext_bytes) % 16)\nplaintext_bytes += b'\u0000' * padding_length\n\n# AES\u52a0\u5bc6\nciphertext = AES.new(key, AES.MODE_ECB).encrypt(plaintext_bytes)\n\n# \u8f93\u51fa\u5bc6\u6587\nciphertext_hex = binascii.hexlify(ciphertext)\nprint(\"\u52a0\u5bc6\u540e\u7684\u5bc6\u6587\uff1a\", ciphertext_hex)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fddpython\u4e0b\u7684aes\u52a0\u5bc6\u7ed3\u679c\u4e0ejava\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fPython\u4e0eJava AES\u52a0\u5bc6\u5dee\u5f02\uff1a\u5bc6\u94a5\u957f\u5ea6\u4e0d\u8db316\u5b57\u8282\u65f6\u5982\u4f55\u5904\u7406\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>python\u4e0b\u7684aes\u52a0\u5bc6\u4e0ejava\u7684\u5dee\u5f02 \u5728java\u4ee3\u7801\u4e2d\uff0caes\u5bc6\u94a5\u957f\u5ea6\u4e0d\u8db316\u4e2a\u5b57\u8282\u65f6\uff0c\u4f1a\u91c7\u7528\u8865\u4f4d\u65b9\u5f0f\u8865\u5145\u523016\u4e2a\u5b57\u8282\u3002\u800c\u5728python\u4e2d\uff0cpycryptodome\u6a21\u5757\u5bf9\u52a0\u5bc6\u5185\u5bb9\u6709\u8981\u6c42\uff0c\u5373\u52a0\u5bc6\u5185\u5bb9\u8981\u4e3a16\u7684\u500d\u6570\u3002 \u539f\u56e0\u5206\u6790 python\u7684pycryptodome\u6a21\u5757\u91c7\u7528\u4e86aes\u7684ecb\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u8981\u6c42\u52a0\u5bc6\u5185\u5bb9\u630916\u5b57\u8282\u5757\u8fdb\u884c\u52a0\u5bc6\u3002\u56e0\u6b64\uff0c\u5982\u679c\u52a0\u5bc6\u5185\u5bb9\u4e0d\u662f16\u7684\u500d\u6570\uff0c\u9700\u8981\u8865\u8db3\u523016\u7684\u500d\u6570\u3002 \u7acb\u5373\u5b66\u4e60\u201cJava\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b \u89e3\u51b3\u65b9\u6cd5 \u4e3a\u4e86\u4f7fpython\u4e0b\u7684aes\u52a0\u5bc6\u7ed3\u679c\u4e0ejava\u4e00\u81f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u6cd5\u8865\u8db3\u52a0\u5bc6\u5185\u5bb9\uff1a import binascii import os # \u751f\u621016\u5b57\u8282\u7684\u968f\u673a\u5bc6\u94a5 key = os.urandom(16) # \u8865\u8db3\u52a0\u5bc6\u5185\u5bb9\u523016\u7684\u500d\u6570 plaintext = &#8220;aaaaaa&#8221; plaintext_bytes = plaintext.encode(&#8216;utf-8&#8217;) padding_length = 16 &#8211; (len(plaintext_bytes) % 16) plaintext_bytes += b&#8221; * padding_length # AES\u52a0\u5bc6 ciphertext = AES.new(key, AES.MODE_ECB).encrypt(plaintext_bytes) # \u8f93\u51fa\u5bc6\u6587 ciphertext_hex = binascii.hexlify(ciphertext) print(&#8220;\u52a0\u5bc6\u540e\u7684\u5bc6\u6587\uff1a&#8221;, ciphertext_hex) \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fddpython\u4e0b\u7684aes\u52a0\u5bc6\u7ed3\u679c\u4e0ejava\u4fdd\u6301\u4e00\u81f4\u3002 \u4ee5\u4e0a\u5c31\u662fPython\u4e0eJava AES\u52a0\u5bc6\u5dee\u5f02\uff1a\u5bc6\u94a5\u957f\u5ea6\u4e0d\u8db316\u5b57\u8282\u65f6\u5982\u4f55\u5904\u7406\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},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-919","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/919","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=919"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/919\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}