-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathaeslib.py
32 lines (28 loc) · 947 Bytes
/
aeslib.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
#coding:utf-8
import sys
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
class prpcrypt():
def __init__(self, key):
self.key = key
self.mode = AES.MODE_CBC
def encrypt(self, text):
cryptor = AES.new(self.key, self.mode, self.key)
length = 16
count = len(text)
add = length - (count % length)
text = text + ('\0' * add)
self.ciphertext = cryptor.encrypt(text)
return b2a_hex(self.ciphertext)
def decrypt(self, text):
cryptor = AES.new(self.key, self.mode, self.key)
plain_text = cryptor.decrypt(a2b_hex(text))
return plain_text.rstrip('\0')
if __name__ == '__main__':
pc = prpcrypt('123xiaorui456789')
e = pc.encrypt("00000")
d = pc.decrypt(e)
print e, d
e = pc.encrypt("welcome to mysql blog, xiaorui.cc")
d = pc.decrypt(e)
print e, d