python中base64库用法详解

1、什么是Base64

        Base64是一种用64个字符来表示任意二进制数据的方法。

        Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于2^6=64,所以每6个比特为一个单元,对应某个可打印字符。

        3个字节有24个比特,对应于4个Base64单元,即3个字节可由4个可打印字符来表示。在Base64中的可打印字符包括字母A-Z、a-z、数字0-9,这样共有62个字符,此外两个可打印符号在不同的系统中而不同。

2、python中的base64模块

Base64模块真正用得上的方法只有8个,分别是:

encode, decode为一组, 专门用来编码和解码文件的, 也可以对StringIO里的数据做编解码;

encodestring, decodestring为一组,专门用来编码和解码字符串

b64encode, b64decode为一组, 用来编码和解码字符串,并且有一个替换符号字符的功能

因为Base64编码后的字符除了英文字母和数字外还有三个字符’ + / =‘,其中’=‘只是为了补全编码后的字符数为4的整数,而’+‘和’/‘在一些情况下需要被替换的,b64encode和b64decode正是提供了这样的功能。至于什么情况下’+‘和’/'需要被替换,最常见的就是对url进行Base64编码的时候。

urlsafe_b64decode, urlsafe_b64encode为一组,这个就是用来专门对url进行Base64编解码的,实际上也是调用的前一组函数。

base64.b64encode()将bytes类型数据进行base64编码,返回编码后的bytes类型

base64.b64deocde()将base64编码的bytes类型进行解码,返回解码后的bytes类型

decode的作用是将其他编码的字符串转换成unicode编码

encode的作用是将unicode编码转换成其他编码的字符串

3、Base64使用场景

        Base64是一种任意二进制到文本字符串的编码方法,常用于在URL、Cookie、网页中传输少量二进制数据,包括MIME的电子邮件及XML的一些复杂数据。

在邮件中的用途:

        在MIME格式的电子邮件中,base64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本。使用时,在传输编码方式中指定base64。使用的字符包括大小写字母各26个,加上10个数字,和加号“+”,斜杠“/”,一共64个字符,等号“=”用来作为后缀用途。

在URL中的用途:

        标准的Base64并不适合直接放在URL里传输,因为URL编码器会把标准Base64中的“/”和“+”字符变为形如“%XX”的形式,而这些“%”号在存入数据库时还需要再进行转换,因为ANSI SQL中已将“%”号用作通配符。

        为解决此问题,可采用一种用于URL的改进Base64编码,它不在末尾填充'='号,并将标准Base64中的“+”和“/”分别改成了“*”和“-”,这样就免去了在URL编解码和数据库存储时所要作的转换,避免了编码信息长度在此过程中的增加,并统一了数据库、表单等处对象标识符的格式。

        另有一种用于正则表达式的改进Base64变种,它将“+”和“/”改成了“!”和“-”,因为“+”,“*”在正则表达式中都可能具有特殊含义。

4、base64转换过程

python中base64库用法详解

5、代码实现

5.1 简单使用

 示例代码1:

import base64

s = '人生苦短,我学python!'
# 加密
b = base64.b64encode(s.encode('utf-8'))
print(b)

# 解密
c = base64.b64decode(b).decode('utf-8')
print(c)

运行结果:

python中base64库用法详解

 示例代码2:

from base64 import b64encode, b64decode

s = 'I love python!'
print(s)

s_encode = s.encode(encoding='utf-8')
print(s_encode)
s_encode_base64 = b64encode(s_encode)
print(s_encode_base64)
s_decode_base64 = b64decode(s_encode_base64)
print(s_decode_base64)
s_decode = s_decode_base64.decode(encoding='utf-8')
print(s_decode)

运行结果:

python中base64库用法详解

5.2 url使用base64

示例代码:

import base64

url = 'http://www.baidu.com/info'
# 加密
b = base64.b64encode(url.encode('utf-8'))
print(b)

# 解密
c = base64.b64decode(b).decode('utf-8')
print(c)

运行结果:

python中base64库用法详解

 5.3 读写文件

示例代码1:

from base64 import b64encode, b64decode

with open(r'./text.txt', 'rb') as f:  # 此处不能使用encoding='utf-8', 否则报错
    base64_data = b64encode(f.read())  # b64encode是编码
    print(base64_data)  # 输出生成的base64码,Bytes类型, b'xxxxx'类型的字符串

new_data = b64decode(base64_data)
print(new_data)

运行结果:

python中base64库用法详解

5.4 base64对图片数据进行加密

示例代码:

import base64

# 读取一张图片,将图片数据转换为base64格式
read_gif = open('./run.gif', 'rb')
read_data = read_gif.read()
print(read_data)
read_gif.close()

base_gif = base64.b64encode(read_data)
print(base_gif)

# data = base_gif.decode('ascii')
data = base_gif.decode()
print(data)

new_data = base64.b64decode(base_gif)
print(new_data)

# 将读取的数据重新写入文件,保存为图片格式
new_run = open('new_run.gif', 'wb')
new_run.write(new_data)
new_run.close()

运行结果:

python中base64库用法详解

python中base64库用法详解

python中base64库用法详解

python中base64库用法详解

python中base64库用法详解

5.5 base64嵌套加解密

 示例代码:

import base64
import re

html = """
<html>
<head>
    <title>My Page Title({0})</title>
</head>
<body>
<h1>This is a Heading({1})</h1>
<p>This is a paragraph({2}).</p>
</body>
</html>
"""
html_list = []
# 对字符串嵌套加密
print('开始加密:')
for i in range(2):
    new_html = html.format(i, i, i)
    title = re.findall(r'<title>(.*?)</title>', new_html)[0]
    h1 = re.findall(r'<h1>(.*?)</h1>', new_html)[0]
    p = re.findall(r'<p>(.*?)</p>', new_html)[0]
    new_html = new_html.replace(title, '{0}').replace(h1, '{1}').replace(p, '{2}')
    new_html = new_html.format(base64.b64encode(title.encode('utf-8')).decode('utf-8'),
                               base64.b64encode(h1.encode('utf-8')).decode('utf-8'),
                               base64.b64encode(p.encode('utf-8')).decode('utf-8'))
    html_list.append(base64.b64encode(new_html.encode('utf-8')).decode('utf-8'))
    print(new_html)

print('开始解密:')
# 对字符串嵌套解密
for data in html_list:
    html_data = base64.b64decode(data).decode('utf-8')
    title = re.findall(r'<title>(.*?)</title>', html_data)[0]
    h1 = re.findall(r'<h1>(.*?)</h1>', html_data)[0]
    p = re.findall(r'<p>(.*?)</p>', html_data)[0]
    html_data = html_data.replace(title, '{0}').replace(h1, '{1}').replace(p, '{2}')
    html_data = html_data.format(base64.b64decode(title).decode('utf-8'),
                               base64.b64decode(h1).decode('utf-8'),
                               base64.b64decode(p).decode('utf-8'))
    print(html_data)

运行结果;

开始加密:

<html>
<head>
    <title>TXkgUGFnZSBUaXRsZSgwKQ==</title>
</head>
<body>
<h1>VGhpcyBpcyBhIEhlYWRpbmcoMCk=</h1>
<p>VGhpcyBpcyBhIHBhcmFncmFwaCgwKS4=</p>
</body>
</html>


<html>
<head>
    <title>TXkgUGFnZSBUaXRsZSgxKQ==</title>
</head>
<body>
<h1>VGhpcyBpcyBhIEhlYWRpbmcoMSk=</h1>
<p>VGhpcyBpcyBhIHBhcmFncmFwaCgxKS4=</p>
</body>
</html>

开始解密:

<html>
<head>
    <title>My Page Title(0)</title>
</head>
<body>
<h1>This is a Heading(0)</h1>
<p>This is a paragraph(0).</p>
</body>
</html>


<html>
<head>
    <title>My Page Title(1)</title>
</head>
<body>
<h1>This is a Heading(1)</h1>
<p>This is a paragraph(1).</p>
</body>
</html>


Process finished with exit code 0

暂未想到更好的base64嵌套解决方案,读者若有更好的解决方法欢迎留言评论!

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年3月8日 下午10:23
下一篇 2023年3月8日 下午10:26

相关推荐