base64

1、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?python

 

首先:ui

在python2中,字符串str默認採用的是ASCII編碼。編碼

所以在base64加密的時候,直接使用ascii的字符串str,加密便可。返回字符串str。加密

 

其次:url

在python2中,使用unicode類型做爲編碼的基礎類型。即spa

     decode              encodecode

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

所以,若是要使用unicode,進行base64加密。utf-8

能夠有兩種方法,是正確的:ci

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=中國'
相關文章
相關標籤/搜索