forked from sahilchadha1991/cryptoExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbcRandomIV.py
36 lines (32 loc) · 1011 Bytes
/
cbcRandomIV.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
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
key = os.urandom(32)
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
ct = encryptor.update(b"a secret messagea secret message")
print("IV1 used is ", iv)
print(ct)
decryptor = cipher.decryptor()
x = decryptor.update(ct)
y = ct[0:16]
z = ct[16:32]
print("\n\n\n\n")
print("1st block ", y, "\t", x[0:16])
print("2nd block ", z, "\t", x[16:32])
print("\n\n\n")
iv2 = os.urandom(16)
print("IV2 used is ", iv2)
cipher2 = Cipher(algorithms.AES(key), modes.CBC(iv2), backend=backend)
encryptor = cipher2.encryptor()
ct2 = encryptor.update(b"a secret messagea secret message")
print(ct2)
decryptor = cipher2.decryptor()
x = decryptor.update(ct2)
y = ct2[0:16]
z = ct2[16:32]
print("\n\n\n\n")
print("1st block ", y, "\t", x[0:16])
print("2nd block ", z, "\t", x[16:32])