Document the project structure and setup while excluding local dependencies and IDE settings. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
32 lines
969 B
Python
32 lines
969 B
Python
import base64
|
||
from Crypto.Cipher import AES
|
||
from Crypto.Util.Padding import pad, unpad
|
||
"""
|
||
des加解密,key长度有要求
|
||
key长度必须8字节(64bit)
|
||
|
||
aes加解密,key长度有要求
|
||
key长度必须16字节(128bit), 24字节(192bit), 32字节(256bit)
|
||
"""
|
||
|
||
def aes_encrypt(plain_text):
|
||
cipher = AES.new(des_key, AES.MODE_CBC, iv)
|
||
encrypted = cipher.encrypt(pad(plain_text.encode("utf-8"), AES.block_size))
|
||
return base64.b64encode(encrypted)
|
||
|
||
|
||
def aes_decrypt(cipher_text):
|
||
cipher = AES.new(des_key, AES.MODE_CBC, iv)
|
||
decrypt_byte_data = base64.b64decode(cipher_text)
|
||
decrypted = unpad(cipher.decrypt(decrypt_byte_data), AES.block_size)
|
||
return decrypted.decode("utf-8")
|
||
|
||
|
||
source_data = "你好,世界!"
|
||
des_key = "1234567890abcdef".encode("utf-8")
|
||
iv = "abcdef1234567890".encode("utf-8")
|
||
if __name__ == "__main__":
|
||
enc_data = aes_encrypt(source_data)
|
||
print(enc_data)
|
||
print(aes_decrypt(aes_encrypt(source_data)))
|