-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.py
75 lines (64 loc) · 2.39 KB
/
decrypt.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
import os
import subprocess
import secure_delete
import shutil
import getpass
from dotenv import load_dotenv
load_dotenv()
print("---------- DECRYPTION -----------")
target_file = ""
if len(sys.argv) != 2:
target_file = input("Please enter the file or folder name: ")
else:
target_file = sys.argv[1]
# Check if it exists
if os.path.exists(target_file) == False:
print(f"The file or folder '{target_file}' does not exist.")
exit()
password = getpass.getpass("Enter your password: ")
# Construct the command
seven_zip_path = os.getenv("SEVEN_ZIP_PATH")
command = f"{seven_zip_path} x -p{password} {target_file}"
# Execute the command
try:
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("\n\n 7-zip executed successfully!\n\n")
#print(" 7-zip Output:", result.stdout + "\n")
except subprocess.CalledProcessError as e:
#print("An error occurred:", e.stderr)
print("Turn on Erorr for the 7zip command to see the issue")
print("\n\nExiting Program\n\n")
exit()
# Function to remove the .7z extension
def remove_extension(filename):
# Check if the filename ends with .7z
if filename.endswith('.7z'):
# Remove the extension
new_filename = filename[:-3] # Removes the last 3 characters (.7z)
return new_filename
return filename # Return the original filename if it doesn't have the .7z extension
# remove the .7z extension
decrypted_file_name = remove_extension(target_file)
# Derive the destination filename
def remove_encrypted(filename):
# Split the filename into base and extension
base, ext = os.path.splitext(filename)
# Remove "ENCRYPTED" from the base name
new_base = base.replace("_ENCRYPTED", "")
# Create the new filename
new_filename = f"{new_base}{ext}"
# Check if the new filename already exists
counter = 1
while os.path.exists(new_filename):
# If it exists, increment the counter and create a new filename
new_filename = f"{new_base}_{counter}{ext}"
counter += 1
return new_filename
# remove the word ENCRYPTED from the output file
destination = remove_encrypted(decrypted_file_name)
# create a normal output filename
#os.rename(decrypted_file_name, destination)
# Delete the old {name}_ENCRYPTED.7z file
secure_delete.secure_delete_path(target_file)
print("---------------------------------")