How to Secure Files using Python?

| | , ,

Home » How to Secure Files using Python?
Spread the love
  •  
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  

In this article, we will learn how to encrypt and decrypt files using Python with the help of a cryptography library. In other words, you can say that in this tutorial you’ll learn how to secure files using Python? We are living in a modern world where data/information transfer is very easy and easy to access. But, secure data transfer is also important, that’s why security of the data stays one of the biggest concerns.

Encrypt: encryption is a process of transforming plain text into something meaningless that will look random(ciphertext).
Decrypt: decryption is a process of transforming encrypted files back to normal files which we can understand(plaintext).

We are going to see how we can encrypt and decrypt the files using Python and in Python, we have a library that makes the process of encryption and decryption very easy. In this tutorial, we will follow the symmetric type of encryption (symmetric encryption: when someone uses symmetric encryption to encrypt the file or message only that person is able to read the file by decrypting it using the same key they used to encrypt the file. It means the key that is used to encrypt the file or message, only that key can decrypt the file back. If you don’t have the key or lost the key you can’t access it).

How to Secure Files using Python and Cryptography?

  • Installing the python library of cryptography.
  • Creating a key for cryptography.
  • Loading the key.
  • Encryption of file.
  • The decryption of the file.

Step -1: Installing the Cryptography library for Python:

If you don’t have the cryptography library we have to install it open a command prompt or terminal and paste the command given below and press enter:

pip3 install cryptography

Step -2: Generating a Key for Cryptography

Now open your favorite IDE and create a new Python file. We are using the symmetric equation for cryptography here, for that we need to import fernet, it is an authenticated cryptography that does not allow you to manipulate or read the message without using the “key” if you don’t have it you can’t read it.

#import the- required module
from cryptography.fernet import Fernet

Now let’s generate the key and save it in the same location/folder where our data file is located:

#generate the key
key = Fernet.generate_key()
#string the key into a file
with open('unlock.key', 'wb') as unlock:
     unlock.write(key)

After running this code you can check the location where your Python code is located, you should see the new file name unlock.key. If you want you can open it in any text editor (Microsoft visual studio code). You can see in the file that it contains our key which should be a line of some random characters.

Now that we have the key, let’s load the key into our environment to encrypt/decrypt the file. Then the step is very simple, we just need to open the file “unlock.key” and store it in local system memory:

#open the key
with open('unlock.key', 'rb') as unlock:
     key = unlock.read()
print(key)

For verification, we can see the following output, and we can also see that the encryption key is stored locally in the system memory.

N61xnryWG23MHuYm2J4KiMbfmBEtb5 RUcXubcipU5is=

Step -3: Encrypting the File

Now we have the file for the encryption and the encryption key we will now discuss how to write code and then code it:

Let’s discuss what we need to do for encrypting the file. First of all, we need to initialize the Fernet and store the file into the fernet variable. Second, we have to read the original file (sample.txt). Next, we have to encrypt the data using the fernet and store it as encrypted into an object. Then finally we can write it into a new file called “enc_sample.txt”

Let’s see the code for this process:

#use the generated key
f = Fernet(key)
#open the original file to encrypt
with open('sample.txt', 'rb') as original_file:
     original = original_file.read()
#encrypt the file
encrypted = f.encrypt(original)
#you can write the encrypted data  file into a enc_sample.txt
with open ('enc_sample.txt', 'wb') as encrypted_file:
     encrypted_file.write(encrypted)
#note you can delete your original file if you want

Output

On executing the code you should see something like this:

The data in the file (sample.txt) before encryption:
 “my secret is in a file”
 The data in the file (enc_sample.txt) after encryption is:
 “gAAAAABbpv1I50T0J3Hy4cgoD30BSD0Jczj7sJVB1ThPN0bXE1zVUksgEeceTt378NMq39JoVCLMXptGPkDu8Q0-5HvKqYKZm4sLu5kXOUMUGM-ibi2aAFY=”

Step -4: Decrypting the File

Once you encrypted the file you can delete the original file and don’t forget to transfer that file you created to another location. Because you have encryption of the original file you can decrypt that file any time you want. In this section, we will see how we can decrypt it back to the original content.

Ist simple you just have to repeat the same process in reverse the previous section. It is exactly the same process, so let’s discuss how we can reverse the encrypted file to decrypted file and then code it.

First of all, we have to initialize the Fernet object and store that into the fernet variable. Next, we need to read the encrypted file (enc_sample.txt). Then we need to decrypt the encrypted file using the Fernet object and store it as an object. and finally, we are going to write it into a new file called “dec_sample.txt”.

Let’s see the code for this process:

#first use the key
f = Fernet(key)
#open the encrypted file
with open('enc_sample.txt', 'rb') as encrypted_file:
     encrypted = encrypted_file.read()
#decrypt the file
decrypted = f.decrypt(encrypted)
#finally you can write the decrypted file into a dec_sample.txt
with open('dec_sample.txt', 'wb') as decrypted_file:
     decrypted_file.write(decrypted)

Output

On executing the code, you should see the below output:

The data in the file (enc_sample.txt) after encryption is:
 “gAAAAABbpv1I50T0J3Hy4cgoD30BSD0Jczj7sJVB1ThPN0bXE1zVUksgEeceTt378NMq39JoVCLMXptGPkDu8Q0-5HvKqYKZm4sLu5kXOUMUGM-ibi2aAFY=”
The data in the file (dec_sample.txt) after decryption:
 “my secret is in a file”

If you compare the decrypted file that we created (dec_sample.txt) and the original file(sample.txt) you will see that both contents of the file are the same that means our process of encryption and decryption is successful.

Final Words

In this article, we learned how to use the symmetric type of file encryption to encrypt and decrypt the file and data inside of the file using a programming language called Python and a library called cryptography. In this library file encryption and decryption a simple process. We don’t need to implement our logical algorithm you can simply generate a key, encrypt the file and then decrypt with the key it’s secure and easy. So, how to Secure Files using Python? It’s easy, right?

Let us know if you guys have any questions/comments regarding this process.
Happy coding!

0 0 votes
Article Rating
  •  
    1
    Share
  •  
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
Previous

Compare Two Images and Highlight Differences using Python

How to Extract Text and Images from PDF using Python?

Next
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x