网站首页 > 技术文章 正文
base64 是经常使用的一种加密方式,在 Python 中有专门的库支持。
本文主要介绍在 Python2 和 Python3 中的使用区别:
在 Python2 环境:
Python 2.7.16 (default, Mar 25 2021, 03:11:28)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import base64
>>> s = 'AlwaysBeta'
>>> a = base64.b64encode(s)
>>> print a
QWx3YXlzQmV0YQ==
>>>
>>> base64.b64decode(a)
'AlwaysBeta'
在 Python3 环境:
Python3 中有一些区别,因为 Python3 中字符都是 unicode 编码,而 b64encode函数的参数为 byte 类型,所以必须先转码。
Python 3.8.5 (default, Jul 21 2020, 10:42:08)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import base64
>>> a = base64.b64encode('AlwaysBeta'.encode('utf-8'))
>>> a
b'QWx3YXlzQmV0YQ=='
>>> str(a, 'utf-8')
'QWx3YXlzQmV0YQ=='
>>>
>>> base64.b64decode(a)
b'AlwaysBeta'
>>> str(base64.b64decode(a), 'utf-8')
'AlwaysBeta'
以上就是本文的全部内容,如果觉得有用的话欢迎点赞和转发,多谢。
猜你喜欢
- 2025-08-01 「Python爬虫」:破解网站字体加密和反反爬虫
- 2025-08-01 3 行代码扒光 PDF 所有图片?Python 办公实战秘籍来了
- 2025-08-01 创意Python代码
- 2025-08-01 为你的python程序上锁:软件序列号生成器
- 2025-08-01 带了一个不熟练Python的女生,好崩溃
- 2025-08-01 Python的RSA操作(私钥与公钥)
- 2025-08-01 Python 3 加密简介
- 2025-08-01 解密Python时间测量迷雾:高精度计时器time.perf_counter的妙用
- 2025-08-01 如何用逻辑运算符写出更优雅的代码?深度剖析底层逻辑
- 2025-08-01 Python中PyPDF2库全解析:轻松玩转PDF文件处理
- 08-05python决策树用于分类和回归问题实际应用案例
- 08-05用Python实现机器学习算法之k-决策树算法并做注释说明
- 08-05Python机器学习之决策树分类详解,保姆级教学!
- 08-05用Python进行机器学习(5)-决策树
- 08-05决策树算法原理与Python实现
- 08-05python学习笔记 1.常见的数据类型
- 08-05从进阶语法到实战应用:Python中级修炼指南
- 08-05Python 面试问题:运算符
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)