使用PKCS1_OAEP/PKCS1_v1_5的Python RSA加密无法解密特殊字符

使用PKCS1_OAEP/PKCS1_v1_5的Python RSA加密无法解密特殊字符,python,encryption,rsa,special-characters,pycryptodome,Python,Encryption,Rsa,Special Characters,Pycryptodome,我使用以下代码只加密/解密密码。除了特殊角色外,它工作得非常好。例如Pa$$w0rd返回Pa1705w0rd。知道怎么修吗?顺便说一句,我也尝试过PKCS1_v1_5,但结果相同 def _encrypt(self, message): public_key = RSA.importKey(self._get_public_key()) cipher = PKCS1_OAEP.new(public_key) encrypted_message = cipher.encry

我使用以下代码只加密/解密密码。除了特殊角色外,它工作得非常好。例如
Pa$$w0rd
返回
Pa1705w0rd
。知道怎么修吗?顺便说一句,我也尝试过PKCS1_v1_5,但结果相同

def _encrypt(self, message):
    public_key = RSA.importKey(self._get_public_key())
    cipher = PKCS1_OAEP.new(public_key)
    encrypted_message = cipher.encrypt(message)
    print(base64.b64encode(encrypted_message))

def _decrypt(self, encoded_encrypted_message):
    encrypted_message = base64.b64decode(encoded_encrypted_message)
    private_key = RSA.importKey(self._get_private_key())
    cipher = PKCS1_OAEP.new(private_key)
    print(cipher.decrypt(encrypted_message))

对我来说,很难给出一个确切的错误答案,因为我看不到执行它所需的整个程序。特别是,我不知道是否以字节的形式传递消息,如果是,则使用什么编码

但是,以下是工作代码:

import base64

from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA


def get_rsa_private_key():
    return RSA.generate(2048)


def encrypt(plaintext, public_key):
    cipher = PKCS1_OAEP.new(public_key)
    ciphertext = cipher.encrypt(bytes(plaintext, encoding="UTF-8"))
    return base64.b64encode(ciphertext)


def decrypt(base64_encoded_ciphertext, private_key):
    ciphertext = base64.b64decode(base64_encoded_ciphertext)
    cipher = PKCS1_OAEP.new(private_key)
    return cipher.decrypt(ciphertext)


key = get_rsa_private_key()
ciphertext = encrypt("Pa$$w0rd", key.publickey())
print("Base64-encoded ciphertext: %s" % str(ciphertext, encoding="UTF-8"))
decrypted_plaintext = decrypt(ciphertext, key)
print("Decrypted plaintext: %s" % str(decrypted_plaintext, encoding="UTF-8"))
输出:

Base64-encoded ciphertext: hy7dhLj8Hy1n1cjcn20x+dWG/bOjXv3zFEd1T/cm4oJgDHFTviD8uexe2pG+lMmoP6qP+1uRwjgfMnGkpLhRwk1w5eN9bjphsm+ekC8B+qjfIG6TLjL0GEcJKTWf/dgNNBSbTWI2bNXREBbxkVWW+11vUfWsP5ni2exhZIMrf29B1z2FAyixdsHQ5KKlvfTGE4LFbrCTNn4qp2tsMTylitdafwYhsSIm5qlwIRU+qTB5bz8nTJHnPyksEIffHXbCJNjUwJJKRihrTZ+vY78XccTY7Bmkw5fmf3KuDRqXR/2LvjWwBtqqrMQRnmArer9Qh3uTmRNvvhUOYsh10172LQ==
Decrypted plaintext: Pa$$w0rd

对我有用。您需要提供更多信息。问题不是加密解密。罪魁祸首是
argparse