-1
import os
from cryptography.fernet import Fernet

#get files

files = []

for file in os.listdir():
        if file == "ransomware.py" or file == "thekey.key" or file == "decrypt.py":
                continue
        if os.path.isfile(file):
                files.append(file)

print(files)


with open("thekey.key" , "wb") as key:
        secretkey = key.read()


secretphrase = "bootcon"

user_phrase = input("Enter Phrase to decrypt your files\n")

if user_phrase == secretphrase:
        for file in files:
                with open(file, "rb") as thefile:
                        contents = thefile.read()
                contents_decrypted = Fernet(secretkey).decrypt(contents)
                with open(file, "wb") as thefile:
                        thefile.write(contents_decrypted)
                print("thank you your files are decrypted")



else:
        print("Wrong try again")

i keep moving it but wont work

Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
  • 2
    You keep moving what? What won't work? Are you getting an actual error traceback (if so, please [edit] your question to include that), or is your IDE linter complaining? The indentation in this code is all over the place, and definitely not conforming to Python style guidance. – JRiggles Jun 05 '23 at 17:45
  • 1
    Your editor may have an option to show tabs and spaces differently, use it if so. – Mark Ransom Jun 05 '23 at 17:53

1 Answers1

0

I optimized your code to make it more pro look and fixed indentation:

import os
from cryptography.fernet import Fernet


def get_files():
    exclude_files = ["ransomware.py", "thekey.key", "decrypt.py"]
    files = [
        file
        for file in os.listdir()
        if file not in exclude_files and os.path.isfile(file)
    ]
    return files


def decrypt_files(files, secret_key):
    for file in files:
        with open(file, "rb") as f:
            contents = f.read()
        contents_decrypted = Fernet(secret_key).decrypt(contents)
        with open(file, "wb") as f:
            f.write(contents_decrypted)
        print("Thank you! Your files have been decrypted.")


def main():
    with open("thekey.key", "rb") as key_file:
        secret_key = key_file.read()

    secret_phrase = "bootcon"
    user_phrase = input("Enter the phrase to decrypt your files: ")

    if user_phrase == secret_phrase:
        files = get_files()
        decrypt_files(files, secret_key)
    else:
        print("Wrong phrase. Try again.")


if __name__ == "__main__":
    main()

I hope it works for you.

Reza K Ghazi
  • 347
  • 2
  • 9