-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
161 lines (141 loc) · 7.39 KB
/
app.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
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
"""
Copyright 2019 (c) GlibGlob Ltd.
Author: Laurence Psychic
Email: [email protected]
Initiate the VesperCrypt System, provide a UI to take a file and encrypt the
file using the provided AES-RSA encryption key in block sizes equal to
512 bytes of information per encoding. An SQL database is used to maintain the
validity of the data stored on disk and retain concurency in data.
"""
import click
from math import ceil
from Crypto.Cipher import AES
from hashlib import md5
from diskio import read_location, write_location, detail_location, get_path
from storage import CryptoStore, CryptoFile, session
from password import Password
AES_IV456_AUTHENTICATION = '84d1a35654ab9af8'
DEFAULT_OUTPUT_DIRECTORY = './output'
FILE_BYTE_LENGTH = 1024
IGNORE_LIST = [
'.DS_Store',
]
def get_encryptable_password():
password_store = str(Password())
while len(password_store.encode()) % 16:
password_store = get_encryptable_password()
return password_store
def get_available_filename():
filename = str(Password(alphabet=['Filename Safe']))
path = get_path('{}/{}'.format(DEFAULT_OUTPUT_DIRECTORY, filename))
while path.exists() or len(path.name.encode()) % 16:
path = get_available_filename()
return path
def encrypt_location(location, cypher, password_key, password_iv):
skip_file = [s for s in IGNORE_LIST if s in location.name]
if skip_file:
return None
location = get_path(location)
byte_length = FILE_BYTE_LENGTH
test_copy = get_encryptable_password()
file = CryptoFile(
# public_key=password_key,
private_key=password_iv,
test_copy=test_copy,
test_encrypted=cypher.encrypt(test_copy).hex(),
filename=location.name,
is_dir=location.is_dir(),
)
session.add(file)
session.commit()
files = []
if location.is_file():
read = read_location(location).hex()
length = len(read)
i = 0
iv456 = md5(str(Password()).encode()).hexdigest()[:16]
for i in range(ceil(length / byte_length)):
password_store = get_encryptable_password()
password = md5(password_store.encode()).hexdigest()
file_location = get_available_filename()
text_value = read[i * byte_length: (i + 1) * byte_length]
text_length = len(text_value)
if len(text_value) < byte_length:
text_value += ''.join(['0'] * (byte_length - len(text_value)))
aes_object = AES.new(password, AES.MODE_CBC, iv456)
write_location(file_location, aes_object.encrypt(text_value).hex())
file_store = CryptoStore(
public_key=cypher.encrypt(password_store).hex(),
private_key=cypher.encrypt(iv456).hex(),
filename=cypher.encrypt(file_location.name).hex(),
filesize=text_length,
cryptofile=file
)
files.append(file_store)
session.add(file_store)
if files:
session.commit()
return file
def dencrypt_hash(data, cypher):
pass
def encrypt_detailed_location(details, aes_pass, password_key, password_iv):
detail_files = []
for key in details.keys():
path = details[key]
if type(path) is dict:
encrypt_detailed_location(path)
else:
file = encrypt_location(path, aes_pass, password_key, password_iv)
if file and path.is_file():
detail_files.append(file)
# elif key == '.':
# current_dir = file
# for file in detail_files:
# file.directory = current_dir
session.commit()
def main():
"""
Main method for application.
"""
app_name = '- VesperCrypt System -'
print(app_name)
password = click.prompt('Password', hide_input=True)
password_key = md5(password.encode()).hexdigest()
password_iv = md5(AES_IV456_AUTHENTICATION.encode()).hexdigest()[:16]
aes_pass = AES.new(password_key, AES.MODE_CBC, password_iv)
crypt_type = click.prompt('Store / Retrieve? [s/r]')
if crypt_type == 's':
location_details = detail_location('./input/')
encrypt_detailed_location(
location_details, aes_pass, password_key, password_iv)
elif crypt_type == 'r':
output = get_path('./restore')
output.mkdir(exist_ok=True)
all_files = session.query(CryptoFile).all()
for file in all_files:
tested = aes_pass.decrypt(file.test_encrypted)
"""
# Matches of decrypting do not pass
# Encryption of same copy does not match stored
# Test password 'lolliepop'
(Pdb) aes_pass.dec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\xaf\x85\x9b:\xb7\xac\x14\x08\xe6\xfd'
(Pdb) aes_pass.encrypt(file.test_copy)
b'\rntH\xf3\xaa(T6\x84\xc8\xbb=\xb9\xae*\xde6\xa2\xfb2\xe37m\xc6\x0f\x1d\x02\xdb\xbc\xa3\x1c\xbb\x9b\x1d\xe2S\x81\x04\x94\xaa=\xde\x85\x82\xea\xfbN\x1f\x866U\x06Y`\xafu\xa5\xc2\xca\xaf\xc6\x11)PL\x98\xe7\x9dw\x81\xc3\x1d\xc27\xd7S\x8c\xb3\x9d\x82\xa5\xaa^e(\xca\xe2+\xd4\x07k\xd0\x82\xd6w\x1e:\x03T\xdf\xd3\xbf\xeb>3\xeb\xfa\xbb\x97\x90\xa5\xba\xf6\x08\x01>\x91\x18\x10\xcbO\x81Pi{\xe0\xcaCf\xd9 F\x129M/\x9c\x9aE\x050\xe9\xcb\x82xR0\xc5\x1eW\xc83\xe9\x1a\xf2\rw\xe8\xf7k5\xd6\x8c\x01Q\xa1\xd9\xb0\x89@\xe4\x940\r\xdd+\xef\x7f\xb6\x04ixn\xabxSE\xd9\x11\xbf\xfa'
(Pdb) file.test_encrypted
'7ba7b9460e027c305e5256020d7e52ef3658e2127b762f65fe43df357902fa36fe31a25d37415be747536efb50e403e37903e1fe181a7a1982e663f60ab0a4c3bfbe9a884f251dea9aa7309b2418282601c4809e23c43e323b6e5ec481be5b3abf74e4711585c60afe5c229018e836e91f8f89a84c3dd8f60d54e361210e086faab66857704cc686c0d247ce0f017bea3f16fe61f4cc164cac6fb1eac04ecaedb8d5d1bbbefd8df3a3bdb3e21f36e7236e1d4eb1f358d21afbd6d8e5647060f0'
(Pdb)
"""
import pdb; pdb.set_trace()
if tested != file.test_copy:
continue
path = get_path(
'{}/{}'.format(DEFAULT_OUTPUT_DIRECTORY, file.filename))
output_path = get_path('{}/{}'.format(output, file.filename))
add_output = output_path.open(mode='a')
add_output.write()
write_location(output_path, read_location(path).hex())
# session.query(CryptoStore).filter_by(cryptofile_id=4).all()
import pdb; pdb.set_trace()
if __name__ == '__main__':
main()