Difference between revisions of "Python-for-Automation/C2/File-Encryption-and-Decryption/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(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:
  
 
<div style="margin-left:1.27cm;margin-right:0cm;"></div>
 
 
{| border="1"
 
{| border="1"
 
|-
 
|-
Line 7: Line 5:
 
|| '''Narration'''
 
|| '''Narration'''
 
|-
 
|-
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| '''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"'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:  
+
|| '''Show slide''':  
  
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
|| In this tutorial, we will learn to
+
|| In this tutorial, we will learn to:
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Encrypt a folder using Symmetric Encryption'''</div>
+
* '''Encrypt''' a folder using '''Symmetric Encryption'''
* <div style="margin-left:1.27cm;margin-right:0cm;">Generate a '''key'''</div>
+
* Generate a '''key'''
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Decrypt '''the '''folder '''using the '''key'''</div>
+
* '''Decrypt '''the '''folder ''' using the '''key'''
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
|| Show Slide:
+
|| '''Show slide''':  
  
 
'''System Requirements'''
 
'''System Requirements'''
 
|| To record this tutorial, I am using
 
|| To record this tutorial, I am using
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Ubuntu''' '''Linux OS version 22.04'''</div>
+
* '''Ubuntu Linux OS''' version 22.04
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Python 3.12.3'''</div>
+
* '''Python ''' 3.12.3
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
|| Show Slide:'''Prerequisites'''
+
|| '''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
* <div style="margin-left:1.27cm;margin-right:0cm;">You must have basic knowledge of using Linux Terminal and Python</div>
+
* You must have basic knowledge of using Linux Terminal and Python
* <div style="margin-left:1.27cm;margin-right:0cm;">For pre-requisite Linux and Python Tutorials, please visit this website</div>
+
* For pre-requisite Linux and Python Tutorials, please visit this website
* <div style="margin-left:1.27cm;margin-right:0cm;">Python libraries required for automation must be installed</div>
+
* Python libraries required for automation must be installed
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
|| Show Slide:
+
||'''Show slide''':  
  
 
'''Code Files'''
 
'''Code Files'''
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">The files used in this tutorial are provided in the Code files link.</div>
+
* The files used in this tutorial are provided in the Code files link.
* <div style="margin-left:1.27cm;margin-right:0cm;">Please download and extract the files.</div>
+
* Please download and extract the files.
* <div style="margin-left:1.27cm;margin-right:0cm;">Make a copy and then use them while practicing.</div>
+
* Make a copy and then use them while practicing.
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
|| Show Slide:'''Encryption and Decryption'''
+
|| '''Show slide''':
|| '''Encryption '''secures data to prevent unauthorized access.
+
  
'''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
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
|| Show Slide:'''Symmetric Encryption'''
+
|| '''Show slide''':  
 +
 
 +
'''Symmetric Encryption'''
 
||  
 
||  
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Symmetric encryption technique '''uses the same key for both '''encryption '''and '''decryption'''.</div>
+
* '''Symmetric encryption technique '''uses the same key for both '''encryption '''and '''decryption'''.
* <div style="margin-left:1.27cm;margin-right:0cm;">This makes it fast and efficient.</div>
+
* This makes it fast and efficient.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''AES '''('''Advanced''' '''Encryption Standard''') is a strong and popular method.</div>
+
* '''AES '''('''Advanced''' '''Encryption Standard''') is a strong and popular method.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''AES '''supports 128, 192, and 256-bit keys for securing files and communications.</div>
+
* '''AES '''supports 128, 192, and 256-bit keys for securing files and communications.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
|| Show Slide:
+
|| '''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:
* <div style="margin-left:1.27cm;margin-right:0cm;">'''os''' manages operating system interfaces like file operations and environment variables</div>
+
* '''os''' manages operating system interfaces like file operations and environment variables
* <div style="margin-left:1.27cm;margin-right:0cm;">'''cryptography '''offers secure communication and data integrity through algorithms</div>
+
* '''cryptography ''' offers secure communication and data integrity through '''algorithms'''
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
|| Show Slide:'''Securing Files with AES Encryption'''
+
|| '''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<span style="color:#ff0000;"> </span>or CBC '''mode with a 128 bit key.
+
We will use the '''AES algorithm '''in '''Cipher Block Chaining''' or '''CBC ''' mode with a 128 bit key.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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'''.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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'''.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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'''.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
|| This '''function '''stores all the '''encrypted '''files to the specified path.  
+
|| This '''function ''' stores all the '''encrypted '''files to the specified path.  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Save the Code in the '''Downloads '''Folder
+
|| Save the Code in the '''Downloads '''Folder
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Save the code as '''encryption.py '''in the '''Downloads '''folder.
+
|| Save the code as '''encryption.py '''in the '''Downloads '''folder.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Open the terminal ('''Ctrl + Alt + T''')
+
|| Open the terminal ('''Ctrl + Alt + T''')
  
 
Start Virtual Environment
 
Start Virtual Environment
Line 194: Line 199:
  
 
'''> source Automation/bin/activate'''
 
'''> source Automation/bin/activate'''
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Open the '''terminal''' by pressing '''Control + Alt + T '''keys simultaneously.
+
|| 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.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Running the Code
+
|| Running the Code
  
 
Type  
 
Type  
  
'''> <span style="background-color:#ffffff;">cd Downloads'''</span>
+
'''cd Downloads'''
  
 
'''> python3 encryption.py'''
 
'''> python3 encryption.py'''
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | <span style="background-color:#ffffff;color:#252525;">Now type, </span><span style="background-color:#ffffff;color:#252525;">'''cd space Downloads'''</span><span style="background-color:#ffffff;color:#252525;">.</span>
+
|| Now type, '''cd space Downloads'''.
  
<span style="color:#252525;">Then type</span><span style="color:#252525;">''' python3 </span><span style="background-color:#ffffff;color:#252525;">space </span>encryption<span style="color:#252525;">.py'''</span><span style="background-color:#ffffff;color:#252525;"> and press </span><span style="color:#252525;">'''Enter'''</span><span style="background-color:#ffffff;color:#252525;"> to run the code.</span>
+
Then type''' python3 space encryption.py''' and press '''Enter''' to run the code.
  
<div style="color:#252525;">It executes the code and gives the output on the terminal as required.</div>
+
It executes the code and gives the output on the terminal as required.
  
<span style="background-color:#ffffff;color:#252525;">The </span><span style="background-color:#ffffff;color:#252525;">'''key '''</span><span style="background-color:#ffffff;color:#252525;">displayed here is used while </span><span style="background-color:#ffffff;color:#252525;">'''decrypting '''</span><span style="background-color:#ffffff;color:#252525;">the files. So, let us copy it.</span>
+
The '''key '''displayed here is used while '''decrypting '''the files. So, let us copy it.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Navigating to Downloads
+
|| Navigating to Downloads
  
'''Files App > Downloads > Sample'''
+
'''Files App >> Downloads >> Sample'''
  
 
'''Open doc3.txt'''
 
'''Open doc3.txt'''
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Go back to the '''Downloads folder '''and double click to open the '''Sample '''folder.
+
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.  
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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'''.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
 
|| The '''encrypted '''data is now '''decrypted '''using the '''fernet '''object.
 
|| The '''encrypted '''data is now '''decrypted '''using the '''fernet '''object.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
||  
 
||  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
||  
 
||  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
||  
 
||  
 
|| Now, we delete the '''encrypted '''version of the file.
 
|| Now, we delete the '''encrypted '''version of the file.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
||  
+
|| 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'''.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
||  
+
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
||  
 
||  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
||  
+
|| 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'''.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
||  
 
||  
 
|| 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.
|- style="border:1pt solid #000000;padding:0.176cm;"
+
|-  
 
||  
 
||  
 
|| Finally, we call the '''function '''which would '''decrypt '''the folder.
 
|| Finally, we call the '''function '''which would '''decrypt '''the folder.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Save the Code in the '''Downloads '''Folder
+
|| Save the Code in the '''Downloads '''Folder
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Save the code as '''decryption.py '''in the '''Downloads '''folder.
+
|| Save the code as '''decryption.py '''in the '''Downloads '''folder.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Observing the output on the terminal
+
|| Observing the output on the terminal
  
 
'''type'''
 
'''type'''
  
 
'''>python3 decryption.py'''
 
'''>python3 decryption.py'''
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Switch back to the''' terminal'''.
+
|| Switch back to the''' terminal'''.
  
<span style="background-color:#ffffff;color:#252525;">Now type, </span><span style="color:#252525;">'''python3 space </span>decryption<span style="color:#252525;">.py'''</span><span style="background-color:#ffffff;color:#252525;"> and press </span><span style="color:#252525;">'''Enter'''</span><span style="background-color:#ffffff;color:#252525;"> to run the code.</span>
+
Now type, '''python3 space decryption.py''' and press '''Enter''' to run the code.
  
<span style="background-color:#ffffff;color:#252525;">It executes the </span><span style="background-color:#ffffff;color:#252525;">'''script'''</span><span style="background-color:#ffffff;color:#252525;"> and displays the paths of all the </span><span style="background-color:#ffffff;color:#252525;">'''decrypted '''</span><span style="background-color:#ffffff;color:#252525;">files and the folder.</span>
+
It executes the '''script''' and displays the paths of all the '''decrypted '''files and the folder.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Navigating to Downloads
+
|| Navigating to Downloads
  
 
'''Files App > Downloads > Sample'''
 
'''Files App > Downloads > Sample'''
  
 
'''Open a file by double clicking'''
 
'''Open a file by double clicking'''
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Go back to the '''Downloads folder '''and double click to open the '''Sample '''folder.
+
|| 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.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Closing the virtual environment
+
|| Closing the virtual environment
  
 
Type  
 
Type  
  
 
'''> deactivate'''
 
'''> deactivate'''
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Switch back to the terminal to close the virtual environment.
+
|| Switch back to the terminal to close the virtual environment.
  
 
Type '''deactivate'''.
 
Type '''deactivate'''.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Show Slide:'''Summary'''
+
|| Show Slide:'''Summary'''
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | This brings us to the end of this tutorial. Let us summarise.
+
|| 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
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Encrypt a folder using Symmetric Encryption'''</div>
+
* '''Encrypt''' a folder using '''Symmetric Encryption'''
* <div style="margin-left:1.27cm;margin-right:0cm;">Generate a '''key'''</div>
+
* Generate a '''key'''
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Decrypt '''the '''folder '''using the '''key'''</div>
+
* '''Decrypt '''the '''folder '''using the '''key'''
  
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Show Slide:
+
|| '''Show Slide''':
  
 
'''Assignment'''
 
'''Assignment'''
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | As an assignment, please do the following:
+
|| As an assignment, please do the following:
  
* <div style="margin-left:1.27cm;margin-right:0cm;">Modify the program '''decryption.py '''</div>
+
* Modify the program '''decryption.py '''
* <div style="margin-left:1.27cm;margin-right:0cm;">Add exception handling to capture the missing encryption key</div>
+
* Add exception handling to capture the missing encryption key
  
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Show Slide:'''About the Spoken Tutorial Project'''
+
|| '''Show Slide''':
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | The video at the following link summarises the '''Spoken Tutorial Project.'''Please download and watch it.
+
 
 +
'''About the Spoken Tutorial Project'''
 +
|| The video at the following link summarises the '''Spoken Tutorial Project.
 +
 
 +
'''Please download and watch it.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Show Slide:
+
|| '''Show Slide''':
  
 
'''Spoken Tutorial Workshops'''
 
'''Spoken Tutorial Workshops'''
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | The '''Spoken Tutorial Project''' team conducts workshops and gives certificates.
+
|| The '''Spoken Tutorial Project''' team conducts workshops and gives certificates.
  
 
For more details, please write to us.
 
For more details, please write to us.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Show Slide:'''Answers for THIS Spoken Tutorial'''
+
|| '''Show Slide''':
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Please post your timed queries in this forum.
+
 
 +
'''Answers for THIS Spoken Tutorial'''
 +
|| Please post your timed queries in this forum.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Show Slide:  
+
|| '''Show Slide''':  
  
 
'''FOSSEE Forum'''
 
'''FOSSEE Forum'''
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | For any general or technical questions on '''Python for Automation''', visit the '''FOSSEE forum''' and post your question.
+
|| For any general or technical questions on '''Python for Automation''', visit the '''FOSSEE forum''' and post your question.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Show Slide:'''Acknowledgement'''
+
|| '''Show Slide''':
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | The '''Spoken Tutorial Project''' was established by the '''Ministry of Education, Government of India.'''
+
 
 +
'''Acknowledgement'''
 +
|| The '''Spoken Tutorial Project''' was established by the '''Ministry of Education, Government of India.'''
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | Show Slide:'''Thank You'''
+
|| '''Show Slide''':
| style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.092cm;padding-right:0.191cm;" | This is '''Sai''' '''Sathwik''', a FOSSEE Semester Long Intern 2024, IIT Bombay signing off.
+
 
 +
'''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:


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