DEV Community

loading...

Securing your passwords with python

prateekbud profile image Prateek budhiraja ・1 min read

We will be using the 'cryptography' package to encrypt and decrypt our password file.
Cryptography gives us symmetric encoding method, with which we can encode and decode using a single key.
Let's start,

Encrypting

First, install cryptography using pip or conda tool.
Then import it into your program using
image
After that get your data to be encrypted into the program
image
After that create a key and save it to a file to use at the time of decryption.
And create an object to encrypt the data.
image
Then use that object to encrypt the data, and save the data to a new file.
image
Now you would have a new file with the encrypted text.

If you are having a problem with reading or writing bytes to a file, use the decode function to convert bytes to string and encode to convert string to bytes.

Dectypting

Decrypting is fairly simple as well, we just need to import the key and the encrypted text into our program and just apply the decrypt function onto it.
image

See the code at Github

Discussion (0)

pic
Editor guide