forked from mhaskar/Octopus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.py
35 lines (28 loc) · 907 Bytes
/
encryption.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python
import base64
from Crypto import Random
from Crypto.Cipher import AES
import string
import random
from .functions import *
# block_size
block_size = AES.block_size
# function to pad the input text
pad = lambda s: s + (block_size - len(s) % block_size) * chr(block_size - len(s) % block_size)
def encrypt_command(key, plain):
iv = Random.new().read(AES.block_size)
decoded_key = base64.b64decode(key)
aesobj = AES.new(decoded_key, AES.MODE_CBC, iv)
data = pad(plain)
try:
encd = aesobj.encrypt(data)
return base64.b64encode(iv + encd)
except:
return ""
def decrypt_command(key, encrypted_text):
decoded_key = base64.b64decode(key)
data = base64.b64decode(encrypted_text)
iv = data[:block_size]
aesobj = AES.new(decoded_key, AES.MODE_CBC, iv)
decd = aesobj.decrypt(data)[block_size:].decode("UTF-8")
return decd