簡體   English   中英

導入 rsa 私鑰並加密字符串

[英]import rsa private key and encrypt string

嘗試導入使用 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

這是因為加密方法需要字節為 arguments。 使用msg = b'blah'應該可以解決問題,並將 encrypt 調用更改為encrypted = encryptor.encrypt(msg.encode('utf-8'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM