Python 名称错误:名称';加密的消息密钥';没有定义
因此,运行encrypt_mk()时的输出为: 回溯(最近一次呼叫最后一次): 文件“C:\Users\ilias\PycharmProjects\virus thing\main.py”,第181行,在 加密_mk() 文件“C:\Users\ilias\PycharmProjects\virus thing\main.py”,第123行,在encrypt_mk中 文件2=打开(加密的消息密钥,“w”) NameError:未定义名称“加密的\u消息\u密钥” 我真的不明白为什么,因为我在派系get_目录()中清楚地设置了它 有人知道发生了什么事吗Python 名称错误:名称';加密的消息密钥';没有定义,python,file,encryption,nameerror,Python,File,Encryption,Nameerror,因此,运行encrypt_mk()时的输出为: 回溯(最近一次呼叫最后一次): 文件“C:\Users\ilias\PycharmProjects\virus thing\main.py”,第181行,在 加密_mk() 文件“C:\Users\ilias\PycharmProjects\virus thing\main.py”,第123行,在encrypt_mk中 文件2=打开(加密的消息密钥,“w”) NameError:未定义名称“加密的\u消息\u密钥” 我真的不明白为什么,因为我在派系
from cryptography.fernet import Fernet
import base64
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import os
password_to_encrypt = "mnwzJkdUUzUKmo8j8c3IG7MtfyIyRjkRJKxGQXX6VwQ="
def get_directories():
global key_file, message_encrypted, message_decrypted, encrypted_message_key, decrypted_message_key
path = 'C:/Users/ilias/PycharmProjects/virus-thing/idk'
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.txt' in file:
files.append(os.path.join(r, file))
for f in files:
if "key.txt" in f:
key_file = f
print(key_file)
elif "message_encrypted.txt" in f:
message_encrypted = str(f)
print(message_encrypted)
elif "message_decrypted.txt" in f:
message_decrypted = str(f)
print(message_decrypted)
elif "encrypted_message_key.txt" in f:
encrypted_message_key = str(f)
print(encrypted_message_key)
elif "decrypted_message_key.txt" in f:
decrypted_message_key = str(f)
print(decrypted_message_key)
def generate_key_password():
global key_file, message_encrypted, message_decrypted, encrypted_message_key, decrypted_message_key
password_provided = password_to_encrypt
password = password_provided.encode()
salt = b'salt_'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(password))
print(f"generate key password: {key}")
file = open(key_file, "wb")
file.write(key)
file.close()
def generate_key_random():
global key_file, message_encrypted, message_decrypted, encrypted_message_key, decrypted_message_key
key = Fernet.generate_key()
file = open(key_file, "wb")
file.write(key)
file.close()
def encrypt(data):
global key_file, message_encrypted, message_decrypted, encrypted_message_key, decrypted_message_key
file = open(key_file, "rb")
key = file.read()
file.close()
message = str(data)
encoded = message.encode()
f = Fernet(key)
encrypted = f.encrypt(encoded)
file2 = open(message_encrypted, "wb")
file2.write(encrypted)
file2.close()
def decrypt():
global key_file, message_encrypted, message_decrypted, encrypted_message_key, decrypted_message_key
file = open(message_encrypted, "rb")
message = file.read()
file.close()
file = open(key_file, "rb")
key = file.read()
file.close()
f = Fernet(key)
decrypted = f.decrypt(message)
original_message = decrypted.decode()
file = open(message_decrypted, "w")
file.write(original_message)
file.close()
def encrypt_key():
global key_file, message_encrypted, message_decrypted, encrypted_message_key, decrypted_message_key
global encrypted_key
file1 = open(key_file, "rb")
random_key = file1.read()
print(f"random_key: {random_key}")
file1.close()
generate_key_password()
encrypt(random_key)
file2 = open(message_encrypted, "rb")
encrypted_key = file2.read()
print(f"encrypted_key: {encrypted_key}")
file2.close()
def encrypt_mk():
get_directories()
global key_file, message_encrypted, message_decrypted, encrypted_message_key, decrypted_message_key
global encrypted_key
generate_key_random()
encrypt(input("data: "))
file1 = open(message_encrypted, "rb")
message = file1.read()
print(f"message: {message}")
file1.close()
encrypt_key()
file2 = open(encrypted_message_key, "w")
encrypted_key = str(encrypted_key)
file2.write(f"encrypted_message: {message}\nencrypted_key: {encrypted_key}")
file2.close()
clear_files()
def decrypt_mk():
get_directories()
global key_file, message_encrypted, message_decrypted, encrypted_message_key, decrypted_message_key
generate_key_password()
file1 = open(encrypted_message_key, "r")
line1 = file1.readline()
line2 = file1.readline()
file1.close()
line1 = line1.split("'")
line2 = line2.split("'")
encrypted_message = line1[1]
encrypted_key = line2[1]
file2 = open(message_encrypted, "w")
file2.write(encrypted_key)
file2.close()
decrypt()
file3 = open(message_decrypted, "r")
read_file = file3.read()
read_file = read_file.split("'")
decrypted_key = read_file[1]
file3.close()
file4 = open(key_file, "w")
file4.write(decrypted_key)
file4.close()
file5 = open(message_encrypted, "w")
file5.write(encrypted_message)
file5.close()
decrypt()
file6 = open(message_decrypted, "r")
decrypted_message = file6.read()
file6.close()
file7 = open(decrypted_message_key, "w")
file7.write(f"decrypted message: {decrypted_message}\ndecrypted key: {decrypted_key}")
file7.close()
clear_files()
def clear_files():
get_directories()
global key_file, message_encrypted, message_decrypted, encrypted_message_key, decrypted_message_key
file1 = open(message_encrypted, "w")
file1.write(" ")
file1.close()
file2 = open(message_decrypted, "w")
file2.write(" ")
file2.close()
file3 = open(key_file, "w")
file3.write(" ")
file3.close()
如果f中有encrypted_message_key.txt
,那么f中也有key.txt
。因为key.txt
是encrypted\u message\u key.txt
的一部分因此,您永远不能转到f:分支中的
elif“encrypted\u message\u key.txt”。f:
中的elif“decrypted\u message\u key.txt”也是如此
也许您应该先检查encrypted\u message\u key.txt
。您可以在一个函数中定义它。您可以在另一个函数中使用它。默认情况下,变量不是全局变量。可能您没有名为加密的\u message\u key.txt的文件
@usr2564301更进一步,有一个全局
语句应该做正确的事情。函数中定义的变量对其他函数不可用。如果需要在函数之间共享信息,可以a)在函数外部定义变量b)使用类创建对象
,因此,您可以使用self.
创建可在该对象内的函数之间共享的属性。@ilias这有点尴尬,但实际上在投票按钮下面有一个接受按钮。。。
for f in files:
if "key.txt" in f:
key_file = f
print(key_file)
elif "message_encrypted.txt" in f:
message_encrypted = str(f)
print(message_encrypted)
elif "message_decrypted.txt" in f:
message_decrypted = str(f)
print(message_decrypted)
elif "encrypted_message_key.txt" in f:
encrypted_message_key = str(f)
print(encrypted_message_key)
elif "decrypted_message_key.txt" in f:
decrypted_message_key = str(f)
print(decrypted_message_key)