-
Notifications
You must be signed in to change notification settings - Fork 2
/
SymmetricEncryption.py
113 lines (93 loc) · 2.87 KB
/
SymmetricEncryption.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
import pyinputplus as pyip
import string
import pyfiglet
import stdiomask
import hashlib
import math
import os
from Crypto.Cipher import AES
from Crypto.Cipher import Salsa20
from Crypto.Random import get_random_bytes
class SymmetricEncryption:
'''
1- encrypt my message
2- decrypt my message
'''
@classmethod
def encrypt(cls, method='AES'):
plaintext = pyip.inputStr('enter data for encryption : ')
plaintext = str.encode(plaintext)
key = stdiomask.getpass()
key = str.encode(key)
if(method=='AES'):
if(len(key)<16):
key = key + str.encode((16-len(key))*'a')
elif(len(key)>16):
key = key[:16]
cipher = AES.new(key, AES.MODE_EAX)
nonce = cipher.nonce
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
print('nonce: \n'+ str(nonce), '\nciphertext: \n'+ str(ciphertext), '\ntag: \n'+ str(tag))
return (nonce,ciphertext,tag)
elif(method=='Salsa20'):
if(len(key)<32):
key = key + str.encode((32-len(key))*'a')
elif(len(key)>32):
key = key[:32]
cipher = Salsa20.new(key=key)
ciphertext = cipher.nonce + cipher.encrypt(plaintext)
print('ciphertext: \n'+ str(ciphertext))
return ciphertext
@classmethod
def decrypt(cls, nonce=None, ciphertext=None, tag=None, method='AES'):
key = stdiomask.getpass()
key = str.encode(key)
if(method=='AES'):
if(len(key)<16):
key = key + str.encode((16-len(key))*'a')
elif(len(key)>16):
key = key[:16]
cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
plaintext = cipher.decrypt(ciphertext)
try:
cipher.verify(tag)
print("\nThe message is authentic:", plaintext)
except ValueError:
print("Key incorrect or message corrupted")
elif(method=='Salsa20'):
if(len(key)<32):
key = key + str.encode((32-len(key))*'a')
elif(len(key)>32):
key = key[:32]
msg_nonce = ciphertext[:8]
ciphertext = ciphertext[8:]
try:
cipher = Salsa20.new(key=key, nonce=msg_nonce)
plaintext = cipher.decrypt(ciphertext)
print("The message is authentic:", plaintext)
except ValueError:
print("Key incorrect or message corrupted")
@classmethod
def menu(cls):
ascii_banner = pyfiglet.figlet_format("SYMMETRIC ENCRYPTION")
print(ascii_banner)
while(True):
print('\n')
choice = pyip.inputMenu(['encryption', 'quit'])
if(choice=='encryption'):
method = pyip.inputMenu(['AES', 'Salsa20'])
print(method)
if(method=='AES'):
nonce,ciphertext,tag = SymmetricEncryption.encrypt(method=method)
else:
ciphertext = SymmetricEncryption.encrypt(method=method)
print('\nFor decryption: ')
decrypt = pyip.inputMenu(['yes','no'])
if(decrypt=='no'):
continue
elif(method=='AES'):
SymmetricEncryption.decrypt(nonce,ciphertext,tag,method)
elif(method=='Salsa20'):
SymmetricEncryption.decrypt(nonce=None,ciphertext=ciphertext,tag=None,method=method)
else:
return