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...