python中base64库用法详解

本文详细介绍了Base64编码原理,包括其在Python中的使用,如base64模块的常见方法,如encode、decode、b64encode、b64decode等。Base64常用于在URL、Cookie和XML中传输二进制数据。文中给出了多个Python代码示例,展示了如何对字符串、URL和文件进行Base64编码和解码,以及对图片数据的处理。此外,还探讨了Base64在嵌套加密解密中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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转换过程

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)

运行结果:

 示例代码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)

运行结果:

示例代码3:

import base64


def base64_encode_func():
    mgs = 'txt'
    res = base64.b64encode(mgs.encode('utf-8'))
    return res


def base64_decode_func(content):
    res = base64.b64decode(content)
    return res


if __name__ == '__main__':
    res = base64_encode_func()
    print(res)  # b'dHh0'

    res2 = base64_decode_func(res)
    print(res2)  # b'txt'

运行结果:

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)

运行结果:

 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)

运行结果:

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()

运行结果:

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嵌套解决方案,读者若有更好的解决方法欢迎留言评论!

要在Python中使用base64,你无需单独安装它。base64Python的标准之一,已经内置在Python中。你可以直接在代码中导入并使用它。以下是两个示例代码,展示了如何使用base64进行编码和解码操作: 示例代码1: ```python import base64 s = '人生苦短,我学python!' # 待加密的字符串 b = base64.b64encode(s.encode('utf-8')) # 进行base64编码 print(b) c = base64.b64decode(b).decode('utf-8') # 进行base64解码 print(c) ``` 示例代码2: ```python from base64 import b64encode, b64decode s = 'I love python!' # 待加密的字符串 s_encode = s.encode(encoding='utf-8') # 将字符串编码为字节流 s_encode_base64 = b64encode(s_encode) # 进行base64编码 print(s_encode_base64) s_decode_base64 = b64decode(s_encode_base64) # 进行base64解码 s_decode = s_decode_base64.decode(encoding='utf-8') # 将字节流解码为字符串 print(s_decode) ``` 你可以根据自己的需求选择其中的一种方式来使用base64。如果你想了解更多关于base64的详细信息,可以参考\[3\]中的链接。 #### 引用[.reference_title] - *1* *2* [pythonbase64用法详解](https://blog.csdn.net/weixin_44799217/article/details/125949538)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [[python] base64的基本使用](https://blog.csdn.net/xiaokai1999/article/details/128436690)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值