Pyton基础-base64加解密

base64加密后是可逆的,所以url中传输参数一般用base64加密

import base64
s='username=lanxia&username2=zdd'
new_s=base64.b64encode(s.encode())# encode 是为了把字符串变为字节,然后再转换
print(s.encode())
print('加密完',new_s)
old_s=base64.b64decode(new_s.decode())
print("解密回来",old_s)

执行结果:

b'username=lanxia&username2=zdd'
加密完 b'dXNlcm5hbWU9bGFueGlhJnVzZXJuYW1lMj16ZGQ='
解密回来 b'username=lanxia&username2=zdd'

def bs64_data_encode(st):
    '''这个函数是用来base64加密的'''
    salt='JMY12345'
    new_str = str(st)+salt
    encode_str = base64.b64encode(new_str.encode()).decode()
    return encode_str

def bs64_data_decode(st):
    '''这个函数是用来base64解密的'''
    salt='JMY12345'
    res = base64.b64decode(st).decode()
    res=res.replace(salt,'')
    return res
原文地址:https://www.cnblogs.com/niuniu2018/p/7867150.html