-
Notifications
You must be signed in to change notification settings - Fork 11
/
encryption.py
44 lines (32 loc) · 1.48 KB
/
encryption.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
from simplecrypt import encrypt, decrypt
import os, pickle
class EncryptLayer(object):
def encryptWrapper(self, password, message):
return encrypt(password, message)
def decryptWrapper(self, password, cipher):
return decrypt(password, cipher)
def encrypt_all_block_files(self, filelist, encryption_password):
list_of_encrypted_files = []
for file in filelist:
encrypted_filename = file + '.enc'
block_file_handle = open(file,'rb')
encrypted_file_handle = open(encrypted_filename, 'wb')
plaintext = block_file_handle.read().decode('utf8')
ciphertext = self.encryptWrapper(encryption_password, plaintext.encode('utf8'))
pickle.dump(ciphertext, encrypted_file_handle, protocol=pickle.HIGHEST_PROTOCOL)
print ('Encrypting file ' + encrypted_filename)
list_of_encrypted_files.append(encrypted_filename)
block_file_handle.close()
encrypted_file_handle.close()
return list_of_encrypted_files
def decrypt_chunks(self, list_of_encrypted_files, encryption_password):
for encrypted_file_name in list_of_encrypted_files:
read_from_enc_file = open(encrypted_file_name[0],'rb')
filename = os.path.splitext(encrypted_file_name[0])[0]
read_contents = pickle.load(read_from_enc_file)
print('Decrypting file ' + str(encrypted_file_name[0]))
data_to_write = self.decryptWrapper(encryption_password, read_contents)
block_file_handle = open(filename,'wb')
block_file_handle.write(data_to_write)
block_file_handle.close()
read_from_enc_file.close()