encrypt password in python

How to Encrypt Password in Python

Python allows you to accept user input as password. But, by default, it displays whatever you to type on the screen. Often you may need to hide the typed password as asterisk sign. There are many python libraries that allow you to hide passwords. In this article, we will learn how to hide user password using maskpass() library and encrypt password in python using base64() module.

How to Encrypt Password in Python

maskpass is a useful python library that allows you to mask user input when your application interacts with users. This can be used to hide passwords of users during input time.

1. Install maskpass

By default, maskpass is not present in python. You need to install it with the following command.

$ pip install maskpass

maskpass() module offers 2 functions:

  • askpass()
  • advpass()

2. Askpass()

askpass() – uses standard library to accept non-blocking input and also return entered password. Here is the sample code to accept user password and return it in string format.

import maskpass
pwd = maskpass.askpass()

The entered password will be stored in pwd variable as a string. The default prompt when calling askpass() function is ‘Enter password:’ and the default masking character is asterisk (*). You can customize it using optional arguments prompt and mask which allow you to specify the prompt text and masking character to be displayed. For example, if you want to display hash # instead of asterisk, enter mask=’#’ in askpass() function.

import maskpass
pwd = maskpass.askpass(mask='#')

In this case, when user enters password, it will be displayed as a series of hash (#) and not asterisk (*).

If you don’t want to echo even the masking character, use mask=”.

import maskpass
pwd = maskpass.askpass(mask='')

When you run the above code, the password you enter will not even be displayed using the masking character.

Here is an example, where we customize both prompt text and masking character.

import maskpass
pwd = maskpass.askpass(prompt='password:',mask='#')

In the above example, when you run the code, you will see ‘password:’ text instead of default ‘Enter password:’ text. When users input password it will be displayed as a sequence of hash marks.

3. Advpass()

Advpass uses pynput to accept text and return the password. It provides more options than askpass() function. Here is a sample code to use Advpass().

import maskpass
pwd = maskpass.advpass()

Advpass() also returns the password in string format. It accept 4 optional arguments.

  • prompt – Default value for prompt is ‘Enter password:’
  • mask – Default value for mask is asterisk(*).
  • Ide – check whether it’s running on IDE or terminal. ide expects a Boolean value i.e., true or false, the default value of ide is False. There is no need to change the value of ide generally.
  • suppress – suppress also accepts a Boolean value i.e., true or false, is used only in Spyder IDE. Setting this as True prevents the input from being passed to the rest of the system. This prevents the Spyder console from jumping down when space bar is pressed. The default value for suppress is True.

Advpass() also offers the ability to reveal the password when you typing it, by pressing left Ctrl key. Press left Ctrl key again to mask the password.

4. Encrypt password in Python

You can use base64() function to encrypt password in python. It allows you to encrypt as well as decrypt passwords using b64encode() and b64decode() functions respectively. But it accepts only byte-like object. So we use use encode() function to convert a string into byte-object.


Once you have encoded the string into byte object, you can use b64encode() function to encrypt it.


In order to decode the encrypted string, use the b64decode() function.


Here is a simple example to accept user password using askpass(), convert the input string into UTF-8 byte string, encrypt it using b64encode() and then decrypt it using b64decode().

import base64
import maskpass

# Accept user password input
pwd = maskpass.askpass()
# Encoding the string
encode = base64.b64encode(pwd.encode("utf-8"))
print("str-byte : ", encode)
# Decoding the string
decode = base64.b64decode(encode).decode("utf-8")
print("byte-str : ", decode)

In this article, we have learnt how to accept user password input, how to hide the user input, encrypt accepted password and then decrypt user password.

Also read:

How to Create Thumbnail from Python
How to Open Multiple Files in Vim
How to Split Vim Screen Vertically & Horizontally
Fix Unable to Mount NTFS Error in Linux
How to Convert PPK to PEM Using PuTTY in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *