Skip to content

Commit 626bb15

Browse files
committed
added file encryption and decryption tutorial
1 parent b4988ee commit 626bb15

File tree

6 files changed

+3391
-0
lines changed

6 files changed

+3391
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
1616
- [Writing a Keylogger in Python from Scratch](https://www.thepythoncode.com/article/write-a-keylogger-python). ([code](ethical-hacking/keylogger))
1717
- [Making a Port Scanner using sockets in Python](https://www.thepythoncode.com/article/make-port-scanner-python). ([code](ethical-hacking/port_scanner))
1818
- [How to Create a Reverse Shell in Python](https://www.thepythoncode.com/article/create-reverse-shell-python). ([code](ethical-hacking/reverse_shell))
19+
- [How to Encrypt and Decrypt Files in Python](https://www.thepythoncode.com/article/encrypt-decrypt-files-symmetric-python). ([code](ethical-hacking/file-encryption))
1920

2021
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
2122
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)

Diff for: ethical-hacking/file-encryption/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [How to Encrypt and Decrypt Files in Python](https://www.thepythoncode.com/article/encrypt-decrypt-files-symmetric-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
-
5+
```
6+
python crypt --help
7+
```
8+
**Output:**
9+
```
10+
usage: crypt.py [-h] [-g] [-e] [-d] file
11+
12+
Simple File Encryptor Script
13+
14+
positional arguments:
15+
file File to encrypt/decrypt
16+
17+
optional arguments:
18+
-h, --help show this help message and exit
19+
-g, --generate-key Whether to generate a new key or use existing
20+
-e, --encrypt Whether to encrypt the file, only -e or -d can be
21+
specified.
22+
-d, --decrypt Whether to decrypt the file, only -e or -d can be
23+
specified.
24+
```
25+
- If you want to encrypt `data.csv` using a new generated key:
26+
```
27+
python crypt.py data.csv --generate-key --encrypt
28+
```
29+
- To decrypt it (must be same key, using `--generate-key` flag with decrypt won't be able to get the original file):
30+
```
31+
python crypt.py data.csv --decrypt
32+
```
33+
- To encrypt another file using the same key generated previously:
34+
```
35+
python crypt.py another_file --encrypt
36+
```

Diff for: ethical-hacking/file-encryption/crypt.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from cryptography.fernet import Fernet
2+
import os
3+
4+
5+
def write_key():
6+
"""
7+
Generates a key and save it into a file
8+
"""
9+
key = Fernet.generate_key()
10+
with open("key.key", "wb") as key_file:
11+
key_file.write(key)
12+
13+
def load_key():
14+
"""
15+
Loads the key from the current directory named `key.key`
16+
"""
17+
return open("key.key", "rb").read()
18+
19+
20+
def encrypt(filename, key):
21+
"""
22+
Given a filename (str) and key (bytes), it encrypts the file and write it
23+
"""
24+
f = Fernet(key)
25+
with open(filename, "rb") as file:
26+
# read all file data
27+
file_data = file.read()
28+
# encrypt data
29+
encrypted_data = f.encrypt(file_data)
30+
# write the encrypted file
31+
with open(filename, "wb") as file:
32+
file.write(encrypted_data)
33+
34+
35+
def decrypt(filename, key):
36+
"""
37+
Given a filename (str) and key (bytes), it decrypts the file and write it
38+
"""
39+
f = Fernet(key)
40+
with open(filename, "rb") as file:
41+
# read the encrypted data
42+
encrypted_data = file.read()
43+
# decrypt data
44+
decrypted_data = f.decrypt(encrypted_data)
45+
# write the original file
46+
with open(filename, "wb") as file:
47+
file.write(decrypted_data)
48+
49+
50+
if __name__ == "__main__":
51+
import argparse
52+
parser = argparse.ArgumentParser(description="Simple File Encryptor Script")
53+
parser.add_argument("file", help="File to encrypt/decrypt")
54+
parser.add_argument("-g", "--generate-key", dest="generate_key", action="store_true",
55+
help="Whether to generate a new key or use existing")
56+
parser.add_argument("-e", "--encrypt", action="store_true",
57+
help="Whether to encrypt the file, only -e or -d can be specified.")
58+
parser.add_argument("-d", "--decrypt", action="store_true",
59+
help="Whether to decrypt the file, only -e or -d can be specified.")
60+
61+
args = parser.parse_args()
62+
file = args.file
63+
generate_key = args.generate_key
64+
65+
if generate_key:
66+
write_key()
67+
# load the key
68+
key = load_key()
69+
70+
encrypt_ = args.encrypt
71+
decrypt_ = args.decrypt
72+
73+
if encrypt_ and decrypt_:
74+
raise TypeError("Please specify whether you want to encrypt the file or decrypt it.")
75+
elif encrypt_:
76+
encrypt(file, key)
77+
elif decrypt_:
78+
decrypt(file, key)
79+
else:
80+
raise TypeError("Please specify whether you want to encrypt the file or decrypt it.")
81+
82+
83+
84+
85+
86+

0 commit comments

Comments
 (0)