-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_3.4.1_DES_CBC.py
More file actions
39 lines (22 loc) · 1.12 KB
/
Copy path_3.4.1_DES_CBC.py
File metadata and controls
39 lines (22 loc) · 1.12 KB
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
from Crypto.Cipher import DES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
def DES_CBC_encrypt(plaintext, DES_key, initialization_vector):
DES_CBC_cipher = DES.new(DES_key, DES.MODE_CBC, iv=initialization_vector)
ciphertext = DES_CBC_cipher.encrypt(pad(plaintext, DES.block_size))
return ciphertext
def DES_CBC_decrypt(ciphertext, DES_key, initialization_vector):
DES_CBC_cipher = DES.new(DES_key, DES.MODE_CBC, iv=initialization_vector)
decrypted_plaintext = unpad(DES_CBC_cipher.decrypt(ciphertext),DES.block_size)
return decrypted_plaintext
plaintext = input("Enter plaintext: ").encode("utf-8")
DES_key = get_random_bytes(8)
initialization_vector = get_random_bytes(8)
anas = str(DES_key)
ciphertext = DES_CBC_encrypt(plaintext, DES_key, initialization_vector)
decrypted_plaintext = DES_CBC_decrypt(ciphertext,DES_key, initialization_vector)
print("Encode PT:", plaintext)
print("Secret_Key: ", str(DES_key.hex()))
print("IV : ", initialization_vector.hex())
print("Ciphertext: ", ciphertext.hex())
print("Decrypted : ", decrypted_plaintext.decode())