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

‎README.md‎

Lines changed: 1 addition & 0 deletions
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)
Lines changed: 36 additions & 0 deletions
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+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
fromcryptography.fernetimportFernet
2+
importos
3+
4+
5+
defwrite_key():
6+
"""
7+
Generates a key and save it into a file
8+
"""
9+
key=Fernet.generate_key()
10+
withopen("key.key", "wb") askey_file:
11+
key_file.write(key)
12+
13+
defload_key():
14+
"""
15+
Loads the key from the current directory named `key.key`
16+
"""
17+
returnopen("key.key", "rb").read()
18+
19+
20+
defencrypt(filename, key):
21+
"""
22+
Given a filename (str) and key (bytes), it encrypts the file and write it
23+
"""
24+
f=Fernet(key)
25+
withopen(filename, "rb") asfile:
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+
withopen(filename, "wb") asfile:
32+
file.write(encrypted_data)
33+
34+
35+
defdecrypt(filename, key):
36+
"""
37+
Given a filename (str) and key (bytes), it decrypts the file and write it
38+
"""
39+
f=Fernet(key)
40+
withopen(filename, "rb") asfile:
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+
withopen(filename, "wb") asfile:
47+
file.write(decrypted_data)
48+
49+
50+
if__name__=="__main__":
51+
importargparse
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+
ifgenerate_key:
66+
write_key()
67+
# load the key
68+
key=load_key()
69+
70+
encrypt_=args.encrypt
71+
decrypt_=args.decrypt
72+
73+
ifencrypt_anddecrypt_:
74+
raiseTypeError("Please specify whether you want to encrypt the file or decrypt it.")
75+
elifencrypt_:
76+
encrypt(file, key)
77+
elifdecrypt_:
78+
decrypt(file, key)
79+
else:
80+
raiseTypeError("Please specify whether you want to encrypt the file or decrypt it.")
81+
82+
83+
84+
85+
86+

0 commit comments

Comments
(0)