Python中的base64、base32实例
来源:互联网 发布:淘宝天猫怎么开店 编辑:程序博客网 时间:2024/06/07 10:27
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.
- Python中的base64、base32实例
- 如何用python解码base32/base64
- python解base16,base32,base64全家桶
- Python | MD5爆破及Base64/Base32爆破解密脚本
- Base64 Base32 Base16全家桶
- python实例27[base64]
- Base64 | Base32 | Base16编码和解码小结
- base32
- python3.x中如何使用base64、base32、base16编码解码
- base家族:base16、base32和base64,转码原理
- base64编码原理以及python中的base64模块
- python base64
- Base64编码在网络图片传输中的应用实例
- Base32编码
- base32 加密
- Python中的Module用法实例
- Python 3 中的 urllib 实例
- python中的二维列表(实例)
- 问题解决: @Resource annotation is not supported on static fields
- SQL之正则表达式
- 认识.NET
- NDK编译X265库到ARM,遇到问题
- 替换字符串String中的元素和分割字符串为数组
- Python中的base64、base32实例
- 云计算容器服务该何去何从
- android studio 目录结构
- am335x在ubuntu下使用StarterWare编写裸机程序并在CCS中用Jlink调试
- 我所理解lua 语言中的点、冒号与self
- Remove Duplicates from Sorted List II
- 数据结构与算法分析 L5
- 命名规则
- Android异常:requestFeature() must be called before adding content解决办法