-1

我正在尝试用 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
4

1 回答 1

0

我很确定您的语法问题是您使用的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: $
于 2018-08-21T22:12:39.223 回答