-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.py
More file actions
192 lines (184 loc) · 6 KB
/
cli.py
File metadata and controls
192 lines (184 loc) · 6 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- coding:utf-8 -*-
import os
from crypter import Fcrypter, ext
path = os.getcwd()
extend = os.sep
merge = path + extend
def mystrip(_path, _merge=merge):
result = ""
temp = 0
for x in _path:
try:
if not x == _merge[temp]:
result += x
except IndexError:
result += x
temp += 1
return result
def mysort(itemlist : list):
folders = []
files = []
result = []
for x in itemlist:
item = x['item']
name = item[::-1].split(extend)[-1]+extend if item.endswith(extend) else item.split(extend)[-1]
itemtype = x['type']
if itemtype == "FILE":
files.append(
{
"path": item,
"name": name,
"type": itemtype
}
)
else:
folders.append(
{
"path": item,
"name": name,
"type": itemtype
}
)
for folder in folders:
result.append(folder)
for file in files:
result.append(file)
return result
def ls(path=merge, fourext=False):
items = []
listdir = os.listdir(path)
listdir.sort()
if fourext:
for root, dirs, file in os.walk(path):
for f in file:
if f.endswith(ext):
items.append(
{
"item": os.path.join(root, f), "type": "FILE"
}
)
else:
for item in listdir:
if os.path.isfile(os.path.join(path, item)):
items.append({"item": os.path.join(path, item), "type": "FILE"})
else:
items.append({"item": os.path.join(path, item), "type": "FOLDER"})
temp = 1
pretty = "[ {} ] {} | {}"
mysorted = mysort(itemlist=items)
if not fourext:
for x in mysorted:
objtype = x["type"]
objname = x["name"]
print(pretty.format(temp, objname, objtype))
temp += 1
else:
for x in mysorted:
objname = mystrip(x['path'])
objtype = x['type']
print(pretty.format(temp, objname, objtype))
temp += 1
return mysorted
def parse(string : str):
result = []
if "," in string:
strlist = string.split(",")
for num in strlist:
result.append(num.strip())
else:
strlist = string.split()
for num in strlist:
result.append(num.strip())
return result
def main():
key = input("[?] Key (max length 32 char): ")
if len(key) > 32:
print("Please put key sorter than 32 char")
return main()
crypter = Fcrypter(key=key)
ec = input("Encrypt/Decrypt?: ").lower()
cryptmode = int(input("\n[ 0 ] AES-256-CBC\n[ 1 ] MultiFernet\n\nCrypt mode: "))
if cryptmode == 0:
aes = True
elif cryptmode == 1:
aes = False
else:
print("Please select 0 or 1")
return main()
spesific_path = input("Path (if press enter, path will be current work directory): ")
print(" ")
print("[ 0 ] All")
if not spesific_path == "":
if ec.startswith("d"):
items = ls(path=spesific_path, fourext=True)
else:
items = ls(path=spesific_path)
else:
if ec.startswith("d"):
items = ls(fourext=True)
else:
items = ls()
print(" ")
options_ask = input("[?] Select file(s)/folder(s) you want crypt: ")
if options_ask.replace(" ", "") == "":
print("Please select number.")
return main()
print(" ")
options = parse(options_ask)
if options[0] != 0 and len(options) > 1:
for opt in options:
item = items[int(opt) - 1]
print(item['name'], "|", item['type'])
else:
for item in items:
print(item['name'], "|", item['type'])
print(" ")
if ec.startswith("e"):
print("Will be encrypted.")
else:
print("Will be decrypted.")
yesno = input("Are you sure?: ").lower()
if yesno.startswith("y"):
if options[0] != 0 and len(options) > 1:
for option in options:
cryptable = items[int(option) - 1]
cpath = cryptable['path']
ctype = cryptable['type']
cname = cryptable['name']
if ec.startswith("e"):
if ctype == 'FOLDER':
crypter.encryptFolder(cpath, aes=aes)
print(f"'{cname}' folder is crypted!")
else:
crypter.encryptFile(cpath, aes=aes)
print(f"'{cname}' file is crypted!")
elif ec.startswith("d"):
if ctype == 'FOLDER':
crypter.decryptFolder(cpath, aes=aes)
print(f"'{cname}' folder is decrypted!")
else:
crypter.decryptFile(cpath, aes=aes)
print(f"'{cname}' file is decrypted!")
else:
for it in items:
cpath = it['path']
ctype = it['type']
cname = it['name']
if ec.startswith("e"):
if ctype == 'FOLDER':
crypter.encryptFolder(cpath, aes=aes)
print(f"'{cname}' folder is crypted!")
else:
crypter.encryptFile(cpath, aes=aes)
print(f"'{cname}' file is crypted!")
elif ec.startswith("d"):
if ctype == 'FOLDER':
crypter.decryptFolder(cpath, aes=aes)
print(f"'{cname}' folder is decrypted!")
else:
crypter.decryptFile(cpath, aes=aes)
print(f"'{cname}' file is decrypted!")
else:
print("Process is canceled!")
if __name__ == '__main__':
main()