標籤:
簡介
功能: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]):使用 Base 64 編碼字串。s 是要編碼的字串。altchars 是用來替換+和/的字串,它們在 url 和檔案系統中它們有特殊含義,通常需要替換。
base64.b64decode(s[, altchars]): 解碼 Base 64 編碼的字串。s 為要解碼的字串。altchars 和b64encode 相同。
? base64.standard_b64encode ( s ) : 參考 b64encode。
? base64.standard_b64decode ( s ) :參考 b64decode。
Base 64 編碼解碼
Base 64 編碼解碼
#!/usr/bin/env python# encoding: utf-8## Copyright (c) 2008 Doug Hellmann All rights reserved.#""""""__version__ = "$Id$"#end_pymotw_headerimport base64import 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_initialprint ‘Expect %d padding bytes‘ % paddingprint ‘%d bytes after encoding‘ % len(encoded_data)printprint encoded_data
?執行結果
$ python base64_b64encode.py168 bytes before encodingExpect 3 padding bytes224 bytes after encodingCgppbXBvcnQgYmFzZTY0CmltcG9ydCB0ZXh0d3JhcAoKIyBMb2FkIHRoaXMgc291cmNlIGZpbGUgYW5kIHN0cmlwIHRoZSBoZWFkZXIuCndpdGggb3BlbihfX2ZpbGVfXywgJ3J0JykgYXMgaW5wdXQ6CiAgICByYXcgPSBpbnB1dC5yZWFkKCkKICAgIGluaXRpYWxfZGF0YSA9IHJhdy5zcGxpdCgn
Base 64 編碼的 4 個位元組對應實際的 3 個位元組,不足四個位元組時,後面部分通常用等號填充。極端的情況下,
一個位元組需要用 4 個 Base 64 編碼來表示。
>>> import base64>>> encoded = base64.b64encode(‘a‘)>>> encoded‘YQ==‘
Base64 解碼參見快速入門部分介紹。
URL-Safe
?base64.urlsafe_b64encode(s):
?base64.urlsafe_b64decode(s):
Base64 預設會使用+和/, 但是這 2 個字元在 url 中也有特殊含義。使用 urlsafe 可以解決這個問題。 +替換為-,
/替換為_。
import base64encodes_with_pluses = chr(251) + chr(239)encodes_with_slashes = chr(255) * 2for 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.pyOriginal: ‘\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 base64original_string = ‘This is the data, in the clear.‘print ‘Original:‘, original_stringencoded_string = base64.b32encode(original_string)print ‘Encoded :‘, encoded_stringdecoded_string = base64.b32decode(encoded_string)print ‘Decoded :‘, decoded_string
?執行結果
$ python base64_base32.pyOriginal: 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 base64original_string = ‘This is the data, in the clear.‘print ‘Original:‘, original_stringencoded_string = base64.b16encode(original_string)print ‘Encoded :‘, encoded_stringdecoded_string = base64.b16decode(encoded_string)print ‘Decoded :‘, decoded_string
?
執行結果
$ python base64_base16.pyOriginal: This is the data, in the clear.Encoded : 546869732069732074686520646174612C20696E2074686520636C6561722EDecoded : 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)
作者部落格:http://my.oschina.net/u/1433482
類型:翻譯加整理
本文最新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
python 模組介紹 - Base16, Base32, Base64 資料編碼