-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge7.py
47 lines (29 loc) · 950 Bytes
/
challenge7.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
from Crypto.Cipher import AES
from util import pkcs7
def ecb_enc(plaintext, key):
cipher = AES.new(key, AES.MODE_ECB)
ciphertext = cipher.encrypt(pkcs7(plaintext))
return ciphertext
def ecb_enc_raw(plaintext, key):
"""
encrypt without applying pkcs7
:param plaintext:
:param key:
:return:
"""
cipher = AES.new(key, AES.MODE_ECB)
ciphertext = cipher.encrypt(plaintext)
return ciphertext
def ecb_dec(ciphertext, key):
assert len(ciphertext) % 16 == 0
cipher = AES.new(key, AES.MODE_ECB)
plaintext = cipher.decrypt(ciphertext)
return plaintext
if __name__ == '__main__':
# testbytes = b'a' * 16
# key = b'0' * 16
# ciphertext = ecb_enc(testbytes, key)
# plaintext = ecb_dec(ciphertext, key)
# assert testbytes == plaintext
# above test is broken, need to unpad when decrypting
assert ecb_dec(ecb_enc_raw(b'0'*16, b'0'*16), b'0'*16) == b'0'*16