Completed
Push — master ( e7778f...95d0df )
by
unknown
33s
created

AESDecrypt   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 29 3
1
import json
2
import base64
3
4
from Crypto.Cipher import AES
5
from Crypto.Hash import MD5
6
7
from st2actions.runners.pythonrunner import Action
8
9
__all__ = [
10
    'AESDecrypt'
11
]
12
13
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