/
c7.py
57 lines (28 loc) · 1022 Bytes
/
c7.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
50
51
52
53
54
55
56
57
from Crypto.Cipher import AES
from c1 import base642bytes
from c9 import pad_pkcs7, unpad_pkcs7, is_padded_pkcs7
# https://www.dlitz.net/software/pycrypto/api/current/
def aes_encrypt_ecb(text, key, pad = False):
# if you want to pad, pass pad = True and appropriate blockSize
cipher = AES.new(key, AES.MODE_ECB)
if pad:
text = pad_pkcs7(text, AES.block_size)
c = cipher.encrypt(text)
return c
def aes_decrypt_ecb(text, key, padded = False):
# padded denotes whether the message was padded or not when encrypting
cipher = AES.new(key, AES.MODE_ECB)
msg = cipher.decrypt(text)
if padded:
msg = unpad_pkcs7(msg)
return msg
def main():
key = b'YELLOW SUBMARINE'
with open('c7.txt') as f:
text = f.read().replace('\n', '')
text = base642bytes(bytes(text, 'utf-8'))
msg = aes_decrypt_ecb(text, key)
assert(aes_decrypt_ecb(aes_encrypt_ecb(msg, key), key) == msg)
print(msg)
if __name__ == '__main__':
main()