python中有关encode()和decode()函数的应用,可以参考:
/m0_38080253/article/details/78841280
base64编码和解码
import base64
copyright = 'a123456'
def main():
#转成bytes string
bytesString = (encoding="utf-8")
print("转化后的二进制:",bytesString)
#base64 编码
encodestr = base64.b64encode(bytesString)
print("base64后的二进制:", encodestr)
print("base64后的字符:",())
#解码
decodestr = base64.b64decode(encodestr)
print("base64后的二进制:", decodestr)
print("base64后的字符:",())
if __name__ == '__main__':
main()
最终结果:
转化后的二进制: b'a123456'
base64后的二进制: b'YTEyMzQ1Ng=='
base64后的字符: YTEyMzQ1Ng==
base64后的二进制: b'a123456'
base64后的字符: a123456
import base64
from binascii import a2b_base64, b2a_base64
copyright = 'a123456'
def base():
#转成bytes string
bytesString = (encoding="utf-8")
print("转化后的二进制:",bytesString)
print("\t")
# 二进制------base64后的字符串
n = b2a_base64(bytesString)
print(n, ())
# base64字符串------二进制
m = a2b_base64(n)
print(m, ())
if __name__ == '__main__':
base()
转化后的二进制: b'a123456'
b'YTEyMzQ1Ng==\n' YTEyMzQ1Ng==
b'a123456' a123456
(1)base64编码,需要先把名文串编码为二进制,然后再编码成二进制密文,最终decode()变为密文
(2)解码的路径与编码相反
(3)base64.b64encode(bytesString) & b2a_base64(bytesString) 功能一致
(4)base64.b64decode(encodestr) & a2b_base64(encodestr) 功能一致