forked from binux/libMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptUtils.py
54 lines (40 loc) · 1.28 KB
/
CryptUtils.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import base64
from config import AESkey
_BS_ = AES.block_size
_pad_ = lambda s: s + (_BS_ - len(s) % _BS_) * chr(_BS_ - len(s) % _BS_)
_unpad_ = lambda s: s[0:-ord(s[-1])]
class Crypt(object):
def __init__(self, key=""):
self._key = key
def __buildCipher(self, key):
if key == None:
key = self._key
return AES.new(key)
def decode(self, data, key=None):
try:
cipher = self.__buildCipher(key)
return _unpad_(cipher.decrypt(data))
except:
return data
def encode(self, data, key=None):
cipher = self.__buildCipher(key)
return cipher.encrypt(_pad_(data))
def decode64(self, data, key=None):
dt = base64.b64decode(data)
return self.decode(dt, key)
def encode64(self, data, key=None):
dt = self.encode(data, key)
return base64.b64encode(dt)
def getCryptUrl(self, string):
if string.find("?") < 0:
string += "?"
if not string.endswith("?"):
string += "&"
string += "cyt=1"
return string
def setKey(self, key):
self.key = key
crypt = Crypt(AESkey)