python内置模块base64模块,base64编码解码

一、Python的标准库

base64模块是用来作base64编码解码,常用于小型数据的传输。编码后的数据是一个字符串,其包括a-z、A-Z、0-9、/、+共64个字符,即可用6个字节表示,写出数值就是0-63.故三个字节编码的话就变成了4个字节,如果数据字节数不是3的倍数,就不能精确地划分6位的块,此时需要在原数据后添加1个或2个零值字节,使其字节数为3的倍数,然后在编码后的字符串后添加1个或2个‘=’,表示零值字节,所以事实上总共由65个字符组成

二、常用方法
  1. base64.b64encode(str, altchars=None):用于对字节字符串进行Base64编码。
  • str:字节字符串
  • altchars:替代字符集,用于替换Base64编码中的"+“和”/"字符。
  1. base64.b64decode(str, altchars=None, validate=False):用于对Base64编码的字节字符串进行解码。
  • str:字节字符串
  • altchars:替代字符集,用于替换Base64编码中的"+“和”/"字符。
  • validate:用于指定是否进行解码前的验证
import base64

org_str = 'hello word'

encode_str = base64.b64encode(org_str.encode())
decode_str = base64.b64decode(encode_str)

print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())

  1. base64.a85encode(str, foldspaces=True, wrapcol=0, pad=False, adobe=False):用于对字节字符串进行Ascii85编码。
  • str:字节字符串
  • foldspaces:是否折叠空格。
  • wrapco:每行的字符数限制。
  • pad:是否在编码后添加填充字符。
  • adobe:是否使用Adobe的Ascii85编码变体。
  1. base64.a85decode(str, foldspaces=True, adobe=False, ignorechars=’ \t\n\r\x0b’):用于对Ascii85编码的字节字符串进行解码。
  • str:字节字符串
  • foldspaces:是否折叠空格。
  • adobe:是否使用Adobe的Ascii85编码变体。
  • ignorechars:要忽略的字符集。
    import base64
import base64

org_str = 'hello word'

encode_str = base64.a85encode(org_str.encode())
decode_str = base64.a85decode(encode_str)

print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())
  1. base64.b16encode(str):用于对字节字符串进行十六进制编码。
  2. base64.b16decode(str, casefold=False):用于对十六进制编码的字节字符串进行解码。
  • str:字节字符串
  • casefold:是否将解码后的结果转换为小写。
import base64

org_str = 'hello word'

encode_str = base64.b16encode(org_str.encode())
decode_str = base64.b16decode(encode_str)

print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())
  1. base64.b32encode(str):用于对字节字符串进行Base32编码。
  2. base64.b32decode(str, casefold=False, map01=None):用于对Base32编码的字节字符串进行解码。
  • str:字节字符串
  • casefold:是否将解码后的结果转换为小写。
  • map01:自定义的Base32字符映射。
import base64

org_str = 'hello word'

encode_str = base64.b32encode(org_str.encode())
decode_str = base64.b32decode(encode_str)

print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())

  1. base64.b32hexencode(str):用于对字节字符串进行Base32hex编码。
  2. base64.b32hexdecode(str, casefold=False):用于对Base32hex编码的字节字符串进行解码。
  • str:字节字符串
  • casefold:是否将解码后的结果转换为小写。
import base64

org_str = 'hello word'

encode_str = base64.b32hexencode(org_str.encode())
decode_str = base64.b32hexdecode(encode_str)

print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())
  1. base64.b85decode(str):用于对Base85编码的字节字符串进行解码。
  2. base64.b85encode(str, pad=False):用于对字节字符串进行Base85编码。
  • str:字节字符串
  • pad:是否在编码后添加填充字符。
import base64

org_str = 'hello word'

encode_str = base64.b85encode(org_str.encode())
decode_str = base64.b85encode(encode_str)

print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())

  1. base64.encodebytes(str):用于对Base64编码的字节字符串进行编码。
  2. base64.decodebytes(str):用于对Base64编码的字节字符串进行解码。
import base64

org_str = 'hello word'

encode_str = base64.encodebytes(org_str.encode())
decode_str = base64.decodebytes(encode_str)

print('原字符串', org_str)
print('编码后的字符串', encode_str)
print('解码后的字符串', decode_str.decode())

  1. base64.standard_b64decode(str):用于对标准Base64编码的字节字符串进行解码。
  2. base64.standard_b64encode(str):用于对字节字符串进行标准Base64编码。
  3. base64.urlsafe_b64encode(str):用于对字节字符串进行URL安全的Base64编码。
  4. base64.urlsafe_b64decode(str):用于对URL安全的Base64编码的字节字符串进行解码。
  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值