导入rsa私钥并加密字符串



尝试导入使用openssl创建的2048位RSA私钥,并使用它来加密字符串。在此之后https://cryptobook.nakov.com/asymmetric-key-ciphers/rsa-encrypt-decrypt-examples

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
pr_key = RSA.import_key(open('my_priv_key.pem', 'r').read())
msg = "blah"
encryptor = PKCS1_OAEP.new(pr_key)
encrypted = encryptor.encrypt(msg)
print(encrypted)

错误:

» python encrypt.py
Traceback (most recent call last):
File "encrypt", line 10, in <module>
encrypted = encryptor.encrypt(msg)
File "/python/lib/python3.7/site-packages/Crypto/Cipher/PKCS1_OAEP.py", line 121, in encrypt
db = lHash + ps + b'x01' + _copy_bytes(None, None, message)
TypeError: can't concat str to bytes

这是因为encrypt方法需要字节作为参数。使用msg = b'blah'就可以了,还可以将加密调用更改为encrypted = encryptor.encrypt(msg.encode('utf-8'))

最新更新