-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cbc_mode.py
49 lines (41 loc) · 1.76 KB
/
test_cbc_mode.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
from cbc_mode import *
def test_encrypt_one_block():
plaintext_block = b'yellow submarine'
key = b'YELLOW SUBMARINE'
salt = bytes([0]*16)
ciphertext = encrypt_one_block(plaintext_block, salt, key)
assert len(ciphertext) == 16
def test_encrypt_decrypt_one_block():
plaintext_block = b'yellow submarine'
key = b'YELLOW SUBMARINE'
salt = bytes([0]*16)
ciphertext_block = encrypt_one_block(plaintext_block, salt, key)
plaintext_decrypted = decrypt_one_block(ciphertext_block, salt, key)
assert plaintext_decrypted == plaintext_block
def test_pad():
plaintext = b'yellow submari'
padded_plaintext = pad(plaintext, 16)
assert padded_plaintext == b'yellow submari\02\02'
plaintext = b'YELLOW SUBMARINEyellow submar'
padded_plaintext = pad(plaintext, 16)
assert padded_plaintext == b'YELLOW SUBMARINEyellow submar\03\03\03'
def test_unpad():
plaintext = b'here we go a wassailing'
padded_plaintext = pad(plaintext, 16)
unpadded_plaintext = unpad(padded_plaintext)
assert plaintext == unpadded_plaintext
def test_encrypt_cbc():
plaintext = b'YELLOW SUBMARINEyellow submar'
initialization_vector = bytes([ord('0')]*16)
key = b'YELLOW SUBMARINE'
ciphertext = encrypt_cbc(plaintext, initialization_vector, key)
assert len(ciphertext) == 32
def test_encrypt_decrypt_cbc():
plaintext = b'a fine day in the woods'
initialization_vector = bytes([ord('0')]*16)
key = b'purple taxi cabs'
ciphertext = encrypt_cbc(plaintext, initialization_vector, key)
proposed_plaintext = decrypt_cbc(ciphertext, initialization_vector, key)
print(proposed_plaintext)
print(len(proposed_plaintext))
assert plaintext == proposed_plaintext