-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.py
68 lines (51 loc) · 2.14 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from Crypto.Cipher import AES, DES3
from Crypto.Random import get_random_bytes
from ascon import ascon_encrypt, ascon_decrypt
def AES_Encrypt(data: bytes, key):
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
return ciphertext + cipher.nonce + tag
def AES_Decrypt(ciphertext: bytes, key):
ciphertext, nonce, tag = ciphertext[:-32], ciphertext[-32:-16], ciphertext[-16:]
cipher = AES.new(key, AES.MODE_EAX, nonce)
data = cipher.decrypt_and_verify(ciphertext, tag)
return data
def DES3_Encrypt(data, key):
cipher = DES3.new(key, DES3.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
return ciphertext + cipher.nonce + tag
def DES3_Decrypt(ciphertext: bytes, key):
ciphertext, nonce, tag = ciphertext[:-24], ciphertext[-24:-8], ciphertext[-8:]
cipher = DES3.new(key, DES3.MODE_EAX, nonce)
data = cipher.decrypt_and_verify(ciphertext, tag)
return data
def ASCON_Encrypt(data, key):
nonce = get_random_bytes(16)
ciphertext = ascon_encrypt(key = key, nonce = nonce, associateddata = b'', plaintext = data)
return ciphertext + nonce
def ASCON_Decrypt(ciphertext, key):
ciphertext, nonce = ciphertext[:-16], ciphertext[-16:]
data = ascon_decrypt(key = key, nonce = nonce, associateddata = b'', ciphertext = ciphertext)
return data
def Decode_Encryption_Method(enc_method):
if enc_method == 1:
return AES_Encrypt, AES_Decrypt
if enc_method == 2:
return DES3_Encrypt, DES3_Decrypt
if enc_method == 3:
return ASCON_Encrypt, ASCON_Decrypt
if __name__ == '__main__':
data = b'Trying out a different super secret message that is much longer to confuse \
the program. asdhfhlkdsfdshflkdsfsahfldsaflkhsaflkhdskfhsahfkshfkjf'
key = get_random_bytes(16)
nonce = get_random_bytes(16)
encrypt_decrypt_pairs = ()
ciphertext = AES_Encrypt(data, key)
data = AES_Decrypt(ciphertext, key)
print(data)
ciphertext = DES3_Encrypt(data, key)
data = DES3_Decrypt(ciphertext, key)
print(data)
ciphertext = ASCON_Encrypt(data, key)
data = ASCON_Decrypt(ciphertext, key)
print(data)