base64

一、base64加密

1.python2加密的时候使用str,加密后的数据也是str。

>>> import base64
>>> url = "https://www.baidu.com?a=23&b=中国"
>>> b=base64.b64encode(url.decode('utf8').encode('utf-8'))
>>> type(b)
<type 'str'>
>>> import sys
>>> sys.getdefaultencoding()
'ascii'

 

>>> b=base64.b64encode(url.encode('utf-8'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 29: ordinal not in range(128)

 

对加密的str,编码后,再使用base64加密,报错。why?

 

首先:

在python2中,字符串str默认采用的是ASCII编码。

因此在base64加密的时候,直接使用ascii的字符串str,加密即可。返回字符串str。

 

其次:

在python2中,使用unicode类型作为编码的基础类型。即

     decode              encode

str ---------> unicode --------->str

因此,如果要使用unicode,进行base64加密。

可以有两种方法,是正确的:

1).按照上面的步骤,先decode,再encode,这个时候字符串是默认的ascii。

>>> b=base64.b64encode(url.decode('utf8').encode('utf-8'))
>>> type(b)
<type 'str'>

 

2).将系统编码设置为utf8,直接encode()

复制代码
>>> sys.getdefaultencoding()
'ascii'
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('utf8')
>>> b=base64.b64encode(url.encode('utf-8'))
>>> type(b)
<type 'str'>
>>> sys.getdefaultencoding()
'utf8'
复制代码

 

 2.python3的字符串在base64加密前,必须先编码为二进制。

>>> import base64
>>> url = "https://www.baidu.com?a=23&b=中国"
>>> b=base64.b64encode(url.encode('utf-8'))
>>> type(b)
<class 'bytes'>

 

否则,抛出异常

>>> 
>>> b=base64.b64encode(url)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'

 

我们可以查看一下python3默认使用的编码

>>> import sys
>>> sys.getdefaultencoding()
'utf-8'

 

总结:

  • 在python3,base64加密前,必须先编码为bytes。
  • 在python2,可能会混淆。因此:
    • 不管系统编码为默认的ascii,还是已经改为utf8。先decode,再encode始终不会错。

      b=base64.b64encode(url.decode('utf-8').encode('utf-8'))

    • 如果可以全局确认,python2的系统编码始终是默认的ascii。就直接base64加密即可。
    • 如果可以全局确认,python2的系统编码始终是默认的utf8。base64加密前,必须先encode。

 

二.解密

1.在python2中,base64解密后,得到的是ascii的字符串。无论系统编码是ascii,还是unicode。

>>> b1=base64.b64decode(b)
>>> type(b1)
<type 'str'>
>>> b1
'https://www.baidu.com?a=23&b=\xe4\xb8\xad\xe5\x9b\xbd'
>>> sys.getdefaultencoding()
'utf8'
>>> b1=base64.b64decode(b)
>>> type(b1)
<type 'str'>
>>> b1
'https://www.baidu.com?a=23&b=\xe4\xb8\xad\xe5\x9b\xbd'
>>> sys.getdefaultencoding()
'ascii'

 

如果想要得到uicode,需要解密后,再decode。无论系统编码是ascii,还是unicode。 

>>> b2=base64.b64decode(b).decode('utf8')
>>> b2
u'https://www.baidu.com?a=23&b=\u4e2d\u56fd'
>>> type(b2)
<type 'unicode'>
>>> sys.getdefaultencoding()
'utf8'

 

 

 

 

>>> b2=base64.b64decode(b).decode('utf8')
>>> type(b2)
<type 'unicode'>
>>> b2
u'https://www.baidu.com?a=23&b=\u4e2d\u56fd'
>>> sys.getdefaultencoding()
'ascii'

 

2.在python3中,base64解密后,得到的是bytes。如果想要得到unicode字符串str,解密后必须decode()

复制代码
>>> b1=base64.b64decode(b)
>>> type(b1)
<class 'bytes'>
>>> b2=base64.b64decode(b).decode()
>>> type(b2)
<class 'str'>
>>> b1
b'https://www.baidu.com?a=23&b=\xe4\xb8\xad\xe5\x9b\xbd'
>>> b2
'https://www.baidu.com?a=23&b=中国'
复制代码

 

posted on   myworldworld  阅读(200)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示