我不能将我的类变量转移到另一个类中

2024-07-04 07:54:12 发布

您现在位置:Python中文网/ 问答频道 /正文

真的很想问这个。我需要把我的密文转移到另一个类,但它不起作用。我错过了什么?抱歉,邮件太长了(我已经删除了这两个类中的一些GUI(tkinter)代码。)我似乎在这里找不到解决方案。非常感谢你的帮助。谢谢

class Decmesbuttons():


   def decmescommand(self):
       datenow = str(datetime.datetime.now().date())
       timenow = str(datetime.datetime.now().time())


       privk = privkeyvarS.get()

       #No key found error
       try:
           pathpriv = r'/home/gene/Desktop/ppl/keys/' + privk
           loadprivkey = M2Crypto.RSA.load_key (pathpriv + '/' + privk + '-private.key')

       except IOError: 
           tkMessageBox.showwarning("Warning!", "No "+ privk + " key found.")


       #Wrong key error & printing of the decrypted message
       try:
           PlainText = loadprivkey.private_decrypt (Ciphertext, M2Crypto.RSA.pkcs1_oaep_padding)

           if PlainText != "":
               tkMessageBox.showinfo("DECRYPTED!","Message decrypted by " + privk + " :" + Plaintext)

       except:
           tkMessageBox.showwarning("Warning!", "Wrong key!")

class Encmesbuttons():


    def encmescommand(self):
        datenow = str(datetime.datetime.now().date())
        timenow = str(datetime.datetime.now().time())

        m = messagevarS.get()
        pk = pubkeyvarS.get()

        #Input of selected message to encrypt

        em = m + '\n' + '-'
        if not m:
            tkMessageBox.showwarning("Warning!", "No message to encrypt")

        else:
            #add to log
            logkey = open ('log'+datenow+'.txt', 'a')
            logkey.write(timenow +'-'  + ' Some user inputted a message.' + "\n") 
            logkey.close()

            f = open ('message.txt', 'w')
            f.write(str(m)) 
            f.close()

        try:
            #select the public key owner to send your encrypted message 

            pathpub = r'/home/gene/Desktop/ppl/keys/' + pk
            loadpub = M2Crypto.RSA.load_pub_key (pathpub+ '/' + pk + '-public.key')

            global CT
            CipherText = loadpub.public_encrypt (m, M2Crypto.RSA.pkcs1_oaep_padding)
            CT = CipherText

            #Print the ciphered message

            tkMessageBox.showinfo("The encrypted message",str(CipherText.encode('base64')))

            #write ciphered text to file
            f = open ('encryption.txt', 'w')
            f.write(str(CipherText.encode ('base64')))
            f.close()

            #add to log
            logkey = open ('log'+datenow+'.txt', 'a')
            logkey.write(timenow +'-' + 'Some user has encrypted the message and selected ' + pk + ' as the receiver' +  "\n") 
            logkey.close()


        except IOError: 
            tkMessageBox.showwarning("Warning!", "No public key inputted")

我需要这个变量:

CipherText = loadpub.public_encrypt (m, M2Crypto.RSA.pkcs1_oaep_padding)

排在这一行(有这里标志的那一行):

 PlainText = loadprivkey.private_decrypt (**HERE**, M2Crypto.RSA.pkcs1_oaep_padding)

Tags: thetokeymessagedatetimepublicrsawrite
2条回答

问题在于你的范围。在方法中定义了CipherText。试着把CipherText去掉,使它成为类中的一个全局变量。然后您可以通过调用Encmesbuttons.CipherText来访问它

它不是首选方法,但是您可以使用global CipherText而不是decmescommand()中的global CT,并且您的变量在所有地方都可以访问。当前变量CT有您的CipherText,它在所有地方都可以访问-因此您可以在encmescommand()内使用CT而不是CipherText

有一件事:方法encmescommand()必须在decmescommand()之前执行,以便为CipherText/CT赋值


编辑:

参见示例

class Decmesbuttons():

    def decmescommand(self):
        print('execute decmescommand()')

        print('CipherText:', CipherText)

class Encmesbuttons():


    def encmescommand(self):
         print('execute encmescommand()')

         global CipherText

         CipherText = "hello world"

d = Decmesbuttons()
e = Encmesbuttons()

e.encmescommand() # <- you have to run it before decmescommand()
d.decmescommand()

结果:

execute encmescommand()
execute decmescommand()
CipherText: hello world

编辑:更喜欢的方法

class Decmesbuttons():

    def decmescommand(self, text):
        print('execute decmescommand()')

        print('CipherText:', text)

class Encmesbuttons():


    def encmescommand(self):
        print('execute encmescommand()')

        CipherText = "hello world"

        return CipherText

d = Decmesbuttons()
e = Encmesbuttons()

result = e.encmescommand() # <- you have to run it before decmescommand()
d.decmescommand(result)

相关问题 更多 >

    热门问题