-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.py
More file actions
49 lines (39 loc) · 1.33 KB
/
Copy pathencrypt.py
File metadata and controls
49 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
import sys
import os
from cryptography.fernet import Fernet
# GNOENCRYPT
key1 = b"VZ0dguatW7_4UnQPTn1r8rxC8aznZ0w-ERL4CBwKAI4="
fernet = Fernet(key1)
def should_skip(file_path):
try:
with open(file_path, "r", errors="ignore") as f:
for line in f:
if "GNOENCRYPT" in line:
return True
except Exception:
return False
return False
files_to_encrypt = sys.argv[1:]
if not files_to_encrypt:
print("ERROR: No files specified.", file=sys.stderr)
sys.exit(1)
for file_to_encrypt in files_to_encrypt:
if should_skip(file_to_encrypt):
print(f"SKIPPED:{file_to_encrypt}")
continue
try:
with open(file_to_encrypt, "rb") as f:
data = f.read()
encrypted_data = fernet.encrypt(data)
encrypted_file = f"{file_to_encrypt}.enc"
with open(encrypted_file, "wb") as f:
f.write(encrypted_data)
if os.path.exists(encrypted_file) and os.path.getsize(encrypted_file) > 0:
os.remove(file_to_encrypt)
print(f"ENCRYPTED:{file_to_encrypt}->{encrypted_file}")
else:
print(f"FAILED:{file_to_encrypt}:Encrypted file not created!", file=sys.stderr)
except Exception as e:
print(f"FAILED:{file_to_encrypt}:{e}", file=sys.stderr)
sys.exit(0)