-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
171 lines (150 loc) · 5.94 KB
/
Main.py
File metadata and controls
171 lines (150 loc) · 5.94 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
#!/usr/bin/env python3
import easygui
import argparse
import os
import sys
import zipfile
from tqdm import tqdm
import tarfile
import B64B64Rotcipher as bbr
import B64B64RotcipherDECODE as bbrd
def compress(tar_file, members):
"""
Adds files (`members`) to a tar_file and compresses it
"""
# open file for gzip compressed writing
tar = tarfile.open(tar_file, mode="w:gz")
# with progress bar
# set the progress bar
progress = tqdm(members)
for member in progress:
# add file/folder/link to the tar file (Compress)
tar.add(member)
# set the progress description of the progress bar
progress.set_description(f"Compressing {member}")
# close the file
tar.close()
def decompress(tar_file, path, members=None):
"""
Extracts `tar_file` and puts the `members` to `path`
If members is None, all members on `tar_file` will be extracted.
"""
tar = tarfile.open(tar_file, mode="r:gz")
if members is None:
members = tar.getmembers()
# with progress bar
# set the progress bar
progress = tqdm(members)
for member in progress:
tar.extract(member, path=path)
# set the progress description of the progress bar
progress.set_description(f"Extracting {member.name}")
# or use this
# tar.extractall(members=members, path=path)
# close the file
tar.close()
#def main():
# while True:
# c = easygui.buttonbox("Encrypt or Decrypt", choices = ["Encrypt", "Decrypt", "Quit"])
# if c == "Encrypt":
# easygui.msgbox(bbr.btwc(easygui.enterbox("Message"), easygui.passwordbox("Key")))
# if c == "Decrypt":
# easygui.msgbox(bbrd.decode(easygui.enterbox("Encrypted Message"), easygui.passwordbox("Key")))
# if c == "Quit":
# break
def main(msg, key, Encrypt, isfile):
iscompressed = False
isall = False
if Encrypt:
if not isfile:
return(bbr.btwc(msg, key).decode())
if isfile:
if msg.endswith("*"):
isall = True
l = list()
if msg == "*":
for r, d, f in os.walk("./"):
for file in f:
l.append(os.path.join(r, file))
elif msg != "*":
msg = msg.replace("*", '')
print(msg)
for r, d, f in os.walk(msg):
for file in f:
l.append(os.path.join(r, file))
for i in l:
with open(i, 'rb') as file:
fl = file.readlines()
flb = b''.join(fl)
with open(i.split("/")[len(i.split("/"))-1], 'wb') as file:
file.write(flb)
for i in range(len(l)):
l[i] = l[i].split("/")[len(l[i].split("/"))-1]
#print(l)
filename = str(input('Filename to save as: '))
compress(f"{filename}.tar.gz", l)
for i in l:
os.remove(i)
msg = f"{filename}.tar.gz"
with open(msg, 'rb') as file:
rl = file.readlines()
rs = b''.join(rl)
with open(msg+".bbr", 'wb') as file:
file.write(bbr.btwc(rs, key))
if isall:
os.remove(f"./{msg}")
#pass # DEBUG
return f"encrypted file is {msg}.bbr"
if not Encrypt:
if not isfile:
return(bbrd.decode(msg, key).decode())
if isfile:
if msg.endswith(".tar.gz.bbr"):
iscompressed = True
with open(msg, 'rb') as file:
rl = file.readlines()
rs = b''.join(rl)
with open(msg.replace(".bbr", ''), 'wb') as file:
file.write(bbrd.decode(rs, key))
if iscompressed:
decompress(msg.replace(".bbr", ''), "./"+msg.replace(".tar.gz.bbr",''))
os.remove(msg.replace(".bbr",''))
return f"Decrypted Folder is {msg.replace('.tar.gz.bbr','')}"
return f"decrypted file is {msg.replace('.bbr','')}"
if __name__ == '__main__':
my_parser = argparse.ArgumentParser(description="Encrypt or decrypt some text according to a key")
# add the arguments
requiredName = my_parser.add_argument_group("Required Named Arguements")
requiredName.add_argument("-m", "--message",
metavar='Message',
type=str,
action='store',
dest='msg',
required=True,
help='the message to encrypt (if there is spaces use quotes)')
my_parser.add_argument("-k", "--key",
metavar="Key",
type=str,
nargs="?",
action='store',
dest='key',
help='the key to encrypt the message (if there is spaces use quotes)')
my_parser.add_argument("-d", "--decrypt",
#metavar="Encrypt",
#type=str,
#nargs="?",
action='store_false',
dest='isencs',
help='Decrypt otherwise encrypt (if not called)')
my_parser.add_argument("-f", "--file",
action='store_true',
dest='isfile',
help='Is not file unless this argument is called, if this argument is called -m must be a filepath')
args = my_parser.parse_args()
msg = args.msg
key = args.key
isencs = args.isencs
isfile = args.isfile
if msg == None:
quit()
print(main(msg, key, isencs, isfile))