Total Complexity | 3 |
Total Lines | 30 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | import json |
||
14 | class AESDecrypt(Action): |
||
15 | def run(self, message, aes_key=None, encrypt=False): |
||
16 | if not aes_key: |
||
17 | aes_key = "this_is_so_secure" |
||
18 | |||
19 | key = MD5.new(aes_key).hexdigest() |
||
20 | |||
21 | aes_handler = AES.new(key, AES.MODE_CBC, 'Save the kittens') |
||
22 | |||
23 | if encrypt: |
||
24 | padding = 16 - (len(message) % 16) |
||
25 | |||
26 | message += chr(padding) * padding |
||
27 | |||
28 | result = { |
||
29 | 'message': base64.b64encode(aes_handler.encrypt(message)) |
||
30 | } |
||
31 | |||
32 | return json.dumps(result) |
||
33 | |||
34 | else: |
||
35 | message = base64.b64decode(message) |
||
36 | decrypted_message = aes_handler.decrypt(message) |
||
37 | unpadded_message = decrypted_message[:- ord(decrypted_message[-1])] |
||
38 | |||
39 | result = { |
||
40 | 'message': unpadded_message |
||
41 | } |
||
42 | |||
43 | return json.dumps(result) |
||
44 |