Python 不正确的解密RSA PyCryptome

Python 不正确的解密RSA PyCryptome,python,encryption,rsa,pycryptodome,Python,Encryption,Rsa,Pycryptodome,我尝试用pycryptodome在Python中实现RSA,加密工作正常,但解密函数不,我的代码如下: from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP from Crypto.Signature import pss from Crypto.Hash import SHA256 class RSA_OBJECT: def create_KeyPair(self): s

我尝试用pycryptodome在Python中实现RSA,加密工作正常,但解密函数不,我的代码如下:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Signature import pss
from Crypto.Hash import SHA256

class RSA_OBJECT:


           def create_KeyPair(self):

    self.key = RSA.generate(self.KEY_LENGTH)


def save_PrivateKey(self, file, password):

    key_cifrada = self.key.export_key(passphrase=password, pkcs=8,protection="scryptAndAES128-CBC")
    file_out = open(file, "wb")
    file_out.write(key_cifrada)
    file_out.close()


def load_PrivateKey(self, file, password):
           key_cifrada = open(file, "rb").read()
    self.private_key = RSA.import_key(key_cifrada, passphrase=password)


def save_PublicKey(self, file):
          key_pub = self.key.publickey().export_key()
    file_out = open(file, "wb")
    file_out.write(key_pub)
    file_out.close()

def load_PublicKey(self, file):

    key_publica = open(file, "rb").read()
    self.public_key = RSA.import_key(key_publica)    

我不知道为什么,因为我认为代码是正确的,任何人都可以帮助我?

您的问题是您生成了两个不同的密钥

self.public_key = RSA.generate(self.KEY_LENGTH)
self.private_key = RSA.generate(self.KEY_LENGTH)
你应该

key = RSA.generate(self.KEY_LENGTH)

请参阅更多详细信息



注意:注意,由于公钥加密,密钥对象有两个功能。您可以将私钥写入文件,将公钥写入另一个文件。通过这种方式,您可以分发密钥。请参阅。

完成,我上载cifrari上载所有代码我不知道如何在创建密钥对写入文件中使用此信息更改代码。然后从文件中加载。Thaks对于所有人来说,现在工作得非常完美。我认为重要的代码是我需要的代码edit@Doe把新的部分放在问题的末尾,加上标题,这样更好。让人们看到原创性和解决方案。
private_key = key.export_key()
file_out = open("private.pem", "wb")
file_out.write(private_key)

public_key = key.publickey().export_key()
file_out = open("receiver.pem", "wb")
file_out.write(public_key)