Files
tulin-code/对称加密-AES-2026-08-24/2. python实现aes加解密.py
Whale-Fall-Code 418a0411d1 Initialize encryption and JavaScript reverse-engineering study repository
Document the project structure and setup while excluding local dependencies and IDE settings.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-09 23:25:05 +08:00

32 lines
969 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)))