以下是一个用Python写的AES256加密算法示例: ```python from Crypto.Cipher import AES from Crypto.Random import get_random_bytes def encrypt(text, key): cipher = AES.new(key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(text.encode('utf-8')) return nonce + ciphertext + tag def decrypt(ciphertext, key): nonce = ciphertext[:16] tag = ciphertext[-16:] ciphertext = ciphertext[16:-16] cipher = AES.new(key, AES.MODE_EAX, nonce=nonce) plaintext = cipher.decrypt_and_verify(ciphertext, tag) return plaintext.decode('utf-8') key = get_random_bytes(32) text = "Hello, World!" encrypted = encrypt(text, key) decrypted = decrypt(encrypted, key) print("Original text: ", text) print("Encrypted text: ", encrypted) print("Decrypted text: ", decrypted) ``` 这段代码使用了Python的Crypto库中的AES模块来实现AES256加密算法。在加密和解密过程中,需要使用相同的密钥。可以使用`get_random_bytes()`函数生成一个随机的32字节密钥。在上面的示例中,我们对"Hello, World!"进行加密,然后再解密,最终输出原始文本、加密后的文本和解密后的文本。 [2024-12-17 11:13:23 | AI写代码神器 | 736点数解答]