aes案例
# 案例
from Crypto.Cipher import AES
"""
AES加解密,windows和linux使用的库有区别,Crypto在linux和unix使用
"""
class AesCBC:
@staticmethod
def encrypt_cbc(data, key, iv):
# aes加密 CBC ,padding:PKCS7
cipher = AES.new(key, AES.MODE_CBC, iv)
x = AES.block_size - (len(data) % AES.block_size)
if x != 0:
data = data + chr(x) * x
msg = cipher.encrypt(data)
return msg
@staticmethod
def decrypt_cbc(data, key, iv):
# aes解密 CBC ,padding:PKCS7
cipher = AES.new(key, AES.MODE_CBC, iv)
msg = cipher.decrypt(data)
padding_len = ord(msg[len(msg) - 1])
return msg[0:-padding_len]
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
上次更新: 2023/09/04, 18:39:45