python-base64编码与解码
生活随笔
收集整理的這篇文章主要介紹了
python-base64编码与解码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
base64編碼原理:
例如:
實例一:
#-*- coding: UTF-8 -*-__author__ = '007' __date__ = '2015/12/23'import base64code = "aGV5LOatpOWkhOWtmOWcqGpvb21sYea8j+a0nu+8jOivt+WPiuaXtuiBlOezuyB4eHh4eHhAMTI2LmNvbSDkv67lpI3mraTmvI/mtJ4="print type(code) cc = base64.decodestring(code) print ccu = u'hey,此處存在joomla漏洞,請及時聯系 xxxxxx@126.com 修復此漏洞' print type(u) d = u.encode('utf8') print type(d)dd = base64.encodestring(d) print dd運行結果:
<type 'str'> hey,此處存在joomla漏洞,請及時聯系 xxxxxx@126.com 修復此漏洞 <type 'unicode'> <type 'str'> aGV5LOatpOWkhOWtmOWcqGpvb21sYea8j+a0nu+8jOivt+WPiuaXtuiBlOezuyB4eHh4eHhAMTI2LmNvbSDkv67lpI3mraTmvI/mtJ4=?
實例二:
#-*- coding: UTF-8 -*-__author__ = '007' __date__ = '2016/2/15'import base64#對字符串進行base64編碼 def str2base64():str_encode = raw_input("請輸入字符串:")en = base64.encodestring(str_encode)print en #對字符串進行base64解碼 def base64tostr():str_decode = raw_input("請輸入base64串:")de = base64.decodestring(str_decode)print de #對url字符串進行base64編碼 def url2base64():url_encode = raw_input("請輸入URL字符串:")en = base64.urlsafe_b64encode(url_encode)print en #對url字符串進行base64解碼 def base64tourl():url_decode = raw_input("請輸入url的base64串:")de = base64.urlsafe_b64decode(url_decode)print de#對文件里面的字符串進行base64編碼 def file_base64_en():f1 = raw_input("請輸入您要讀取的文件:")f2 = raw_input("請輸入您要寫入的文件:")rf = open(f1,'r')lines = rf.readlines()wf = open(f2,'w')for line in lines:word = line.strip()en = base64.encodestring(word)#print enwf.write(word+"的base64編碼結果是:"+en)#wf.write("\n") rf.close()wf.close() #對文件里面的字符串進行base64解碼 def file_base64_de():f1 = raw_input("請輸入您要讀取的文件:")f2 = raw_input("請輸入您要寫入的文件:")rf = open(f1,'r')lines = rf.readlines()wf = open(f2,'w')for line in lines:de = base64.decodestring(line)#print dewf.write(line+"的base64解碼結果是:"+de)wf.write("\n")rf.close()wf.close()def main():print u"a.字符串"print u"b.url字符串"print u"c.讀取文件操作"ch = raw_input("請選擇操作數據類型:")if ch == "a":print u"1.base64編碼"print u"2.base64解碼"choice = raw_input("請選擇編碼或解碼:")if choice == "1":str2base64()elif choice == "2":base64tostr()else:print u"您的選擇不是合理的編碼或解碼!"elif ch == "b":print u"1.base64編碼"print u"2.base64解碼"choice = raw_input("請選擇編碼或解碼:")if choice == "1":url2base64()elif choice == "2":base64tourl()else:print u"您的選擇不是合理的編碼或解碼!"elif ch == "c":print u"1.base64編碼"print u"2.base64解碼"choice = raw_input("請選擇編碼或解碼:")if choice == "1":file_base64_en()elif choice == "2":file_base64_de()else:print u"您的選擇不是合理的編碼或解碼!"else:print u"未找到您所需要編碼或解碼的數據類型!"if __name__ == "__main__":main()?
?
?
轉載于:https://www.cnblogs.com/andr01la/p/5191004.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python-base64编码与解码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图解Android事件传递之ViewGr
- 下一篇: Python 列表count()函数元素