我正在尝试使用Crypto
方法对一段文本进行编码
我需要使用RSA方法和给定的公钥对一段字符串进行编码,这是我在引用this link.之后当前编写的代码
我的代码
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64
def encrypt_private_key(a_message, private_key):
encryptor = PKCS1_OAEP.new(private_key)
encrypted_msg = encryptor.encrypt(a_message)
encoded_encrypted_msg = base64.b64encode(encrypted_msg)
return encoded_encrypted_msg
key = open('public.pem', 'r')
byte_key = bytes(key.read().encode())
byte_message = b'1200|2000.00'
output = encrypt_private_key(byte_message, byte_key)
print(byte_message)
然而,当我试图运行这段代码时,我被抛出了这个错误
File "C:\Users\Acer\Desktop\Web Development\Learning new features\Play video on scroll\venv\lib\site-packages\Crypto\Cipher\PKCS1_OAEP.py", line 107, in encrypt
modBits = Crypto.Util.number.size(self._key.n)
AttributeError: 'bytes' object has no attribute 'n'
我不明白我到底做错了什么
非常感谢您的帮助。 谢谢
加密使用收件人的公钥,因此只有收件人才能使用其私钥解密密文。相反,签名使用签名者的私钥,从而使用关联的公钥执行验证。在给定的情况下,消息将被加密,因此将应用收件人的公钥
在发布的代码中,缺少带有^{} 的键的导入,这会导致错误。如果添加了导入,则加密成功:
美国。另外this example在PyCryptodome文档中,使用OAEPadding实现RSA
相关问题 更多 >
编程相关推荐