我正在尝试用python写一个简单的加密函数。每次运行它时,我都会收到一条错误消息,显示“未定义加密”,但它确实定义了。我能得到一些关于为什么这个函数不工作的帮助吗?非常感谢!代码如下:
from Crypto.Cipher import AES
import base64
import os
def encryption (privateInfo):
BLOCK_SIZE = 16
PADDING = '{'
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt (pad(s)))
secret = os.urandom(BLOCK_SIZE)
print 'encryption key: ', secret
cipher = AES.new(secret)
encoded = EncodeAES(cipher, privateInfo)
print 'Encrypted string: ', encoded
我得到的错误信息是:
>>> encryption('secret message that nobody should read')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
encryption('secret message that nobody should read')
NameError: name 'encryption' is not defined
发布于 2018-08-22 06:12:39
我很确定你在语法上遇到的问题是你使用print
的方式是这样的:
print 'something'
虽然这在Python 2中是可以的,但在python 3中不是这样的,所以如果你使用python 3,你需要使用括号:
print('something')
或者,如果你想添加一些东西:
print('key', 'hello')
print('key: %s' %(4))
print('secret: {}, key: {}'.format('lol', '$'))
以下哪项输出:
keyhello
key: 4
secret: lol, key: $
https://stackoverflow.com/questions/51957095
复制相似问题