Reputation: 13
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()
so the output when you run encrypt_mk() is:
Traceback (most recent call last): File "C:\Users\ilias\PycharmProjects\virus-thing\main.py", line 181, in encrypt_mk() File "C:\Users\ilias\PycharmProjects\virus-thing\main.py", line 123, in encrypt_mk file2 = open(encrypted_message_key, "w")
NameError: name 'encrypted_message_key' is not defined
I can't really understand why because I clearly set it in the faction get_directories() Does anyone know what is going on??
Upvotes: 0
Views: 761
Reputation: 871
In
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)
If you have encrypted_message_key.txt
in f, you also have key.txt
in f. Because key.txt
is part of encrypted_message_key.txt
.
So you can never go to the elif "encrypted_message_key.txt" in f:
branch. Same goes for elif "decrypted_message_key.txt" in f:
.
Maybe you should check encrypted_message_key.txt
first.
Upvotes: 1