簡介
功能: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/rfc1421
http://tools.ietf.org/html/rfc2045
快速入門
請看 python 模塊介紹中的實體:
>>> import base64
>>> encoded = base64.b64encode('data to be encoded')
>>> encoded
'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data = https://www.cnblogs.com/testing-/archive/2023/05/18/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 = https://www.cnblogs.com/testing-/archive/2023/05/18/raw.split('#end_pymotw_header')[1]
encoded_data = https://www.cnblogs.com/testing-/archive/2023/05/18/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)
參考資料
- 軟體測驗精品書籍檔案下載持續更新 https://github.com/china-testing/python-testing-examples 請點贊,謝謝!
- 本文涉及的python測驗開發庫 謝謝點贊! https://github.com/china-testing/python_cn_resouce
- python精品書籍下載 https://github.com/china-testing/python_cn_resouce/blob/main/python_good_books.md
- python3 官方網址:https://docs.python.org/3/library/base64.html
- python 標準庫 https://pymotw.com/3/base64/index.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/552819.html
標籤:其他
下一篇:返回列表