-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathEncrypt.py
More file actions
24 lines (19 loc) · 709 Bytes
/
Encrypt.py
File metadata and controls
24 lines (19 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import base64
from Crypto.Cipher import AES
class AESObject():
def __init__(self, key):
self.BLOCK_SIZE = 32
self.PADDING = '*'
self.key = key + ('x' * (self.BLOCK_SIZE - len(key)))
self.cipher = AES.new(self.key)
self.pad = lambda s: s + (self.BLOCK_SIZE - len(s) % self.BLOCK_SIZE) * self.PADDING
self.encodeAES = lambda c, s: base64.b64encode(c.encrypt(self.pad(s)))
self.decodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(self.PADDING)
def encrypt(self, string):
return self.encodeAES(self.cipher, string)
def decrypt(self, string):
try:
return self.decodeAES(self.cipher, string)
except ValueError:
# String isn't correct encrypted length
return None