提问者:小点点

导入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

共1个答案

匿名用户

那是因为加密方法需要字节作为参数。使用msg=b'blah'应该可以做到这一点,并将加密调用更改为加密=加密.加密(msg. encode('utf-8'))