-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge07.py
39 lines (35 loc) · 1.34 KB
/
challenge07.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
#!/usr/bin/env python3
# AES in ECB mode
from base64 import b64decode
import os
from Crypto.Cipher import AES
from padding import pkcs7_unpad
# --------------------------------------------------------
# ---------------------- functions -----------------------
# --------------------------------------------------------
def aes_ecb_decrypt(ciphertext: bytes, key: bytes) -> bytes:
# create cipher object
cipher = AES.new(key, AES.MODE_ECB)
return cipher.decrypt(ciphertext)
def aes_ecb_encrypt(plaintext: bytes, key: bytes) -> bytes:
# create cipher object
cipher = AES.new(key, AES.MODE_ECB)
return cipher.encrypt(plaintext)
# --------------------------------------------------------
# ------------------- Problem Solution -------------------
# --------------------------------------------------------
# AES-128-ECB
def main():
key = b"YELLOW SUBMARINE"
# opening the file and reading the ciphertext
dirname = os.path.dirname(__file__)
path = os.path.join(dirname, 'challenge07-text.txt')
with open(path) as f:
ciphertext = b64decode(f.read())
plaintext = aes_ecb_decrypt(ciphertext, key)
# remove the padding after decrypting
plaintext = pkcs7_unpad(plaintext)
#ciphertext = aes_ecb_encrypt(plaintext.encode(), key).decode()
print(plaintext.decode())
if __name__ == "__main__":
main()