Base64编码解码
base64.b64encode(s[,altchars]):使用Base64编码字符串。s是要编码的字符串。altchars是用来替换+和/的字符串,在url和文件系统中它们有特殊含义,通常需要替换。
base64.b64decode(s[,altchars]): 解码Base64编码的字符串。s为要解码的字符串。altchars和b64encode相同。
请看python模块介绍中的实例:
>>> import base64
>>> encoded =base64.b64encode('data to be encoded')
>>> encoded
'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data =base64.b64decode(encoded)
>>> data
'data to be encoded'
下面是维基百科中的实例,包含了填充符:
>>> import base64
>>> s = '''Man isdistinguished, not only by his reason, but by this singular passion from otheranimals, which is a lust of the mind, that by a perseverance of delight in thecontinued and indefatigable generation of knowledge, exceeds the shortvehemence of any carnal pleasure.'''
>>> encoded_data =base64.b64encode(s)
>>> print(encoded_data)
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=
>>>base64.b64decode(encoded_data)
'Man is distinguished, not only by hisreason, but by this singular passion from other animals, which is a lust of themind, that by a perseverance of delight in the continued and indefatigablegeneration of knowledge, exceeds the short vehemence of any carnal pleasure.'
Base64默认会使用+和/, 但是这2个字符在url中也有特殊含义。使用urlsafe可以解决这个问题。+替换为-,/替换为_。
import base64
encodes_with_pluses = chr(251)+ chr(239)
encodes_with_slashes = chr(255)*2
for originalin[ 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)
执行结果:
# python base64_urlsafe.py
Original : '\xfb\xef'
Standard encoding: ++8=
URL-safe encoding: --8=
Original : '\xff\xff'
Standard encoding: //8=
URL-safe encoding: __8=
类似函数base64.standard_b64encode(s),base64.standard_b64decode(s)。
Bsae32编码
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 theclear.
Encoded :KRUGS4ZANFZSA5DIMUQGIYLUMEWCA2LOEB2GQZJAMNWGKYLSFY======
Decoded : This is the data, in theclear.
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 theclear.
Encoded :546869732069732074686520646174612C20696E2074686520636C6561722E
Decoded : This is the data, in theclear.