天天看點

base64的 b64encode和b64decode

文檔字元串

Decode the Base64 encoded

bytes-like object

or

ASCII string s

.--------------解碼(解的是base64加密的)

Encode

the bytes-like object s

using Base64 and return

a bytes object

.----------編碼(用64個字元表示任意二進制資料)

兩者得到的均是bytes 類型

測試

例子1

c=base64.b64decode('abc')  
# 因為不比對bytes資料的要求而報錯  Error: Incorrect padding
           

例子2

import pickle
d = dict(name='Bob', age=20, score=88)
pickle.dumps(d)  # 字典-->二進制資料
# result
# b'\x80\x03}q\x00(X\x05\x00\x00\x00scoreq\x01KXX\x03\x00\x00\x00ageq\x02K\x14X\x04\x00\x00\x00nameq\x03X\x03\x00\x00\x00Bobq\x04u.'

# 用base64編碼
base64.b64encode(pickle.dumps(d))  ---------------2
# b'gAN9cQAoWAUAAABzY29yZXEBS1hYAwAAAGFnZXECSxRYBAAAAG5hbWVxA1gDAAAAQm9icQR1Lg

# 将上一條代碼的二進制資料變成字元串
base64.b64encode(pickle.dumps(d)).decode()# --->二進制資料變成字元串-------1
# 'gAN9cQAoWAUAAABzY29yZXEBS1hYAwAAAGFnZXECSxRYBAAAAG5hbWVxA1gDAAAAQm9icQR1Lg=='
           

如何還原