真的不顾一切地问这个。我需要将我的CipherText转移到另一个类中,它不起作用。我错过了什么?对不起,很长的帖子。 (我已经删除了那两个类中的一些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)
在这一行(带有Here符号的那一行):
PlainText = loadprivkey.private_decrypt (**HERE**, M2Crypto.RSA.pkcs1_oaep_padding)
答案 0 :(得分:0)
问题在于你的范围。您在方法中定义了CipherText
。尝试取出CipherText
并将其作为班级中的全局变量。然后,您可以通过拨打Encmesbuttons.CipherText
来访问它。
答案 1 :(得分:0)
这不是首选方法,但您可以在global CipherText
中使用global CT
代替decmescommand()
,并且您的变量可以在所有地方使用。目前变量CT
包含您的CipherText
,并且可以在所有地方访问 - 因此您可以在CT
内使用CipherText
代替encmescommand()
。
有一点是:方法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)