Python-for-Automation/C2/File-Encryption-and-Decryption/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Show slide:

Welcome

Hello and Welcome to the Spoken Tutorial on "File Encryption and Decryption".
Show slide:


Learning Objectives

In this tutorial, we will learn to:
  • Encrypt a folder using Symmetric Encryption
  • Generate a key
  • Decrypt the folder using the key
Show slide:

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux OS version 22.04
  • Python 3.12.3
Show slide:

Prerequisites

https://www.spoken-tutorial.org

To follow this tutorial
  • You must have basic knowledge of using Linux Terminal and Python
  • For pre-requisite Linux and Python Tutorials, please visit this website
  • Python libraries required for automation must be installed
Show slide:

Code Files

  • The files used in this tutorial are provided in the Code files link.
  • Please download and extract the files.
  • Make a copy and then use them while practicing.
Show slide:

Encryption and Decryption

Encryption secures data to prevent unauthorized access.

Decryption converts encrypted data back to its original form using a key.

This allows authorized users to access the information

Show slide:

Symmetric Encryption

  • Symmetric encryption technique uses the same key for both encryption and decryption.
  • This makes it fast and efficient.
  • AES (Advanced Encryption Standard) is a strong and popular method.
  • AES supports 128, 192, and 256-bit keys for securing files and communications.
Show slide:

Encryption and Decryption - Libraries

To automate the encryption and decryption of a folder, we need the below libraries:
  • os manages operating system interfaces like file operations and environment variables
  • cryptography offers secure communication and data integrity through algorithms
Show slide:

Securing Files with AES Encryption

In this tutorial, we will see how to encrypt a folder named Sample and then decrypt it.

We will use the AES algorithm in Cipher Block Chaining or CBC mode with a 128 bit key.

Open the Downloads Folder

Files App >> Downloads Folder >> Sample

I have created a folder Sample which contains some files for demonstration.

Let us see how the Encryption and Decryption can be automated.

Point to the encryption.py in downloads folder

Open the Text Editor with the source file

I have created the source file encryption.py for demonstration.

Now, we will go through the source code in the text editor.

Looking at the Code This source code has encryption functionalities to provide security.
Highlight:

import os

from cryptography.fernet import Fernet

First we need to import the libraries required to encrypt and decrypt folders.

The fernet class is used for symmetric encryption.

Highlight:

def encrypt_file(file_path, key):

We define a function to encrypt each file by mentioning the path to it.
Highlight:

with open(file_path, 'rb') as file:

file_data = file.read()

We then open the file in binary read mode rb and read its contents.

Binary read is used to read raw bytes from a file without any translation or conversion.

Highlight:

fernet = Fernet(key)

A fernet instance is created using the provided key.
Highlight:

encrypted_data = fernet.encrypt(file_data)

The data read from the file is encrypted using the fernet instance.
Highlight:

encrypted_file_path = file_path + '.encrypted'

We then add .encrypted to the original path to create a new file path.
Highlight:

with open(encrypted_file_path, 'wb') as encrypted_file:

encrypted_file.write(encrypted_data)

Now, we open the file in binary write mode wb and write the encrypted data to it.

Binary Write is used to write raw bytes to a file without any translation or conversion.

Highlight:

os.remove(file_path)

To prevent unauthorized access, only the encrypted version is retained.

The original files are removed using this command.

Highlight: This function stores all the encrypted files to the specified path.
Highlight:

encrypted_files = []

An empty list is initialized to store the paths of the encrypted files.
Highlight: We iterate through all directories and files mentioned in the path.

We then construct the full path, encrypt the files and add them to the list.

Highlight:

def main(folder_path):

Finally, we define the main function to invoke the encryption process.
Highlight:

key = Fernet.generate_key()

We generate a random symmetric encryption key using this function.
Highlight:

print(f'Encryption Key: {key.decode()}')

The generated key is decoded and displayed.
Highlight: We call the function to encrypt all files in the specified path using the key.
Highlight: Now, we iterate over the encrypted files and print the path of each file.
Highlight: Finally, we set the path of the folder and call the main function with the path.
Save the Code in the Downloads Folder Save the code as encryption.py in the Downloads folder.
Open the terminal (Ctrl + Alt + T)

Start Virtual Environment

Type

> source Automation/bin/activate

Open the terminal by pressing Control, Alt, T keys simultaneously.

We will open the virtual environment we created for the Automation series.

Type source space Automation forward slash bin forward slash activate.

Then press enter.

Running the Code

Type

cd Downloads

> python3 encryption.py

Now type, cd space Downloads.

Then type python3 space encryption.py and press Enter to run the code.

It executes the code and gives the output on the terminal as required.

The key displayed here is used while decrypting the files. So, let us copy it.

Navigating to Downloads

Files App >> Downloads >> Sample

Open doc3.txt

Go back to the Downloads folder and double click to open the Sample folder.

The files and folders here are now encrypted as you can see because of the extension.

If we have any sub folders, the files within them will be encrypted as well.

Let us open the encrypted file and check.

We can see the content of the file is encrypted and not in a readable format.

We need to decrypt the file to make it readable. Let us see how to do that.

Point to the decryption.py in downloads folder

Open the Text Editor with the source file

I have created the source file decryption.py for demonstration.

Now, we will go through the source code in the text editor.

Highlight: The same libraries we used to encrypt are used here as well.
Highlight: We define a function to decrypt each encrypted file by mentioning the path.
Highlight:

fernet = Fernet(key)

A fernet instance is created using the provided key.
Highlight: We then open the encrypted file in binary read mode rb and read its contents.
Highlight: The encrypted data is now decrypted using the fernet object.
Next, we define a path for the decrypted file after removing .encrypted from the file path.
Now, we open a new file in binary write mode wb and write the decrypted data to it.
Now, we delete the encrypted version of the file.
Point to the path. The path of the decrypted file is displayed on the terminal.
Point to the function. The next function we define is to store all the decrypted files.
We iterate over the directory tree and construct the full path for each file.

The function to decrypt the files is called here.

Point to the path. The path of the decrypted folder is displayed on the terminal.
We set the path of the folder which contains the encrypted files.

Then, we paste the key which we copied earlier.

Finally, we call the function which would decrypt the folder.
Save the Code in the Downloads Folder Save the code as decryption.py in the Downloads folder.
Observing the output on the terminal

type

>python3 decryption.py

Switch back to the terminal.

Now type, python3 space decryption.py and press Enter to run the code.

It executes the script and displays the paths of all the decrypted files and the folder.

Navigating to Downloads

Files App > Downloads > Sample

Open a file by double clicking

Go back to the Downloads folder and double click to open the Sample folder.

You can see the list of all the files we had originally not encrypted.

Now, you can double click to open the files.

Closing the virtual environment

Type

> deactivate

Switch back to the terminal to close the virtual environment.

Type deactivate.

Show Slide:Summary This brings us to the end of this tutorial.

Let us summarise.

In this tutorial, we have learnt to

  • Encrypt a folder using Symmetric Encryption
  • Generate a key
  • Decrypt the folder using the key
Show Slide:

Assignment

As an assignment, please do the following:
  • Modify the program decryption.py
  • Add exception handling to capture the missing encryption key
Show Slide:

About the Spoken Tutorial Project

The video at the following link summarises the Spoken Tutorial Project.

Please download and watch it.

Show Slide:

Spoken Tutorial Workshops

The Spoken Tutorial Project team conducts workshops and gives certificates.

For more details, please write to us.

Show Slide:

Answers for THIS Spoken Tutorial

Please post your timed queries in this forum.
Show Slide:

FOSSEE Forum

For any general or technical questions on Python for Automation, visit the FOSSEE forum and post your question.
Show Slide:

Acknowledgement

The Spoken Tutorial Project was established by the Ministry of Education, Government of India.
Show Slide:

Thank You

This is Sai Sathwik, a FOSSEE Semester Long Intern 2024, IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat