Python将内容进行base64编码与解码实现
作者:牛奶咖啡13 时间:2021-02-19 17:47:42
一、需求说明
需要使用Python实现将内容转为base64编码,解码,方便后续的数据操作。
二、base64简介
Base64是一种二进制到文本的编码方式【是一种基于 64 个可打印字符来表示二进制数据的表示方法(由于 2^6=64,所以每 6 个比特为一个单元,对应某个可打印字符。3 个字节有 24 个比特(3*8),对应于 4 个 Base64 单元,即 3 个字节可由 4 个可打印字符来表示)】;
简单的说Base64是一种将 byte数组编码为字符串的方法,而且编码出的字符串只包含ASCII基础字符(是网络传输 8Bit 字节代码的编码方式之一,是一种基于 64 个可打印字符来表示二进制数据的方法)
Base64使用到的64个字符 | ||
序号 | 字符 | 数目 |
1 | A-Z | 26个 |
2 | a-z | 26个 |
3 | 0-9 | 10个 |
4 | + | 1个 |
5 | / | 1个 |
注意:Base64不是加密算法,仅仅是一种编码方式,并且算法也是公开的,故不能依赖它进行加密。
深入浅出base64编码原理
三、base64编码解码
3.1、Python中base64编码解码
Python中base64编码解码的方法
import base64
#将内容转为base64格式内容
def convertToBase64(info):
try:
tmpBytes=info.encode()
tmpBase64=base64.b64encode(tmpBytes)
return tmpBase64
except Exception as e:
print('异常:',e)
#将base64格式内容转为正常信息
def convertTostring(base64Info):
try:
tmpBytes=base64.b64decode(base64Info)
tmpStr=tmpBytes.decode()
return tmpStr
except Exception as e:
print('异常:',e)
3.2、Python使用示例
#Python编码解码使用示例
#将正常信息转为base64格式内容
tmpStr='这是一个测试信息'
base64Info =base64OPC.convertToBase64(tmpStr)
print('\n',tmpStr,'转为base64内容:',base64Info)
#将base64格式内容转为正常信息
info=base64OPC.convertTostring(base64Info)
print('\n',base64Info,'转为正常信息是:',info)
tmpBase64='W215c3FsXQ0KZGVmYXVsdC1jaGFyYWN0ZXItc2V0PXV0ZjgN'
#tmpBase64='w215c3fsxq0kzgvmyxvsdc1jagfyywn0zxitc2v0pxv0zjgn'
tmp=base64OPC.convertTostring(tmpBase64)
print('\n',tmpBase64,'转为正常信息是:',tmp)
执行结果如下:
来源:https://blog.csdn.net/xiaochenXIHUA/article/details/129204245
标签:Python,base64,编码,解码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python3 shelve对象持久存储原理详解
2022-06-30 13:43:39
![](https://img.aspxhome.com/file/2023/2/86542_0s.png)
python删除列表内容
2023-05-23 02:47:49
python numpy 显示图像阵列的实例
2022-09-23 01:24:13
![](https://img.aspxhome.com/file/2023/1/112791_0s.jpg)
Flask框架实现的前端RSA加密与后端Python解密功能详解
2021-07-26 16:28:13
![](https://img.aspxhome.com/file/2023/7/89277_0s.jpg)
php中session_unset与session_destroy的区别分析
2023-07-17 21:34:56
解读ASP.NET 5 & MVC6系列教程(2):初识项目
2023-06-28 02:10:47
![](https://img.aspxhome.com/file/2023/8/75528_0s.png)
python实现决策树分类
2021-04-04 13:35:28
![](https://img.aspxhome.com/file/2023/7/94067_0s.png)
php获取当前时间的毫秒数的方法
2023-06-28 06:43:37
Centos7 Python3下安装scrapy的详细步骤
2021-12-03 10:59:26
![](https://img.aspxhome.com/file/2023/3/93543_0s.png)
Python中IPYTHON入门实例
2021-01-23 04:56:25
python实现Dijkstra算法的最短路径问题
2023-05-18 23:22:15
![](https://img.aspxhome.com/file/2023/4/95784_0s.png)
[译]Javascript风格要素(一)
2008-02-28 12:58:00
Python对象类型及其运算方法(详解)
2023-08-30 09:11:59
![](https://img.aspxhome.com/file/2023/1/84941_0s.png)
Python中的进程操作模块(multiprocess.process)
2022-09-17 23:10:32
详解python时间模块中的datetime模块
2023-09-26 02:41:28
python服务器与android客户端socket通信实例
2022-04-28 02:30:31
从Oracle 表格行列转置说起第1/2页
2009-09-24 12:51:00
Tensorflow与RNN、双向LSTM等的踩坑记录及解决
2021-04-29 21:25:55
![](https://img.aspxhome.com/file/2023/2/81492_0s.png)
详解 PyTorch Lightning模型部署到生产服务中
2021-10-23 01:17:25
![](https://img.aspxhome.com/file/2023/6/90416_0s.jpg)
python接口自动化测试数据和代码分离解析
2022-12-29 06:00:11
![](https://img.aspxhome.com/file/2023/9/97669_0s.png)