1import cryptolib
2
3key = b"16bytessecretkey"
4cipher = cryptolib.aes(key, 1)
5
6# must be a multiple of 16, requires manual padding
7plain = b"0123456789abcdef"
8encrypted = cipher.encrypt(plain)
9print(encrypted)
10
11# The same cipher must not be reused;
12# a new one must be created each time.
13cipher2 = cryptolib.aes(key, 1)
14decrypted = cipher2.decrypt(encrypted)
15print(decrypted)
16
17
18def pad16(data):
19 n = 16 - (len(data) % 16)
20 return data + bytes([n]) * n
21
22
23def unpad16(data):
24 return data[: -data[-1]]
25
26
27msg = b"hello"
28msg_padded = pad16(msg)
29
30c = cryptolib.aes(key, 1)
31ct = c.encrypt(msg_padded)
32
33c2 = cryptolib.aes(key, 1)
34pt = unpad16(c2.decrypt(ct))
35print(pt)