使用windows 10 专业版操作系统
用的是IDLE shell 3.9.5版本
代码内容如下:
from Crypto.Cipher import AES
from Crypto import Random
def fill_text(msg):
to_add = 0
if len(msg) %16 != 0:
to_add= 16 - len(msg) % 8
return msg + b\0"*to_add
#sdfghggfdfg
key=b'12345678'
iv = Random.new().read(AES.block_size)
cipher = AES.new(fill_text(key), AES.MODE_ECB, iv)
plaintext = "CSDN"
print("原文:",plaintext)
msg = cipher.encrypt(fill_text(plaintext.encode()))
print("加密后的字节:",msg)
text = cipher.decrypt(msg)
print("解密后的文本:",text.decode())
到底哪里错了?第二部分与第三部分之间的空格在执行代码后一直变红,这到底是为甚么?求各位提点一二。
代码中错误较多:1.所有打印语句中的逗号均是中文标点符号,改成英文的
2.text = cipher.decrypt(msg)这句在执行时会报错,在decrypt函数中有说明, 加密对象是有状态的,不能用该对象直接解密。需要建立另一个对象,将这一句改写成两句:d_cipher = AES.new(fill_text(key), AES.MODE_CBC, iv),text = d_cipher.decrypt(msg)
3.cipher = AES.new(fill_text(key), AES.MODE_ECB, iv),对于ECB模式iv没有意义,将其改成AES.MODE_CBC。
如果对你有帮助,请点击采纳按钮支持一下。
你应该把报错也截出来
from Crypto.Cipher import AES
from Crypto import Random
def fill_text(msg):
to_add = 0
if len(msg) %16 != 0:
to_add= 16 - len(msg) % 8
return msg + b'\0'*to_add
#sdfghggfdfg
key=b'12345678'
iv = Random.new().read(AES.block_size)
cipher = AES.new(fill_text(key), AES.MODE_ECB, iv)
plaintext = "CSDN"
print("原文:", plaintext)
msg = cipher.encrypt(fill_text(plaintext.encode()))
print("加密后的字节:", msg)
text = cipher.decrypt(msg)
print("解密后的文本:", text.decode())