-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathaes_tutorial.py
36 lines (27 loc) · 1 KB
/
aes_tutorial.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
"""
https://pypi.org/project/pycryptodome/
pip install pycryptodome
ref
https://pycryptodome.readthedocs.io/en/latest/src/cipher/classic.html#cbc-mode
"""
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
def aes_encrypt(plaintext, key, iv):
# AES.block_size -> 16
encoded_padded = pad(plaintext.encode(), AES.block_size)
cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8'))
encoded = cipher.encrypt(encoded_padded)
print(encoded)
return encoded
def aes_decrypt(plaintext, key, iv):
# AES.block_size -> 16
decrypt_cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8'))
decrypted = decrypt_cipher.decrypt(plaintext)
decoded = unpad(decrypted, AES.block_size)
print(decoded)# 必須為16位
return decoded
plaintext = "hello world"
key = "key23X8Ib9LM8w16" # 必須為16位
iv = "iva23X8Ib9LM8w16" # 必須為16位
encode_data = aes_encrypt(plaintext, key, iv)
decoded_data = aes_decrypt(encode_data, key, iv)