{"id":204319,"date":"2025-05-29T09:02:01","date_gmt":"2025-05-29T01:02:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204319\/"},"modified":"2025-05-29T09:02:01","modified_gmt":"2025-05-29T01:02:01","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:\/\/server.hk\/cnblog\/204319\/","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><b><\/b>     <\/p>\n<h1>Python\u4e0eJava AES\u52a0\u5bc6\u5dee\u5f02\uff1a\u5bc6\u94a5\u957f\u5ea6\u4e0d\u8db316\u5b57\u8282\u65f6\u5982\u4f55\u5904\u7406\uff1f<\/h1>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241104\/1730699688672861a835660.jpg\" class=\"aligncenter\"><\/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><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'\\0' * 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>\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\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e0eJava AES\u52a0&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204319","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204319"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204319\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}