-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge7.py
More file actions
45 lines (32 loc) · 1.1 KB
/
Copy pathchallenge7.py
File metadata and controls
45 lines (32 loc) · 1.1 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
40
41
42
43
44
import challenge6
import base64
from Crypto.Cipher import AES
def aes_encrypt_single_block(key, block):
if len(block) != AES.block_size:
raise ValueError('Invalid block size.')
cipher = AES.new(key, AES.MODE_ECB)
return cipher.encrypt(block)
def aes_decrypt_single_block(key, block):
if len(block) != AES.block_size:
raise ValueError('Invalid block size.')
cipher = AES.new(key, AES.MODE_ECB)
return cipher.decrypt(block)
def aes_ecb_encrypt(pt, key):
if len(pt) % AES.block_size != 0:
raise ValueError('Invalid message length')
blocks = challenge6.get_blocks(pt, AES.block_size)
ct = b''
for block in blocks:
ct += aes_encrypt_single_block(key, block)
return ct
def aes_ecb_decrypt(ct, key):
blocks = challenge6.get_blocks(ct, AES.block_size)
pt = b''
for block in blocks:
pt += aes_decrypt_single_block(key, block)
return pt
if __name__ == '__main__':
with open('7.txt') as f:
ct = base64.b64decode(f.read())
key = b'YELLOW SUBMARINE'
print(aes_ecb_decrypt(ct, key))