Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3bb02ed

Browse files
Merge pull request avinashkranjan#1748 from Abhinavcode13/patch-5
Added the File Encryption/Decryption script
2 parents 9cd87ed + ec25b7d commit 3bb02ed

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# File Downloader Script
2+
3+
4+
This script is about file encryption and decryption using the Fernet symmetric encryption algorithm from the cryptography library in Python.
5+
6+
The script provides functions to generate an encryption key, load the key from a file, encrypt a specified file, and decrypt a previously encrypted file.
7+
8+
<br>
9+
10+
## Instructions 📝
11+
12+
### Step 1:
13+
14+
Make sure you have Python installed on your system. You can download and install Python from the official Python website (https://www.python.org).
15+
16+
### Step 2:
17+
18+
Open a text editor and copy the script provided in the previous response into a new file.
19+
20+
21+
22+
### Step 3:
23+
24+
Save the file with a .py extension, such as file_encryption.py
25+
26+
27+
### Step 4:
28+
29+
Open a terminal or command prompt and navigate to the directory where you saved the Python script.
30+
31+
32+
### Step 5:
33+
34+
Check the Output: The script will attempt to download the file from the specified URL and save it to the destination path. You will see either a success message or a failure message displayed in the command prompt or terminal, depending on the outcome of the download operation.
35+
36+
37+
### Step 6:
38+
39+
If you haven't generated a key before, uncomment the generate_key() line in the script by removing the # character at the beginning of the line. This line should only be executed once to generate the key. After generating the key, you can comment out the generate_key() line again by adding the # character.
40+
41+
### step 7:
42+
43+
Run the script again (python file_encryption.py) to encrypt the file specified in the file_to_encrypt variable. It will print a success message when the encryption is complete.
44+
45+
### step 8:
46+
To decrypt the file, run the script once more (python file_encryption.py). It will use the same key to decrypt the file, and print a success message when the decryption is complete.
47+
48+
<hr>
49+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from cryptography.fernet import Fernet
2+
3+
4+
def generate_key():
5+
key = Fernet.generate_key()
6+
with open("key.key", "wb") as key_file:
7+
key_file.write(key)
8+
9+
10+
def load_key():
11+
with open("key.key", "rb") as key_file:
12+
key = key_file.read()
13+
return key
14+
15+
16+
def encrypt_file(file_path, key):
17+
cipher = Fernet(key)
18+
with open(file_path, "rb") as file:
19+
file_data = file.read()
20+
encrypted_data = cipher.encrypt(file_data)
21+
with open(file_path, "wb") as file:
22+
file.write(encrypted_data)
23+
24+
25+
def decrypt_file(file_path, key):
26+
cipher = Fernet(key)
27+
with open(file_path, "rb") as file:
28+
encrypted_data = file.read()
29+
decrypted_data = cipher.decrypt(encrypted_data)
30+
with open(file_path, "wb") as file:
31+
file.write(decrypted_data)
32+
33+
34+
# Generate a new key (run this only once)
35+
# generate_key()
36+
37+
# Load the key
38+
key = load_key()
39+
40+
# Encrypt a file
41+
file_to_encrypt = "example.txt"
42+
encrypt_file(file_to_encrypt, key)
43+
print("File encrypted successfully!")
44+
45+
# Decrypt the file
46+
file_to_decrypt = "example.txt"
47+
decrypt_file(file_to_decrypt, key)
48+
print("File decrypted successfully!")

‎SCRIPTS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,6 @@
9898
|94\. | Linked List | Linked List data Scripts are used to store and manage a collection of elements in a dynamic and flexible manner | [Take Me](./Linked%20List/) | [Himanshu Agarwal](https://github.com/himanshu-03)
9999
| 95\. | Searching Techniques | Searching techniques are scripts used to find a specific element or value within a collection of data. | [Take Me](./Searching%20Techniques/) | [Himanshu Agarwal](https://github.com/himanshu-03)
100100
| 96\. | Sorting Techniques | Sorting techniques are scripts that arrange a collection of data elements in a particular order, typically ascending or descending. | [Take Me](./Sorting%20Techniques/) | [Himanshu Agarwal](https://github.com/himanshu-03)
101+
| 99\. | File Organizer script | A File Organizer script is a program or script that automates the process of organizing files within a directory or multiple directories | [Take Me](./File Organizer script/) | [Abhinav kumar](https://github.com/Abhinavcode13)
102+
| 100\. | File Encryption/Decryption script | A File Organizer script is a program or script that automates the process of organizing files within a directory or multiple directories | [Take Me](./File Encryption/Decryption script/) | [Abhinav kumar](https://github.com/Abhinavcode13)
101103

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /