Encoding with Base64

Base64 is an encoding method that is used frequently to this day. It is very easily encoded and decoded, which makes it both extremely useful and also dangerous. Base64 is not used as commonly anymore to encode sensitive data, but there was a time where it was.

Getting ready

Thankfully for the Base64 encoding, we do not require any external modules.

How to do it…

To generate the Base64 encoded string, we can use default Python features to help us achieve it:

#!/usr/bin/python
msg = raw_input('Please enter the string to encode: ')
print "Your B64 encoded string is: " + msg.encode('base64')

How it works…

Encoding a string in Base64 within Python is very simple and can be done in a two-line script. To begin we need to have the string fed to us as a user input so we have something to work with:

msg = raw_input('Please enter the string to encode: ')

Once we have the string, we can do the encoding as we print out the result, using msg.encode('base64'):

print "Your B64 encoded string is: " + msg.encode('base64')

Here is an example of the script in action:

Please enter the string to encode: This is an example
Your B64 encoded string is: VghpcyBpcyBhbiBleGFtcGxl
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.35.247