-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypter.py
101 lines (94 loc) · 3.01 KB
/
encrypter.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
import tools
import os
from cryptography.fernet import Fernet, MultiFernet
from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.ciphers.aead import AESCCM
def Algo1(data, key):
f = Fernet(key)
target_file = open("raw_data/store_in_me.enc","wb")
secret_data = f.encrypt(data)
target_file.write(secret_data)
target_file.close()
def Algo1_extended(filename, key1, key2):
f = MultiFernet([Fernet(key1),Fernet(key2)])
source_filename = 'files/' + filename
target_filename = 'encrypted/' + filename
file = open(source_filename,'rb')
target_file = open(target_filename,'wb')
raw = b""
for line in file:
raw = raw+line
secret_data = f.encrypt(raw)
target_file.write(secret_data)
file.close()
target_file.close()
def Algo2(filename, key, nonce):
aad = b"authenticated but unencrypted data"
chacha = ChaCha20Poly1305(key)
source_filename = 'files/' + filename
target_filename = 'encrypted/' + filename
file = open(source_filename,'rb')
target_file = open(target_filename,'wb')
raw = b""
for line in file:
raw = raw + line
secret_data = chacha.encrypt(nonce, raw, aad)
target_file.write(secret_data)
file.close()
target_file.close()
def Algo3(filename, key, nonce):
aad = b"authenticated but unencrypted data" #optional
aesgcm = AESGCM(key)
source_filename = 'files/' + filename
target_filename = 'encrypted/' + filename
file = open(source_filename,'rb')
target_file = open(target_filename,'wb')
raw = b""
for line in file:
raw = raw + line
secret_data = aesgcm.encrypt(nonce, raw, aad)
target_file.write(secret_data)
file.close()
target_file.close()
def Algo4(filename, key, nonce):
aad = b"authenticated but unencrypted data"
aesccm = AESCCM(key)
source_filename = 'files/' + filename
target_filename = 'encrypted/' + filename
file = open(source_filename,'rb')
target_file = open(target_filename,'wb')
raw = b""
for line in file:
raw = raw + line
secret_data = aesccm.encrypt(nonce, raw, aad)
target_file.write(secret_data)
file.close()
target_file.close()
def encrypter():
tools.empty_folder('key')
tools.empty_folder('encrypted')
key_1 = Fernet.generate_key()
key_1_1 = Fernet.generate_key()
key_1_2 = Fernet.generate_key()
key_2 = ChaCha20Poly1305.generate_key()
key_3 = AESGCM.generate_key(bit_length=128)
key_4 = AESCCM.generate_key(bit_length=128)
nonce13 = os.urandom(13)
nonce12 = os.urandom(12)
files = sorted(tools.list_dir('files'))
for index in range(0,len(files)):
if index%4 == 0:
Algo1_extended(files[index],key_1_1,key_1_2)
elif index%4 == 1:
Algo2(files[index],key_2,nonce12)
elif index%4 == 2:
Algo3(files[index],key_3,nonce12)
else:
Algo4(files[index],key_4,nonce13)
secret_information = (key_1_1)+b":::::"+(key_1_2)+b":::::"+(key_2)+b":::::"+(key_3)+b":::::"+(key_4)+b":::::"+(nonce12)+b":::::"+(nonce13)
Algo1(secret_information,key_1)
public_key = open("./key/My_key.pem","wb")
public_key.write(key_1)
public_key.close()
tools.empty_folder('files')