python 模塊介紹 - Base16, Base32, Base64 數據編碼
簡介
功能:RFC 3548: Base16, Base32, Base64 數據編碼。轉換二進制數據為適合明文協議傳輸的 ASCII 序列。轉換
8bits 為每個字節包含 6,5 或 4bits 的有效數據,比如 SMTP, URL 的一部分或者 HTTP POST 的一部分。參考:
RFC 3548。編碼算法不同于 uuencode。
類型:標準庫
相關模塊:uu, binhex, uu, quopri
Base64 是一種基于 64 個可打印字符來表示二進制數據的表示方法。由于 2 的 6 次方等于 64,所以每 6
個位元為一個單元,對應某個可打印字符。三個字節有 24 個位元,對應于 4 個 Base64 單元,即 3 個字節
需要用 4 個可打印字符來表示。它可用來作為電子郵件的傳輸編碼。在 Base64 中的可打印字符包括字母 A-
Z、a-z、數字 0-9,這樣共有 62 個字符,此外兩個可打印符號在不同的系統中而不同。之后在 6 位的前面補
兩個 0,形成 8 位一個字節的形式。一些如 uuencode 的其他編碼方法,和之后 binhex 的版本使用不同的
64 字符集來代表 6 個二進制數字,但是它們不叫 Base64。
Base64 常用于在通常處理文本數據的場合,表示、傳輸、存儲一些二進制數據。包括 MIME 的
email,email via MIME,在 XML 中存儲復雜數據。
Python Base64 模塊提供了 RFC3548 中的數據編碼和解碼(轉換二進制數據為適合明文協議傳輸的
ASCII 序列,如 RFC3548 中指定。該標準定義了 Base16,Base32 和 Base64 算法,編碼和解碼的任意二進制字符串轉換為文本字符串,這樣就可以通過電子郵件安全發送,作為網址的一部分,或包含在 HTTP POST 請求中。
Base64 模塊提供兩個接口。新式接口支持使用三個字母的編碼和解碼的字符串對象。傳統接口提供了編
碼和解碼文件對象和字符串,但只使用了標準的 Base64 字母。傳統接口這里不做介紹。
base64、 base32、 base16 可以分別編碼轉化 8 位字節為 6 位、 5 位、 4 位。 16,32,64 分別表示用多少個字
符來編碼。
更多 base64 的資料,參見
http://zh.wikipedia.org/wiki/Base64,http://tools.ietf.org/html/rfc822,http://tools.ietf.org/html/rfc14
21,http://tools.ietf.org/html/rfc2045。
快速入門
請看 python 模塊介紹中的實例:
>>> import base64 >>> encoded = base64.b64encode('data to be encoded') >>> encoded 'ZGF0YSB0byBiZSBlbmNvZGVk' >>> data = base64.b64decode(encoded) >>> data 'data to be encoded'
base64.b64encode(s[, altchars]):使用 Base64 編碼字符串。s 是要編碼的字符串。altchars 是用來替換+和/的字符串,它們在 url 和文件系統中它們有特殊含義,通常需要替換。
base64.b64decode(s[, altchars]): 解碼 Base64 編碼的字符串。s 為要解碼的字符串。altchars 和b64encode 相同。
? base64.standard_b64encode ( s ) : 參考 b64encode。
? base64.standard_b64decode ( s ) :參考 b64decode。
Base64 編碼解碼
Base64 編碼解碼
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2008 Doug Hellmann All rights reserved. # """ """ __version__ = "$Id$" #end_pymotw_header import base64 import textwrap # Load this source file and strip the header. with open(__file__, 'rt') as input: raw = input.read() initial_data = raw.split('#end_pymotw_header')[1] encoded_data = base64.b64encode(initial_data) num_initial = len(initial_data) # There will never be more than 2 padding bytes. padding = 3 - (num_initial % 3) print '%d bytes before encoding' % num_initial print 'Expect %d padding bytes' % padding print '%d bytes after encoding' % len(encoded_data) print print encoded_data
?執行結果
$ python base64_b64encode.py 168 bytes before encoding Expect 3 padding bytes 224 bytes after encoding CgppbXBvcnQgYmFzZTY0CmltcG9ydCB0ZXh0d3JhcAoKIyBMb2FkIHRoaXMgc291cmNlIGZpbGUgYW5kIHN0cmlwIHRoZSBoZWFk ZXIuCndpdGggb3BlbihfX2ZpbGVfXywgJ3J0JykgYXMgaW5wdXQ6CiAgICByYXcgPSBpbnB1dC5yZWFkKCkKICAgIGluaXRpYWxfZGF0 YSA9IHJhdy5zcGxpdCgn
Base64 編碼的 4 個字節對應實際的 3 個字節,不足四個字節時,后面部分通常用等號填充。極端的情況下,
一個字節需要用 4 個 Base64 編碼來表示。
>>> import base64 >>> encoded = base64.b64encode('a') >>> encoded 'YQ=='
Base64 解碼參見快速入門部分介紹。
URL-Safe
?base64.urlsafe_b64encode(s):
?base64.urlsafe_b64decode(s):
Base64 默認會使用+和/, 但是這 2 個字符在 url 中也有特殊含義。使用 urlsafe 可以解決這個問題。 +替換為-,
/替換為_。
import base64 encodes_with_pluses = chr(251) + chr(239) encodes_with_slashes = chr(255) * 2 for original in [ encodes_with_pluses, encodes_with_slashes ]: print 'Original :', repr(original) print 'Standard encoding:', base64.standard_b64encode(original) print 'URL-safe encoding:', base64.urlsafe_b64encode(original) print
?執行結果
$ python base64_urlsafe.py Original : '\xfb\xef' Standard encoding: ++8= URL-safe encoding: --8= Original : '\xff\xff' Standard encoding: //8= URL-safe encoding: __8=
其他編碼
Base32 包含 26 個大寫字母和 2-7 的數字。
? base64.b32encode(s):使用 Base32 編碼字符串。s 是要編碼的字符串。
? base64.b32decode(s[, casefold[, map01]]):解碼 Base32 編碼的字符串。s 為要解碼的字符串 。
casefold 表示是否允許小寫字母。 map01 表示允許 0 表示 0,1 表示 L 。
import base64 original_string = 'This is the data, in the clear.' print 'Original:', original_string encoded_string = base64.b32encode(original_string) print 'Encoded :', encoded_string decoded_string = base64.b32decode(encoded_string) print 'Decoded :', decoded_string
?執行結果
$ python base64_base32.py Original: This is the data, in the clear. Encoded : KRUGS4ZANFZSA5DIMUQGIYLUMEWCA2LOEB2GQZJAMNWGKYLSFY====== Decoded : This is the data, in the clear.
Base16 包含 16 個 16 進制大寫數字。類似的有 base64.b16encode(s) ,base64.b16decode(s[,
casefold]) 。
import base64 original_string = 'This is the data, in the clear.' print 'Original:', original_string encoded_string = base64.b16encode(original_string) print 'Encoded :', encoded_string decoded_string = base64.b16decode(encoded_string) print 'Decoded :', decoded_string
?
執行結果
$ python base64_base16.py Original: This is the data, in the clear. Encoded : 546869732069732074686520646174612C20696E2074686520636C6561722E Decoded : This is the data, in the clear. Python3.4 中增加了 Ascii85 和 base85 支持 。這里暫不做詳細介紹。函數如下: ? base64.a85encode(s, *, foldspaces=False, wrapcol=0, pad=False, adobe=False) ? base64.a85decode(s, *, foldspaces=False, adobe=False, ignorechars=b' tnrv') ? base64.b85encode(s, pad=False) ? base64.b85decode(b)
-
類型:翻譯加整理
-
本文最新pdf版本下載。本文最初基于libreoffice的odt格式創建,拷貝過來格式有部分變化,建議查看原來的pdf。
-
python2 官方網址:http://docs.python.org/2/library/base64.html
-
python3 官方網址:https://docs.python.org/3/library/base64.html
-
python 標準庫 pymotw:http://pymotw.com/2/base64/index.html#module-base64
來自:http://my.oschina.net/u/1433482/blog/465371