1
2
3
4 """Pure-Python AES implementation."""
5
6 from .cryptomath import *
7
8 from .aes import *
9 from .rijndael import rijndael
10
11 -def new(key, mode, IV):
13
19
21 AES.encrypt(self, plaintext)
22
23 plaintextBytes = plaintext[:]
24 chainBytes = self.IV[:]
25
26
27 for x in range(len(plaintextBytes)//16):
28
29
30 blockBytes = plaintextBytes[x*16 : (x*16)+16]
31 for y in range(16):
32 blockBytes[y] ^= chainBytes[y]
33
34
35 encryptedBytes = self.rijndael.encrypt(blockBytes)
36
37
38 for y in range(16):
39 plaintextBytes[(x*16)+y] = encryptedBytes[y]
40
41
42 chainBytes = encryptedBytes
43
44 self.IV = chainBytes[:]
45 return plaintextBytes
46
48 AES.decrypt(self, ciphertext)
49
50 ciphertextBytes = ciphertext[:]
51 chainBytes = self.IV[:]
52
53
54 for x in range(len(ciphertextBytes)//16):
55
56
57 blockBytes = ciphertextBytes[x*16 : (x*16)+16]
58 decryptedBytes = self.rijndael.decrypt(blockBytes)
59
60
61 for y in range(16):
62 decryptedBytes[y] ^= chainBytes[y]
63 ciphertextBytes[(x*16)+y] = decryptedBytes[y]
64
65
66 chainBytes = blockBytes
67
68 self.IV = chainBytes[:]
69 return ciphertextBytes
70