{"id":385,"date":"2021-08-25T14:33:00","date_gmt":"2021-08-25T14:33:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=385"},"modified":"2021-08-25T14:33:00","modified_gmt":"2021-08-25T14:33:00","slug":"385","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/08\/25\/385\/","title":{"rendered":"Python3 \u5b57\u7b26\u4e32base64\u7f16\u89e3\u7801\u7b14\u8bb0"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/4138256080.png\" alt=\"b64-fb.png\" title=\"b64-fb.png\"><\/p>\n<ul>\n<li>Base64\u5e38\u7528\u4e8e\u5728\u901a\u5e38\u5904\u7406\u6587\u672c\u6570\u636e\u7684\u573a\u5408\uff0c\u8868\u793a\u3001\u4f20\u8f93\u3001\u5b58\u50a8\u4e00\u4e9b\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5305\u62ecMIME\u7684\u7535\u5b50\u90ae\u4ef6\u53caXML\u7684\u4e00\u4e9b\u590d\u6742\u6570\u636e\u3002<\/li>\n<li>Base64 \u5e94\u7528\u4e3e\u4f8b\uff1a\u6e90\u7801<a href=\"https:\/\/github.com\/hongwenjun\/pillow_font\/blob\/main\/bitfont.py\">bitfont.py<\/a>\u628a2\u4e2a\u5b57\u4f53\u7528\u5230\u76844\u4e2a\u6587\u4ef6\u548c1\u5f20PNG\u56fe\u7247\u5185\u5d4c\u5230\u6e90\u7801\u4e2d\uff1b\u8fd8\u6709Adobe \u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u4e00\u822c\u628ajpg\u9884\u89c8\u56fe\u7f16\u7801\u4fdd\u5b58\u5728\u6587\u6863\u4e2d\u7684XML\u6570\u636e\u91cc<\/li>\n<\/ul>\n<h3>Python3 \u4e2d <code>Base64<\/code> \u6570\u636e\u7f16\u7801\u529f\u80fd:<\/h3>\n<ul>\n<li><code>base64.b64encode<\/code>(s, altchars=None)<br \/>\u5bf9 bytes-like object(\u5b57\u8282\u7c7b\u5bf9\u8c61) s \u8fdb\u884c Base64 \u7f16\u7801\uff0c\u5e76\u8fd4\u56de\u7f16\u7801\u540e\u7684 bytes\u3002<\/li>\n<li><code>base64.b64decode<\/code>(s, altchars=None, validate=False)<br \/>\u89e3\u7801 Base64 \u7f16\u7801\u8fc7\u7684 bytes-like object \u6216 ASCII \u5b57\u7b26\u4e32 s \u5e76\u8fd4\u56de\u89e3\u7801\u8fc7\u7684 bytes\u3002<\/li>\n<\/ul>\n<h3>\u6240\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u5148\u628a <code>utf-8<\/code> \u7684\u5b57\u7b26\u4e32\u5148\u8f6c\u6362\u6210 <code>bytes<\/code> \u7c7b\u578b\uff0c\u518d\u4f7f\u7528 <code>Base64<\/code> \u5e93\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801<\/h3>\n<pre><code>import base64\ns = \"www.python.org\"\nb = s.encode(\"utf-8\")  # s = b.decode('utf-8')\nb64 = base64.b64encode(b)\nprint(s, b, b64)\n#  www.python.org b'www.python.org' b'd3d3LnB5dGhvbi5vcmc='\n\nb64 = (\n    b\"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE\"\n    b\"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT\"\n    b\"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n\"\n)   # \u591a\u884c\u5b57\u7b26\u4e32\u4f7f\u7528\u5c0f\u62ec\u53f7\u65b9\u6cd5\n\nb = base64.b64decode(b64)\ns = b.decode(\"utf-8\")\nprint(b64)\nprint(b)\nprint(s)\n#  abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#0^&amp;*();:&lt;&gt;,. []{}<\/code><\/pre>\n<h3>\u7f16\u7801 <code>Man<\/code> \u7684\u7ed3\u679c\u4e3a <code>TWFu<\/code>\uff0c\u8be6\u7ec6\u539f\u7406\u5982\u4e0b\uff1a<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/512630127.png\" alt=\"1.PNG\" title=\"1.PNG\"><br \/>\u5728\u6b64\u4f8b\u4e2d\uff0cBase64\u7b97\u6cd5\u5c063\u4e2a\u5b57\u8282\u7f16\u7801\u4e3a4\u4e2a\u5b57\u7b26\u3002 \u8f6c\u7801\u4e00\u6b2124bit\uff1a3x8 =4x6  ; \u5185\u5b581\u4e2a\u5b57\u8282\u53608\u4f4d<\/p>\n<h3>Base64\u7d22\u5f15\u8868<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/3565207222.png\" alt=\"2.PNG\" title=\"2.PNG\"><\/p>\n<p>\u5982\u679c\u8981\u7f16\u7801\u7684\u5b57\u8282\u6570\u4e0d\u80fd\u88ab3\u6574\u9664\uff0c\u6700\u540e\u4f1a\u591a\u51fa1\u4e2a\u62162\u4e2a\u5b57\u8282\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\uff1a\u5148\u4f7f\u75280\u5b57\u8282\u503c\u5728\u672b\u5c3e\u8865\u8db3\uff0c\u4f7f\u5176\u80fd\u591f\u88ab3\u6574\u9664\uff0c\u7136\u540e\u518d\u8fdb\u884cBase64\u7684\u7f16\u7801\u3002\u5728\u7f16\u7801\u540e\u7684Base64\u6587\u672c\u540e\u52a0\u4e0a\u4e00\u4e2a\u6216\u4e24\u4e2a=\u53f7\uff0c\u4ee3\u8868\u8865\u8db3\u7684\u5b57\u8282\u6570\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6700\u540e\u5269\u4f59\u4e24\u4e2a\u516b\u4f4d(\u5f85\u8865\u8db3)\u5b57\u8282\uff082\u4e2abyte\uff09\u65f6\uff0c\u6700\u540e\u4e00\u4e2a6\u4f4d\u7684Base64\u5b57\u8282\u5757\u6709\u56db\u4f4d\u662f0\u503c\uff0c\u6700\u540e\u9644\u52a0\u4e0a\u4e24\u4e2a\u7b49\u53f7\uff1b\u5982\u679c\u6700\u540e\u5269\u4f59\u4e00\u4e2a\u516b\u4f4d(\u5f85\u8865\u8db3)\u5b57\u8282\uff081\u4e2abyte\uff09\u65f6\uff0c\u6700\u540e\u4e00\u4e2a6\u4f4d\u7684base\u5b57\u8282\u5757\u6709\u4e24\u4f4d\u662f0\u503c\uff0c\u6700\u540e\u9644\u52a0\u4e00\u4e2a\u7b49\u53f7\u3002 \u53c2\u8003\u4e0b\u8868\uff1a<br \/><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/3437138684.png\" alt=\"3.PNG\" title=\"3.PNG\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Base64\u5e38\u7528\u4e8e\u5728\u901a\u5e38\u5904\u7406\u6587\u672c\u6570\u636e\u7684\u573a\u5408\uff0c\u8868\u793a\u3001\u4f20\u8f93\u3001\u5b58\u50a8\u4e00\u4e9b\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5305\u62ecMIME\u7684\u7535\u5b50\u90ae\u4ef6\u53ca [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-385","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=385"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/385\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}