import base64 a = "liming我是中国人" bytesString = a.encode(encoding="utf-8") # 转码--字符串转成字节形式的字符串 #b'limingxe6x88x91xe6x98xafxe4xb8xadxe5x9bxbdxe4xbaxba' encodestr = base64.b64encode(bytesString) #加密--转换成base64编码 #参数:字节形式的字符串 #b'bGltaW5n5oiR5piv5Lit5Zu95Lq6' x=encodestr.decode() # 解码 # bGltaW5n5oiR5piv5Lit5Zu95Lq6 decodestr = base64.b64decode(encodestr) #解密 #b'limingxe6x88x91xe6x98xafxe4xb8xadxe5x9bxbdxe4xbaxba' x=decodestr.decode() print(encodestr,decodestr,x)