1 | 1 | | from utils.Singleton import Singleton |
2 | 2 | | from Crypto.Cipher import AES |
| 3 | + | from Crypto.Util.Padding import pad |
| 4 | + | from Crypto.Util.Padding import unpad |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | class ChannelAES(Singleton): |
| skipped 6 lines |
12 | 14 | | self.IV = self.hashed_password[0:self.BS] |
13 | 15 | | |
14 | 16 | | def encrypt(self, plain_data): |
15 | | - | pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS) |
16 | | - | plain_data_pad = pad(plain_data) |
| 17 | + | plain_data_pad = pad(plain_data, self.BS) |
17 | 18 | | aes = AES.new(self.hashed_password, AES.MODE_CBC, self.IV) |
18 | 19 | | encrypted_data = aes.encrypt(plain_data_pad) |
19 | 20 | | return encrypted_data |
20 | 21 | | |
21 | 22 | | def decrypt(self, encrypted_data): |
22 | 23 | | aes = AES.new(self.hashed_password, AES.MODE_CBC, self.IV) |
23 | | - | unpad = lambda s: s[:-ord(s[len(s) - 1:])] |
24 | 24 | | decrypted_data = aes.decrypt(encrypted_data) |
25 | | - | return unpad(decrypted_data).decode() |
| 25 | + | return unpad(decrypted_data, self.BS) |
26 | 26 | | |