-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_aes.py
41 lines (34 loc) · 1.16 KB
/
crypto_aes.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
from Crypto.Cipher import DES
class MyDESCrypt:
key = chr(11) + chr(11) + chr(11) + chr(11) + chr(11) + chr(11) + chr(11) + chr(11)
iv = chr(22) + chr(22) + chr(22) + chr(22) + chr(22) + chr(22) + chr(22) + chr(22)
def __init__(self, key='', iv=''):
if len(key) > 0:
self.key = key
if len(iv) > 0:
self.iv = iv
def ecrypt(self, ecryptText):
try:
cipherX = DES.new(self.key, DES.MODE_CBC, self.iv)
pad = 8 - len(ecryptText) % 8
padStr = ""
for i in range(pad):
padStr = padStr + chr(pad)
ecryptText = ecryptText + padStr
x = cipherX.encrypt(ecryptText)
return x.encode('hex_codec').upper()
except:
return ""
def decrypt(self, decryptText):
try:
cipherX = DES.new(self.key, DES.MODE_CBC, self.iv)
str = decryptText.decode('hex_codec')
y = cipherX.decrypt(str)
return y[0:ord(y[len(y) - 1]) * -1]
except:
return ""
if __name__=='__main__':
a=MyDESCrypt()
b=a.ecrypt('123456')
print b
print a.decrypt(b)