def main(): # 测试用例 key = bytes.fromhex("6162636465666768696a6b6c6d6e6f70") ciphertext = bytes.fromhex("69c4e0d86a7b0430d8cdb78070b4c55a") aes = AES(int.from_bytes(key, 'big')) plaintext = aes.aes_decrypt(ciphertext) print("解密结果:", plaintext.hex()) if __name__ == "__main__": main() 帮我把这个主函数改为key和ciphertext由用户输入, key = input("请输入16字节密钥(例如:6162636465666768696a6b6c6d6e6f70): ").strip() plaintext = input("请输入16字节明文(例如:00112233445566778899aabbccddeeff): ").strip() try: key_bytes = bytes.fromhex(key) plain_bytes = bytes.fromhex(plaintext) if len(key_bytes) != 16 or len(plain_bytes) != 16: raise ValueError("长度错误") except: print("输入格式错误,请确保输入的是32位十六进制字符(16字节)") return aes = AES(int.from_bytes(key_bytes, 'big')) ciphertext = aes.encrypt_block(plain_bytes) print("加密结果:", ciphertext.hex())格式参照如下
时间: 2025-03-11 15:05:54 浏览: 44
<<
为了满足您的需求,将代码调整为从用户获取`key`和`ciphertext`作为输入值并进行相应的处理。下面是根据您提供的模板修改后的Python脚本版本:
```python
def main():
# 用户输入测试用例
key_input = input("请输入16字节密钥(例如:6162636465666768696a6b6c6d6e6f70): ").strip()
cipher_text_input = input("请输入16字节密文(例如:69c4e0d86a7b0430d8cdb78070b4c55a): ").strip()
try:
key_bytes = bytes.fromhex(key_input)
cipher_bytes = bytes.fromhex(cipher_text_input)
if len(key_bytes) != 16 or len(cipher_bytes) != 16:
raise ValueError("长度错误")
aes = AES(int.from_bytes(key_bytes, 'big'))
plaintext = aes.aes_decrypt(cipher_bytes)
print("解密结果:", plaintext.hex())
except Exception as e:
print(f"发生了一个错误:{str(e)}")
if __name__ == "__main__":
main()
```
请注意,上述示例假定存在名为 `AES` 的类及方法 `aes_decrypt` ,它们能接收给定参数完成AES算法的操作;并且此环境支持直接转换大整数到该类实例化。
阅读全文