-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdes.py
More file actions
29 lines (24 loc) · 793 Bytes
/
Copy pathdes.py
File metadata and controls
29 lines (24 loc) · 793 Bytes
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
from Crypto import Cipher
from Crypto.Cipher import DES
from secrets import token_bytes
key= token_bytes(8)
def encrypt (msg) :
Cipher=DES.new(key,DES.MODE_EAX)
nonce=Cipher.nonce
chiphertext, tag =Cipher.encrypt_and_digest(msg.encode('ascii'))
return nonce,chiphertext,tag
def decrypt(nonce,ciphertext,tag):
Cipher=DES.new(key,DES.MODE_EAX,nonce=nonce)
plaintext=Cipher.decrypt(ciphertext)
try:
Cipher.verify(tag)
return plaintext.decode('ascii')
except:
return False
nonce,ciphertext,tag=encrypt(input("Enter Plaintext: "))
print("Cipher text:", ciphertext)
plaintext=decrypt(nonce,ciphertext,tag)
if not plaintext:
print("Message is corrupted")
else:
print("Decrypted PlainText: ",plaintext)