Python Cypto:AttributeError:"函数"对象没有属性"加密"



我的代码是

import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
random_generator = Random.new().read
key = RSA.generate(1024, random_generator) #generate public and private keys
publickey = key.publickey # pub key export for exchange
encrypted = publickey.encrypt('encrypt this message', 32)
#message to encrypt is in the above line 'encrypt this message'
print ('encrypted message:', encrypted) #ciphertext
f = open ('encryption.txt', 'w')
f.write(str(encrypted)) #write ciphertext to file
f.close()
#decrypted code below
f = open ('encryption.txt', 'r')
message = f.read()
decrypted = key.decrypt(message)
print ('decrypted', decrypted)
f = open ('encryption.txt', 'w')
f.write(str(message))
f.write(str(decrypted))
f.close()

输出错误为

Traceback (most recent call last):
File "C:UsersvinayDesktopDIATProgramsPython2018-10-08-RSA-sample.py", line 10, in <module>
encrypted = publickey.encrypt('encrypt this message', 32)
AttributeError: 'function' object has no attribute 'encrypt'

可用的软件包有

C:Program Files (x86)Python37-32Scripts>pip list
Package         Version
--------------- ---------
certifi         2018.8.24
chardet         3.0.4
cycler          0.10.0
Django          2.1.2
idna            2.7
image           1.5.27
kiwisolver      1.0.1
matplotlib      3.0.0
Naked           0.1.31
numpy           1.15.2
opencv-python   3.4.3.18
pandas          0.23.4
Pillow          5.3.0
pip             18.1
pprint          0.1
pycryptodome    3.6.6
pyparsing       2.2.2
python-dateutil 2.7.3
pytz            2018.5
PyYAML          3.13
requests        2.19.1
scikit-learn    0.20.0
scipy           1.1.0
setuptools      39.0.1
shellescape     3.4.1
six             1.11.0
sklearn         0.0
urllib3         1.23
wheel           0.32.1

运行代码时,我收到如图所示的错误。尝试了所有可能的组合来解决它。

你应该使用公钥函数
生成一个密钥publickey = key.publickey()

相关内容

  • 没有找到相关文章