Difference between revisions of "Python-for-Automation/C2/File-Encryption-and-Decryption/English"
(Created page with " <div style="margin-left:1.27cm;margin-right:0cm;"></div> {| border="1" |- || '''Visual Cue''' || '''Narration''' |- |- style="border:1pt solid #000000;padding-top:0cm;paddin...") |
|||
Line 1: | Line 1: | ||
− | |||
− | |||
{| border="1" | {| border="1" | ||
|- | |- | ||
Line 7: | Line 5: | ||
|| '''Narration''' | || '''Narration''' | ||
|- | |- | ||
− | + | || '''Show slide''': | |
− | || Show slide: | + | |
'''Welcome''' | '''Welcome''' | ||
− | || Welcome to the Spoken Tutorial on '''"File Encryption and Decryption" | + | || Hello and Welcome to the Spoken Tutorial on '''"File Encryption and Decryption"'''. |
− | |- | + | |- |
− | || Show slide: | + | || '''Show slide''': |
'''Learning Objectives''' | '''Learning Objectives''' | ||
− | || In this tutorial, we will learn to | + | || In this tutorial, we will learn to: |
− | * | + | * '''Encrypt''' a folder using '''Symmetric Encryption''' |
− | * | + | * Generate a '''key''' |
− | * | + | * '''Decrypt '''the '''folder ''' using the '''key''' |
− | |- | + | |- |
− | || Show | + | || '''Show slide''': |
'''System Requirements''' | '''System Requirements''' | ||
|| To record this tutorial, I am using | || To record this tutorial, I am using | ||
− | * | + | * '''Ubuntu Linux OS''' version 22.04 |
− | + | * '''Python ''' 3.12.3 | |
− | |- | + | |- |
− | || Show | + | || '''Show slide''': |
+ | |||
+ | '''Prerequisites''' | ||
[https://www.spoken-tutorial.org/ https://www.spoken-tutorial.org] | [https://www.spoken-tutorial.org/ https://www.spoken-tutorial.org] | ||
|| To follow this tutorial | || 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 | + | ||'''Show slide''': |
'''Code Files''' | '''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''': |
− | + | ||
− | '''Decryption '''converts '''encrypted '''data back to its original form using a key. | + | '''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 | This allows authorized users to access the information | ||
− | |- | + | |- |
− | || Show | + | || '''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 | + | || '''Show slide''': |
'''Encryption and Decryption - Libraries''' | '''Encryption and Decryption - Libraries''' | ||
− | || To automate the '''encryption '''and '''decryption '''of a '''folder''', we need the below 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 | + | || '''Show slide''': |
+ | |||
+ | '''Securing Files with AES Encryption''' | ||
|| In this '''tutorial''', we will see how to '''encrypt '''a folder named '''Sample '''and then '''decrypt '''it. | || 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 | + | We will use the '''AES algorithm '''in '''Cipher Block Chaining''' or '''CBC ''' mode with a 128 bit key. |
− | |- | + | |- |
|| Open the '''Downloads Folder''' | || Open the '''Downloads Folder''' | ||
− | '''Files App > Downloads Folder > Sample''' | + | '''Files App >> Downloads Folder >> Sample''' |
− | || I have created a folder '''Sample '''which contains some files for demonstration. | + | || I have created a folder '''Sample ''' which contains some files for demonstration. |
Let us see how the '''Encryption and Decryption''' can be automated. | Let us see how the '''Encryption and Decryption''' can be automated. | ||
− | |- | + | |- |
|| Point to the '''encryption.py''' in downloads folder | || Point to the '''encryption.py''' in downloads folder | ||
Line 88: | Line 93: | ||
Now, we will go through the source code in the text editor. | Now, we will go through the source code in the text editor. | ||
− | |- | + | |- |
|| Looking at the Code | || Looking at the Code | ||
− | || This source code has '''encryption '''functionalities to provide security. | + | || This source code has '''encryption ''' functionalities to provide security. |
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
Line 97: | Line 102: | ||
'''from cryptography.fernet import Fernet''' | '''from cryptography.fernet import Fernet''' | ||
− | || First we need to import the '''libraries '''required to '''encrypt '''and '''decrypt '''folders. | + | || First we need to import the '''libraries '''required to '''encrypt '''and '''decrypt ''' folders. |
The '''fernet class''' is used for '''symmetric encryption'''. | The '''fernet class''' is used for '''symmetric encryption'''. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
'''def encrypt_file(file_path, key):''' | '''def encrypt_file(file_path, key):''' | ||
|| We define a function to '''encrypt '''each '''file '''by mentioning the path to it. | || We define a function to '''encrypt '''each '''file '''by mentioning the path to it. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
Line 114: | Line 119: | ||
'''Binary read''' is used to read raw bytes from a file without any translation or conversion. | '''Binary read''' is used to read raw bytes from a file without any translation or conversion. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
'''fernet = Fernet(key)''' | '''fernet = Fernet(key)''' | ||
− | || A '''fernet '''instance is created using the provided '''key'''. | + | || A '''fernet ''' instance is created using the provided '''key'''. |
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
'''encrypted_data = fernet.encrypt(file_data)''' | '''encrypted_data = fernet.encrypt(file_data)''' | ||
− | || The data read from the file is '''encrypted '''using the '''fernet '''instance. | + | || The data read from the file is '''encrypted ''' using the '''fernet '''instance. |
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
'''encrypted_file_path = file_path + '.encrypted'''' | '''encrypted_file_path = file_path + '.encrypted'''' | ||
|| We then add '''.encrypted''' to the original '''path '''to create a new '''file path'''. | || We then add '''.encrypted''' to the original '''path '''to create a new '''file path'''. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
Line 135: | Line 140: | ||
'''encrypted_file.write(encrypted_data)''' | '''encrypted_file.write(encrypted_data)''' | ||
− | || Now, we open the '''file '''in '''binary write mode wb''' and write the '''encrypted '''data to it. | + | || 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. | '''Binary Write''' is used to write raw bytes to a file without any translation or conversion. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
'''os.remove(file_path)''' | '''os.remove(file_path)''' | ||
− | || To prevent unauthorized access, only the '''encrypted '''version is retained. | + | || To prevent unauthorized access, only the '''encrypted ''' version is retained. |
The original files are removed using this command. | The original files are removed using this command. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
− | || This '''function '''stores all the '''encrypted '''files to the specified path. | + | || This '''function ''' stores all the '''encrypted '''files to the specified path. |
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
'''encrypted_files = []''' | '''encrypted_files = []''' | ||
|| An empty list is initialized to store the paths of the '''encrypted '''files. | || An empty list is initialized to store the paths of the '''encrypted '''files. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
|| We iterate through all '''directories '''and '''files '''mentioned in the path. | || 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. | We then construct the full path, '''encrypt '''the files and add them to the list. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
'''def main(folder_path):''' | '''def main(folder_path):''' | ||
|| Finally, we define the main '''function '''to invoke the '''encryption '''process. | || Finally, we define the main '''function '''to invoke the '''encryption '''process. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
'''key = Fernet.generate_key()''' | '''key = Fernet.generate_key()''' | ||
− | || We generate a random '''symmetric encryption key '''using this function. | + | || We generate a random '''symmetric encryption key ''' using this function. |
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
'''print(f'Encryption Key: {key.decode()}')''' | '''print(f'Encryption Key: {key.decode()}')''' | ||
|| The generated key is decoded and displayed. | || The generated key is decoded and displayed. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
|| We call the '''function '''to '''encrypt '''all files in the specified path using the key. | || We call the '''function '''to '''encrypt '''all files in the specified path using the key. | ||
− | |- | + | |- |
|| Highlight:''' ''' | || Highlight:''' ''' | ||
|| Now, we iterate over the '''encrypted '''files and print the path of each file. | || Now, we iterate over the '''encrypted '''files and print the path of each file. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
|| Finally, we set the '''path '''of the folder and call the '''main function '''with the path. | || 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 | Start Virtual Environment | ||
Line 194: | Line 199: | ||
'''> source Automation/bin/activate''' | '''> 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. | We will open the virtual environment we created for the '''Automation''' series. | ||
− | Type '''source space Automation forward slash bin forward slash activate | + | Type '''source space Automation forward slash bin forward slash activate'''. |
Then press enter. | Then press enter. | ||
|- | |- | ||
− | | | + | || Running the Code |
Type | Type | ||
− | ''' | + | '''cd Downloads''' |
'''> python3 encryption.py''' | '''> 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''' | + | '''Files App >> Downloads >> Sample''' |
'''Open doc3.txt''' | '''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. | The files and folders here are now '''encrypted''' as you can see because of the extension. | ||
Line 233: | Line 238: | ||
We need to decrypt the file to make it readable. Let us see how to do that. | 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 | || Point to the '''decryption.py''' in downloads folder | ||
Line 240: | Line 245: | ||
Now, we will go through the source code in the text editor. | Now, we will go through the source code in the text editor. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
|| The same '''libraries '''we used to '''encrypt '''are used here as well. | || The same '''libraries '''we used to '''encrypt '''are used here as well. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
|| We define a function to '''decrypt '''each '''encrypted file '''by mentioning the path. | || We define a function to '''decrypt '''each '''encrypted file '''by mentioning the path. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
'''fernet = Fernet(key)''' | '''fernet = Fernet(key)''' | ||
|| A '''fernet '''instance is created using the provided '''key'''. | || A '''fernet '''instance is created using the provided '''key'''. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
|| We then open the '''encrypted file '''in '''binary read mode rb''' and read its contents. | || We then open the '''encrypted file '''in '''binary read mode rb''' and read its contents. | ||
− | |- | + | |- |
|| Highlight: | || Highlight: | ||
|| The '''encrypted '''data is now '''decrypted '''using the '''fernet '''object. | || 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. | || 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 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. | || Now, we delete the '''encrypted '''version of the file. | ||
− | |- | + | |- |
− | || | + | || Point to the path. |
|| The path of the '''decrypted '''file is displayed on the '''terminal'''. | || 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. | || 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. | || We '''iterate '''over the '''directory '''tree and construct the full path for each file. | ||
+ | |||
The function to '''decrypt '''the files is called here. | The function to '''decrypt '''the files is called here. | ||
− | |- | + | |- |
− | || | + | || Point to the path. |
|| The path of the '''decrypted '''folder is displayed on the '''terminal'''. | || The path of the '''decrypted '''folder is displayed on the '''terminal'''. | ||
− | |- | + | |- |
|| | || | ||
|| We set the path of the folder which contains the encrypted files. | || We set the path of the folder which contains the encrypted files. | ||
Then, we paste the key which we copied earlier. | Then, we paste the key which we copied earlier. | ||
− | |- | + | |- |
|| | || | ||
|| Finally, we call the '''function '''which would '''decrypt '''the folder. | || 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''' | '''type''' | ||
'''>python3 decryption.py''' | '''>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''' | '''Files App > Downloads > Sample''' | ||
'''Open a file by double clicking''' | '''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'''. | You can see the list of all the '''files '''we had originally not '''encrypted'''. | ||
Line 313: | Line 319: | ||
Now, you can double click to open the files. | Now, you can double click to open the files. | ||
|- | |- | ||
− | | | + | || Closing the virtual environment |
Type | Type | ||
'''> deactivate''' | '''> deactivate''' | ||
− | | | + | || Switch back to the terminal to close the virtual environment. |
Type '''deactivate'''. | Type '''deactivate'''. | ||
|- | |- | ||
− | | | + | || Show Slide:'''Summary''' |
− | | | + | || This brings us to the end of this tutorial. |
+ | |||
+ | Let us summarise. | ||
In this tutorial, we have learnt to | 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''' | '''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''' | '''Spoken Tutorial Workshops''' | ||
− | | | + | || The '''Spoken Tutorial Project''' team conducts workshops and gives certificates. |
For more details, please write to us. | 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''' | '''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. | Thanks for joining. | ||
|- | |- | ||
|} | |} |
Latest revision as of 13:19, 27 November 2024
Visual Cue | Narration |
Show slide:
Welcome |
Hello and Welcome to the Spoken Tutorial on "File Encryption and Decryption". |
Show slide:
|
In this tutorial, we will learn to:
|
Show slide:
System Requirements |
To record this tutorial, I am using
|
Show slide:
Prerequisites |
To follow this tutorial
|
Show slide:
Code Files |
|
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 |
|
Show slide:
Encryption and Decryption - Libraries |
To automate the encryption and decryption of a folder, we need the below libraries:
|
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
|
Show Slide:
Assignment |
As an assignment, please do the following:
|
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. |