{"id":171,"date":"2021-08-24T10:44:39","date_gmt":"2021-08-24T02:44:39","guid":{"rendered":"http:\/\/119.23.244.75\/?p=171"},"modified":"2021-09-27T13:44:00","modified_gmt":"2021-09-27T05:44:00","slug":"python%e5%8a%a0%e5%af%86%e8%a7%a3%e5%af%86aes","status":"publish","type":"post","link":"https:\/\/www.keioi.cn\/archives\/171","title":{"rendered":"python\u52a0\u5bc6\u89e3\u5bc6AES"},"content":{"rendered":"\n<p><strong>from Crypto.Cipher import AES<\/strong><\/p>\n\n\n\n<p><strong>from binascii import b2a_hex,a2b_hex&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>import base64<\/strong><\/p>\n\n\n\n<p><strong>def the_str16(st): #\u886516\u4f4d<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; st=zwencode(st)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; while len(st)%16!=0:<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; st=st+&#8217;\\0&#8242;<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; return str.encode(st)<\/strong><\/p>\n\n\n\n<p><strong>def zwencode(st): #\u7f16\u7801<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; st=base64.b64encode(st.encode(&#8216;utf-8&#8217;))<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; st=st.decode()<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; return st<\/strong><\/p>\n\n\n\n<p><strong>def zwdecode(st): #\u89e3\u7801<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; missing_padding = 4 &#8211; len(st) % 4<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; if missing_padding:<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; st += &#8216;=&#8217;* missing_padding<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; st=base64.b64decode(st)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; st=st.decode(&#8216;utf-8&#8217;)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; return st<\/strong><\/p>\n\n\n\n<p><strong>def encryt_ecb(key,st): #\u52a0\u5bc6<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; st=the_str16(st)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; key=the_str16(key)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; aes=AES.new(key,AES.MODE_ECB)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; text_str=aes.encrypt(st)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; return b2a_hex(text_str)<\/strong><\/p>\n\n\n\n<p><strong>def decryt_ecb(key,st): #\u89e3\u5bc6<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; key=the_str16(key)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; aes=AES.new(key,AES.MODE_ECB)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; text_str=aes.decrypt(a2b_hex(st))<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; return text_str<\/strong><\/p>\n\n\n\n<p><strong>if __name__==&#8217;__main__&#8217;:<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; isjiami=input(&#8216;\u8f93\u5165encode\u52a0\u5bc6\uff0cdecode\u89e3\u5bc6\uff1a&#8217;)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; if isjiami == &#8216;encode&#8217;:<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; text=input(&#8216;\u8f93\u5165\u8981\u52a0\u5bc6\u5b57\u7b26\uff1a&#8217;)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; key=input(&#8216;\u8f93\u5165key:&#8217;)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; try:<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(encryt_ecb(key,text).decode())<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; except:<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#8216;\u52a0\u5bc6\u5931\u8d25&#8217;)&nbsp; &nbsp; &nbsp; &nbsp;<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; if isjiami == &#8216;decode&#8217;:<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; text=input(&#8216;\u8f93\u5165\u8981\u89e3\u5bc6\u5b57\u7b26\uff1a&#8217;)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; key=input(&#8216;\u8f93\u5165key:&#8217;)<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; try:<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(zwdecode(decryt_ecb(key,text).decode()))<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; except:<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#8216;\u89e3\u5bc6\u5931\u8d25&#8217;)<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>from Crypto.Ciph&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,4],"tags":[11],"class_list":["post-171","post","type-post","status-publish","format-standard","hentry","category-python","category-4","tag-python"],"_links":{"self":[{"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/posts\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/comments?post=171"}],"version-history":[{"count":1,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":172,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/posts\/171\/revisions\/172"}],"wp:attachment":[{"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}