繁体   English   中英

使用RSA python加密消息时出错

[英]Error with encrypt message with RSA python

使用示例代码使用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,可能是由于这个问题?

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM