-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes.py
32 lines (30 loc) · 1.07 KB
/
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
#Using the PyCrypto module
import base64
from Crypto.Cipher import AES, DES
import random
import hashlib
def pkcs7padding(text):
bs = AES.block_size
length = len(text)
bytes_length = len(bytes(text, encoding='utf-8'))
padding_size = length if(bytes_length == length) else bytes_length
padding = bs - padding_size % bs
padding_text = chr(padding) * padding
return text + padding_text
def pkcs7unpadding(text):
length = len(text)
unpadding = ord(text[length-1])
return text[0:length-unpadding]
def encrypt(key, iv, content):
cipher = AES.new(key, AES.MODE_CBC, iv)
content_padding = pkcs7padding(content)
encrypt_bytes = cipher.encrypt(bytes(content_padding, encoding='utf-8'))
result = str(base64.b64encode(encrypt_bytes), encoding='utf-8')
return result
def decrypt(key, iv, content):
cipher = AES.new(key, AES.MODE_CBC, iv)
encrypt_bytes = base64.b64decode(content)
decrypt_bytes = cipher.decrypt(encrypt_bytes)
result = str(decrypt_bytes, encoding='utf8')
result = pkcs7unpadding(result)
return result