真的很想问这个。我需要把我的密文转移到另一个类,但它不起作用。我错过了什么?抱歉,邮件太长了(我已经删除了这两个类中的一些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)
问题在于你的范围。在方法中定义了
CipherText
。试着把CipherText
去掉,使它成为类中的一个全局变量。然后您可以通过调用Encmesbuttons.CipherText
来访问它它不是首选方法,但是您可以使用
global CipherText
而不是decmescommand()
中的global CT
,并且您的变量在所有地方都可以访问。当前变量CT
有您的CipherText
,它在所有地方都可以访问-因此您可以在encmescommand()
内使用CT
而不是CipherText
有一件事:方法
encmescommand()
必须在decmescommand()
之前执行,以便为CipherText
/CT
赋值编辑:
参见示例
结果:
编辑:更喜欢的方法
相关问题 更多 >
编程相关推荐