python - 为什么我的脚本不产生输出,而交互式 session 中的相同代码却产生输出?

标签 python

我从 pycrypto 示例中得到以下信息:

>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key456', AES.MODE_ECB)
>>> message = "The answer is no"
>>> ciphertext = obj.encrypt(message)
>>> ciphertext
'o\x1aq_{P+\xd0\x07\xce\x89\xd1=M\x989'
>>> obj2 = AES.new('This is a key456', AES.MODE_ECB)
>>> obj2.decrypt(ciphertext)
'The answer is no'

现在我创建一个脚本:

#!/usr/bin/env python
from Crypto.Cipher import AES
obj = AES.new('This is a key456', AES.MODE_ECB)
message = "The answer is no"
ciphertext = obj.encrypt(message)
ciphertext
#'o\x1aq_{P+\xd0\x07\xce\x89\xd1=M\x989'
obj2 = AES.new('This is a key456', AES.MODE_ECB)
obj2.decrypt(ciphertext)

当我运行脚本时。我希望看到字符串输出

'o\x1aq_{P+\xd0\x07\xce\x89\xd1=M\x989'
'The answer is no'

但是我什么也没得到。我尝试了 print ciphertext,但它打印了乱码。

请帮忙。

最佳答案

你必须调用 print 函数(或 py2 中的 print 语句)来打印你想要打印的内容:

#!/usr/bin/env python
from Crypto.Cipher import AES
obj = AES.new('This is a key456', AES.MODE_ECB)
message = "The answer is no"
ciphertext = obj.encrypt(message)
print(ciphertext)
#'o\x1aq_{P+\xd0\x07\xce\x89\xd1=M\x989'
obj2 = AES.new('This is a key456', AES.MODE_ECB)
print(obj2.decrypt(ciphertext))

关于python - 为什么我的脚本不产生输出,而交互式 session 中的相同代码却产生输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23023581/

相关文章:

python - 在 Python Pandas 中格式化字符串数字

python - 使用python下载时在http连接中寻找

python - ROS中如何订阅和发布图片

python - 类函数不返回正确的值

python - Django 图像未上传

python - CherryPy + Werkzeug 调试器?

python - 超过 2 维的 numpy 点

javascript - 相当于 Javascript 中的 Python 目录

python - boto set_contents_from_filename 内存泄漏

python - 检查程序是否以编程方式运行