-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMAC.py
54 lines (43 loc) · 1.37 KB
/
MAC.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
import hashlib
charsDict = {i: chr(i+65) for i in range(0, 26)}
def encrypt(plainText, K):
cipherText = ''
for i in plainText:
if i.isnumeric():
cipherText += i
continue
letterIndex = ord(i.upper()) - 65
tempkey = (letterIndex + K) % 26
if i.islower():
cipherText += charsDict[tempkey].lower()
else:
cipherText += charsDict[tempkey]
return cipherText
def decrypt(cipherText, K):
plainText = ''
for i in cipherText:
if i.isnumeric():
plainText += i
continue
letterIndex = ord(i.upper()) - 65
tempkey = (letterIndex - K) % 26
if i.islower():
plainText += charsDict[tempkey].lower()
else:
plainText += charsDict[tempkey]
return plainText
def verify(M, K):
recv = decrypt(M, K)
msgR, shaRecv = recv.split("xx")
shaC = hashlib.sha256(msgR.encode()).hexdigest()
print("\nSHA256 Calculated :", shaC)
print("SHA256 Recieved :", shaRecv)
if shaRecv == shaC:
print(f"\nHence,\nMessage {msgR} Is Not Corrupted")
else:
print(f"\nHence,\nMessage {msgR} Is Corrupted")
msg = input("Enter Message :: ").replace(" ", "")
key = int(input("Enter The Key :: "))
sha = hashlib.sha256(msg.encode()).hexdigest()
send = encrypt(msg + "xx" + sha, key)
verify(send, key)