PYTHON-BASE64 encoding and decoding

Source: Internet
Author: User
Tags base64



Base64 Coding principle:






For example:









Example one:


#-*- coding: UTF-8 -*-

__author__ = ‘007’
__date__ = ‘2015/12/23’


Import base64


Code = "aGV5LOatpOWkhOWtmOWcqGpvb21sYea8j+a0nu+8jOivt+WPiuaXtuiBlOezuyB4eHh4eHhAMTI2LmNvbSDkv67lpI3mraTmvI/mtJ4="

Print type(code)
Cc = base64.decodestring(code)
Print cc

u = u‘hey, there is a joomla vulnerability here, please contact xxxxxx@126.com to fix this vulnerability ‘
Print type(u)
d = u.encode(‘utf8‘)
Print type(d)

Dd = base64.encodestring(d)
Print dd 


Operation Result:


<type ‘str‘>
Hey, there is a joomla vulnerability here, please contact xxxxxx@126.com to fix this vulnerability.
<type ‘unicode‘>
<type ‘str‘>
aGV5LOatpOWkhOWtmOWcqGpvb21sYea8j+a0nu+8jOivt+WPiuaXtuiBlOezuyB4eHh4eHhAMTI2LmNvbSDkv67lpI3mraTmvI/mtJ4= 





Example two:


#-*- coding: UTF-8 -*-

__author__ = ‘007’
__date__ = ‘2016/2/15’

Import base64

#base64 encoding the string
Def str2base64():
    Str_encode = raw_input("Please enter a string:")
    En = base64.encodestring(str_encode)
    Print en
#base64 decoding of strings
Def base64tostr():
    Str_decode = raw_input("Please enter base64 string:")
    De = base64.decodestring(str_decode)
    Print de
#base64 encoding the url string
Def url2base64():
    Url_encode = raw_input("Please enter the URL string:")
    En = base64.urlsafe_b64encode(url_encode)
    Print en
#base64 decoding the url string
Def base64tourl():
    Url_decode = raw_input("Please enter the base64 string of the url:")
    De = base64.urlsafe_b64decode(url_decode)
    Print de

#base64 encoding the string in the file
Def file_base64_en():
    F1 = raw_input("Please enter the file you want to read:")
    F2 = raw_input("Please enter the file you want to write:")
    Rf = open(f1,‘r‘)
    Lines = rf.readlines()
    Wf = open(f2,‘w‘)
    For line in lines:
        Word = line.strip()
        En = base64.encodestring(word)
        #print en
        The base64 encoding result of wf.write(word+" is: "+en)
        #wf.write("\n")
    Rf.close()
    Wf.close()
#base64 decoding the string inside the file
Def file_base64_de():
    F1 = raw_input("Please enter the file you want to read:")
    F2 = raw_input("Please enter the file you want to write:")
    Rf = open(f1,‘r‘)
    Lines = rf.readlines()
    Wf = open(f2,‘w‘)
    For line in lines:
        De = base64.decodestring(line)
        #print de
        The base64 decoding result of wf.write(line+" is: "+de)
        Wf.write("\n")
    Rf.close()
    Wf.close()

Def main():
    Print u"a.string"
    Print u"b.url string"
    Print u"c.Read file operation"
    Ch = raw_input("Please select the operation data type:")

    If ch == "a":
        Print u"1.base64 encoding"
        Print u"2.base64 decoding"

        Choice = raw_input("Please select encoding or decoding:")
        If choice == "1":
            Str2base64()
        Elif choice == "2":
            Base64tostr()
        Else:
            Print u" Your choice is not a reasonable encoding or decoding!"
    Elif ch == "b":
        Print u"1.base64 encoding"
        Print u"2.base64 decoding"

        Choice = raw_input("Please select encoding or decoding:")
        If choice == "1":
            Url2base64()
        Elif choice == "2":
            Base64tourl()
        Else:
            Print u" Your choice is not a reasonable encoding or decoding!"
    Elif ch == "c":
        Print u"1.base64 encoding"
        Print u"2.base64 decoding"

        Choice = raw_input("Please select encoding or decoding:")
        If choice == "1":
            File_base64_en()
        Elif choice == "2":
            File_base64_de()
        Else:
            Print u" Your choice is not a reasonable encoding or decoding!"
    Else:
        Print u" did not find the type of data you need to encode or decode!"


If __name__ == "__main__":
    Main() 










PYTHON-BASE64 encoding and decoding


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.