-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.py
More file actions
161 lines (139 loc) · 5.48 KB
/
Copy pathdecrypt.py
File metadata and controls
161 lines (139 loc) · 5.48 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
try:
import os
import sys
from fernet import Fernet
#check file open by app
if os.path.exists("process/run_by_app_01") :
os.remove("process/run_by_app_01")
else:
sys.exit()
#read files for decryption
file = open("process/decrypt_select_file","r" ,encoding='utf8')
files_for_encrypt = file.read().split("<><><><><>")[:-1]
file.close()
#read no. data
data_count_total = 0
data_count_list = []
for file in files_for_encrypt:
read_file = open(file ,"rb")
data_count = 0
while True:
if read_file.readline() == b"":
f = open("process/file_read_complete","w")
f.close()
while os.path.exists("process/file_read_complete"):
#check program is closed ?
if os.path.exists("process/exit"):
sys.exit()
data_count_list.append(data_count)
read_file.close()
break
data_count_total += 1
data_count += 1
#check program is closed ?
if os.path.exists("process/exit"):
sys.exit()
#zero line files check
data_count_list_index = 0
while data_count_list_index < len(data_count_list) :
#check program is closed ?
if os.path.exists("process/exit"):
sys.exit()
if data_count_list[data_count_list_index] == 0:
data_count_list[data_count_list_index] = 1
data_count_list_index += 1
#get key
try:
key_file = open("process/key","rb")
key = key_file.readline()
except:
sys.exit()
key = Fernet.generate_key()
key_file = open("process/key","wb")
key_file.write(key)
key_file.close()
while 1:
if os.path.exists("process/encrypt_decrypt_start"):
break
#create file name
def genarate_name(file_name):
file_name = file_name.replace("_ec","")
file_type = file_name.split(".")[-1]
file_name = file_name.replace("."+file_type,"")
genarated_name = file_name + "_dc." + file_type
return genarated_name
file_index = -1
for file in files_for_encrypt:
#reading and createing files
encrypted_file = open(file,"rb")
decrypted_file = open(genarate_name(file),"wb")
lines_decrypted = 0
file_index += 1
decrypted_target = 1
failed_to_decrypt = False
while True:
if int((lines_decrypted/data_count_list[file_index])*100) == decrypted_target :
try:
progression_file = open("process/progression" ,"w")
progression_file.write(str(decrypted_target))
progression_file.close()
except:
pass
decrypted_target += 1
#find errors in percentage
if int((lines_decrypted/data_count_list[file_index])*100) > 100 :
try:
error_file = open("system error/decrypt pro.txt","a")
except:
error_file = open("system error/decrypt pro.txt","w")
error_file.write(str(files_for_encrypt[file_index]) + "\n")
error_file.write(str(lines_decrypted) + " | " + str(data_count_list[file_index]) + "\n")
error_file.write(str(int((lines_decrypted/data_count_list[file_index])*100)) +"%" + "\n")
error_file.write("--------------------------------------------------------" + "\n\n")
#read encrypted file
encrypted_content = encrypted_file.readline()
#check end of file
if encrypted_content == b"" or failed_to_decrypt == True:
#one file decrypt completed
progression_file = open("process/progression" ,"w")
progression_file.write("100")
progression_file.close()
file = open("process/file_completed","w")
file.close()
while os.path.exists("process/file_completed"):
#check program is closed ?
if os.path.exists("process/exit"):
sys.exit()
break
else:
#remove added content
encrypted_content = encrypted_content.replace(b"|~~~<>\n",b"")
try:
#decryption
if lines_decrypted < 10000:
decrypted_content = Fernet(key).decrypt(encrypted_content)
else:
decrypted_content = encrypted_content.replace(b"`>~`",b" ").replace(b"`[`>`",b"")
except:
failed_to_decrypt = True
#check decryption fail
if failed_to_decrypt == False:
#write decrypted content on new file
decrypted_file.write(decrypted_content)
#count encrypted lines
lines_decrypted += 1
#check program is closed ?
if os.path.exists("process/exit"):
sys.exit()
except Exception as error :
try :
os.mkdir("system error")
except:
pass
try:
f = open("system error/decrypt log.txt" ,"a")
except:
f = open("system error/decrypt log.txt" ,"w")
f.write(str(error)+"\n")
f.write("-------------------------------------------------------------" + "\n\n")
f.close()