How to Encrypt Password for Privacy Protection in Python

How to Encrypt Password for Privacy Protection in Python

Published at - Nov 15, 2021

Privacy? In today’s world, this is a crucial term. The number of online platforms and apps, as well as their popularity, has exploded.

Many developers are using this to create new projects and platforms. Because all of these require an authentication method, effective password security is a must. After all, you can’t just save the user’s password in its raw form in the database. Before storing the password in the database, it must be encrypted in some way.

Does this appear to be difficult? Don’t be concerned. To make this procedure easier, I’ll show you how to encrypt a password using Python.

To begin, let’s look at the project setup:

  1. Install Python and the pip package manager on your system.

  2. Make a directory for your project.

  3. Inside the directory, create a file called “”

  4. Open terminal at the directory and run- pip install bcrypt

  5. Import bcrypt in the “” file. — Bcrypt is the python package that will help us achieve encryption. You can read more about bcrypt at Bcrypt

So, now that the project has been set up, let’s get started encrypting a password.

Take a string that you’d want to use as a password. For example, “youareSmart123” as a password.

Step 1:

The next step is to create a string named “Salt.” Salt is a cryptographically strong fixed-length random number that is added to the input of hash functions to produce unique hashes for each piece of data. Even if users use common passwords, salt is used to make a password hash result unique. Using salt as an example:


Step 2:

To produce salt, we’ll use the “bcrypt” package. To do so, use the code below:

salt = bcrypt.gensalt()

Step 3:

The password must now be encrypted as the last step. Encryption:

passwordhashed=bcrypt.hashpw(password, salt)

Step 4:

That is all there is to it. We’ll obtain the encrypted output if we print “password hashed.”

Final Words

So that was the end of this tutorial. I hope you learned something from reading this. Try out the strategy described above, leave a comment if you have any queries, and provide feedback to the blog.

Thank you for your time, don’t forget to hit follow button to read more posts in the future.

More content at

About author

Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.

Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile