用于加密/解密的Python脚本突然不起作用

2024-03-29 09:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在复习公钥/私钥对加密。为了向自己说明这个概念,我写了一个原始脚本。一周前,我的python客户端还可以使用它:

> Python 2.7.9 (default, Apr  2 2015, 15:33:21)  [GCC 4.9.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>>

今天,我尝试运行它,但出现以下错误:

work@work-IdeaPad-U330p:~/projects/Script$ python example_encrypt_decrypt.py 
Traceback (most recent call last):
  File "example_encrypt_decrypt.py", line 9, in <module>
    encrypted_message = public_key_1.encrypt(M, 24)
  File "/usr/local/lib/python2.7/dist-packages/Crypto/PublicKey/RSA.py", line 123, in __getattr__
    raise AttributeError(attrname)
AttributeError: encrypt

我运行的脚本是:

from Crypto.PublicKey import RSA
import binascii

private_key_1 = RSA.generate(1024)
public_key_1  = private_key_1.publickey()

M = "Hello World."

encrypted_message = public_key_1.encrypt(M, 24)
# print binascii.hexlify(encrypted_message[0])

# The private key corresponding to the public key used
# to encrypt the message can decrypt the message.
decrypted_message = private_key_1.decrypt(encrypted_message)
print decrypted_message

# The message can not be decrypted with another
# private key.
private_key_2 = RSA.generate(1024)
print private_key_2.decrypt(encrypted_message)

Tags: thekeypy脚本messageexampleprivatepublic