加密解密字符串:
字符串必须先要组成bytes string
import base64 def solve(str): #转成bytes string bytesString = str.encode(encoding="utf-8") print(bytesString) #base64 编码 encodestr = base64.b64encode(bytesString) print(encodestr) print(encodestr.decode()) #解码 decodestr = base64.b64decode(encodestr) print(decodestr.decode()) if __name__ == '__main__': solve("ABC")
加密解密图片:
import base64 with open("nfsq.jpg","rb") as f: # b64encode是编码,b64decode是解码 base64_data = str(base64.b64encode(f.read()), encoding='utf-8') # base64.b64decode(base64data) print(base64_data)
html显示:
<img src="data:image/jpg;base64,这里是base64的编码"/>
打算写image2base64的程序,以交互式方式运行正确,写成脚本却报错“AttributeError: module 'base64' has no attribute 'b64decode'”。
解决方法:我的程序名"base64.py"与包名冲突了,因此改名即可。
参考链接:
1. https://blog.csdn.net/XiangLanLee/article/details/84136519
2. https://www.cnblogs.com/lanzhi/p/6468386.html
3.