我正在编写一个简单的 AES 加密代码,但我被困在它说的部分:
TypeError:
decrypt()
之后无法调用encrypt()
我尝试更改这些行的顺序,但没有帮助:
encrypted = encrypt(message)
decrypted = decrypt(encrypted)
我有两个例子:
示例 1
from Crypto.Cipher import AES
key = 'ACD310AE179CE245624BB238867CE189'
message = 'this is my super secret message'
cipher = AES.new(key.encode('utf-8'),AES.MODE_CBC)
def pad(s):
return s + ((16 - len(s) % 16) * '{')
def encrypt(plaintext):
global cipher
return cipher.encrypt(pad(plaintext).encode('utf-8'))
def decrypt(ciphertext):
global cipher
dec = cipher.decrypt(ciphertext).decode('utf-8')
l = dec.count('{')
return dec[:len(dec)-1]
encrypted = encrypt(message)
decrypted = decrypt(encrypted)
print("Message: ", message)
print("Encrypted: ", encrypted)
print("Decrypted: ", decrypted)
示例 2
from Crypto.Cipher import AES
key = b'Sixteen byte key'
data = b'hello from other side'
cipher = AES.new(key, AES.MODE_EAX)
e_data = cipher.encrypt(data)
d_data = cipher.decrypt(e_data)
print("Encryption was: ", e_data)
print("Original Message was: ", d_data)