【问题标题】:Error with encrypt message with RSA python使用 RSA python 加密消息时出错
【发布时间】:2015-08-11 07:25:44
【问题描述】:

使用示例代码使用 RSA 加密消息,但出现以下错误。

Traceback (most recent call last):
  File "E:/PythonProjects/skypy/skypy/codebase/utils/crypto.py", line 32, in <module>
    print(RSAPubKey.encrypt("Hello.", 32))
  File "E:\Programs\Python3.4\lib\site-packages\Crypto\PublicKey\RSA.py", line 150, in encrypt
    return pubkey.pubkey.encrypt(self, plaintext, K)
  File "E:\Programs\Python3.4\lib\site-packages\Crypto\PublicKey\pubkey.py", line 75, in encrypt
    ciphertext=self._encrypt(plaintext, K)
  File "E:\Programs\Python3.4\lib\site-packages\Crypto\PublicKey\RSA.py", line 224, in _encrypt
    return (self.key._encrypt(c),)
  File "E:\Programs\Python3.4\lib\site-packages\Crypto\PublicKey\_slowmath.py", line 65, in _encrypt
    return pow(m, self.e, self.n)
TypeError: unsupported operand type(s) for pow(): 'str', 'int', 'int'

这里是示例代码

from Crypto.PublicKey import RSA
from Crypto.Util import randpool

blah = randpool.RandomPool()
RSAKey = RSA.generate(1024, blah.get_bytes)

RSAPubKey = RSAKey.publickey()
print(RSAPubKey.encrypt("Hello.", 32))

操作系统使用的是Windows,可能是什么原因造成的这个问题?

【问题讨论】:

    标签: python python-3.x encryption rsa


    【解决方案1】:

    错误提示encrypt 方法不支持加密字符串消息。尝试先使用encode 将字符串编码为字节,例如:

    print(RSAPubKey.encrypt("Hello.".encode('utf-8'), 32))
    

    还值得注意的是,根据文档,encrypt 执行“教科书式”RSA 加密,由于缺少填充,这是不安全的。您应该改用Crypto.Cipher.PKCS1_OAEPCrypto.Cipher.PKCS1_v1_5

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-01
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      相关资源
      最近更新 更多