我有一个公钥可以使用,我需要用我得到的RSA公钥来加密一些文本。
这是我到目前为止掌握的代码:
import rsa
fKey = open('key','r')
publicKey = fKey.read()
cipher = rsa.encrypt('Test', publicKey)
print(cipher)
通过这段代码,我不断地得到以下错误:
Traceback (most recent call last):
File "login.py", line 30, in <module>
cipher = rsa.encrypt('Test', publicKey)
File "/home/vagrant/.local/lib/python3.8/site-packages/rsa/pkcs1.py", line 169, in encrypt
keylength = common.byte_size(pub_key.n)
AttributeError: 'str' object has no attribute 'n'
谁能帮我指出正确的方向吗?
注意:我必须使用那个公钥文件
发布于 2020-09-17 05:10:41
我就是这样解决的。
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
from base64 import b64decode,b64encode
pubkey = open('key','r').read()
msg = "Test"
keyDER = b64decode(pubkey)
keyPub = RSA.importKey(keyDER)
cipher = Cipher_PKCS1_v1_5.new(keyPub)
cipher_text = cipher.encrypt(msg.encode())
emsg = b64encode(cipher_text)
print(emsg)
发布于 2022-05-28 05:12:46
你试过:
fKey = open('key','rb')
publicKey = rsa.PublicKey.load_pkcs1(fKey.read())
https://stackoverflow.com/questions/63937995
复制