Skip to content

Commit fb69546

Browse files
author
martian
committed
Initial commit
1 parent 7ee64a3 commit fb69546

File tree

3 files changed

+180
-2
lines changed

3 files changed

+180
-2
lines changed

README.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,48 @@
1-
# enc
2-
Encrypting files and directories with enc
1+
# Enc
2+
3+
Enc is a command-line tool for encrypting and decrypting files and folders using AES encryption. Encrypted files can only be decrypted with the key specified by the user.
4+
5+
## Features
6+
7+
- Encrypt files and folders
8+
- Decrypt encrypted files and folders with the user-specified key
9+
10+
## Usage
11+
12+
To encrypt a file:
13+
14+
enc --encrypt f <file_path>
15+
16+
17+
To encrypt a folder:
18+
19+
enc --encrypt F <folder_path>
20+
21+
22+
To decrypt an encrypted file:
23+
24+
enc --decrypt f <file_path>
25+
26+
27+
To decrypt an encrypted folder:
28+
29+
enc --decrypt F <folder_path>
30+
31+
32+
You will be prompted to enter the encryption/decryption key, which must be at least 16 characters long.
33+
34+
## Example
35+
36+
Encrypt a file:
37+
38+
enc --encrypt f secret.txt
39+
40+
41+
Decrypt the encrypted file:
42+
43+
enc --decrypt f secret.txt
44+
45+
46+
## Note
47+
48+
Make sure to keep your encryption key secure. Once a file or folder is encrypted, it can only be decrypted with the correct key.

enc

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
import os
4+
from Crypto.Cipher import AES
5+
from Crypto.Util.Padding import pad, unpad
6+
from tqdm import tqdm
7+
import getpass
8+
import base64
9+
10+
red='\033[1;31m'
11+
green='\033[1;32m'
12+
no_color='\033[0m'
13+
14+
15+
def encrypt_file_inplace(file_path, key):
16+
try:
17+
with open(file_path, 'rb') as f:
18+
plaintext = f.read()
19+
20+
cipher = AES.new(key, AES.MODE_CBC)
21+
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
22+
23+
with open(file_path, 'wb') as f:
24+
f.write(cipher.iv + ciphertext)
25+
except Exception as e:
26+
print(f"Error encrypting file {file_path}: {e}")
27+
28+
def decrypt_file_inplace(file_path, key):
29+
try:
30+
with open(file_path, 'rb') as f:
31+
data = f.read()
32+
iv = data[:AES.block_size]
33+
ciphertext = data[AES.block_size:]
34+
35+
cipher = AES.new(key, AES.MODE_CBC, iv)
36+
plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)
37+
38+
with open(file_path, 'wb') as f:
39+
f.write(plaintext)
40+
except Exception as e:
41+
print(f"Error decrypting file {file_path}: {e}")
42+
43+
def process_files_in_folder(folder_path, key, encrypt=True):
44+
try:
45+
func = encrypt_file_inplace if encrypt else decrypt_file_inplace
46+
file_count = sum(len(files) for _, _, files in os.walk(folder_path))
47+
progress_bar = tqdm(total=file_count, desc="Processing", unit="file")
48+
49+
for root, dirs, files in os.walk(folder_path):
50+
for file in files:
51+
file_path = os.path.join(root, file)
52+
func(file_path, key)
53+
progress_bar.update(1) # Update progress bar
54+
55+
progress_bar.close()
56+
except Exception as e:
57+
print(f"Error processing files in folder {folder_path}: {e}")
58+
59+
def main():
60+
try:
61+
parser = argparse.ArgumentParser(description="Encrypt or decrypt files/folders, sudo -E enc --encrypt f/F file.txt/folder")
62+
group = parser.add_mutually_exclusive_group(required=True)
63+
group.add_argument("--encrypt", nargs=2, metavar=("f/F", "file/folder"), help="Encrypt a file or folder")
64+
group.add_argument("--decrypt", nargs=2, metavar=("f/F", "file/folder"), help="Decrypt an encrypted file or folder")
65+
args = parser.parse_args()
66+
key_is_set=False
67+
while key_is_set == False:
68+
key = getpass.getpass(prompt="Enter encryption/decryption key (16 chars): ").encode()
69+
if len(key) >= 16:
70+
key_is_set = True
71+
else:
72+
print(f"{red}Key must be at least 16 chars long!{no_color}")
73+
key_is_set = False
74+
if args.encrypt:
75+
method, target = args.encrypt
76+
if method not in ["f", "F"]:
77+
print("Invalid method. Use f or F.")
78+
return
79+
if method == "f":
80+
if not os.path.isfile(target):
81+
print("File not found.")
82+
return
83+
confirm_key = getpass.getpass(prompt="Confirm encryption key: ").encode()
84+
if key != confirm_key:
85+
print("Keys do not match. Aborting.")
86+
return
87+
encrypt_file_inplace(target, key)
88+
print("File", target, "encrypted.")
89+
elif method == "F":
90+
if not os.path.isdir(target):
91+
print("Folder not found.")
92+
return
93+
confirm_key = getpass.getpass(prompt="Confirm encryption key: ").encode()
94+
if key != confirm_key:
95+
print("Keys do not match. Aborting.")
96+
return
97+
process_files_in_folder(target, key)
98+
print("All files in", target, "encrypted.")
99+
elif args.decrypt:
100+
method, target = args.decrypt
101+
if method not in ["f", "F"]:
102+
print("Invalid method. Use f or F.")
103+
return
104+
if method == "f":
105+
if not os.path.isfile(target):
106+
print("File not found.")
107+
return
108+
decrypt_file_inplace(target, key)
109+
print("File", target, "decrypted.")
110+
elif method == "F":
111+
if not os.path.isdir(target):
112+
print("Folder not found.")
113+
return
114+
process_files_in_folder(target, key, encrypt=False)
115+
print("All encrypted files in", target, "decrypted.")
116+
except Exception as e:
117+
print(f"An error occurred: {e}")
118+
119+
if __name__ == "__main__":
120+
main()
121+

install_requirements

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
import subprocess,time
3+
modules=[
4+
"Crypto",
5+
"tqdm",
6+
]
7+
8+
for i in modules:
9+
subprocess.run(["pip3","install",i])
10+
time.sleep(1)
11+
print("All done.")

0 commit comments

Comments
 (0)