base64()
The base64 encode and decode function both require a byte-like object. To convert a string into bytes, we must encode a string using Python’s built-in encode function. Mostly UTF-8 encoding is used, you can also use ‘ASCII’ to encode but I recommend using UTF-8 encoding.
Python3
# encoding the string string = "greeksforgreek" # encoding string with utf-8 b = string.encode( "UTF-8" ) print (b) |
Output:
F:\files>python strencode.py b'greeksforgreek'
here b prefix denotes that the value is a byte object.
Encoding the string using base64() module:
To encode the string i.e. to convert the string into byte-code, use the following method:
base64.b64encode(‘string’.encode(“utf-8”))
Decoding the byte-code using base64() module:
To decode the byte-code i.e. to convert the byte-code again into a string, use the following method:
base64.b64decode(‘byte-code’).decode(“utf-8”)
Example:
Python3
# importing base64 modules for # encoding & decoding string import base64 string = "GreeksforGreeks" # Encoding the string encode = base64.b64encode(string.encode( "utf-8" )) print ( "str-byte : " , encode) # Decoding the string decode = base64.b64decode(encode).decode( "utf-8" ) print ( "byte-str : " , decode) |
Output:
F:\files>python base64.py str-byte : b'R3JlZWtzZm9yR3JlZWtz' byte-str : GreeksforGreeks
In the above example, “GreeksforGreeks” the string is firstly encoded using base64 module i.e. string is converted into byte-code and then with help of base64 module again the byte-code is decoded into its original string i.e. “GreeksforGreeks”.
Hide the user’s password during the input time
Python3
# Hiding the inputted password with maskpass() # and encrypting it with use of base64() import maskpass # to hide the password import base64 # to encode and decode the password # dictionary with username # as key & password as value dict = { 'Rahul' : b 'cmFodWw=' , 'Sandeep' : b 'U2FuZGVlcA==' } # function to create password def createpwd(): print ( "\n========Create Account=========" ) name = input ( "Username : " ) # masking password with prompt msg 'Password :' pwd = maskpass.askpass( "Password : " ) # encoding the entered password encpwd = base64.b64encode(pwd.encode( "utf-8" )) # appending username and password in dict dict [name] = encpwd # print(dict) # function for sign-in def sign_in(): print ( "\n\n=========Login Page===========" ) name = input ( "Username : " ) # masking password with prompt msg 'Password :' pwd = maskpass.askpass( "Password : " ) # encoding the entered password encpwd = base64.b64encode(pwd.encode( "utf-8" )) # fetching password with # username as key in dict password = dict [name] if (encpwd = = password): print ( "Successfully logged in." ) else : print ( "Login Failed" ) # calling function createpwd() sign_in() |
Output:
F:\files>python "userLogin.py" ========Create Account========= Username : Rahulraj Password : ***** =========Login Page=========== Username : Rahulraj Password : ***** Successfully logged in.
Hiding and encrypting passwords in Python?
There are various Python modules that are used to hide the user’s inputted password, among them one is maskpass() module. In Python with the help of maskpass() module and base64() module we can hide the password of users with asterisk(*) during input time and then with the help of base64() module it can be encrypted.
Contact Us