#πŸ”’ what this mean

62 messages Β· Page 1 of 1 (latest)

surreal talonBOT
#

@graceful cipher

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

graceful cipher
#
Permissions for C:\Users\kb\Desktop\Kbs Bundle\locked set to read/write.
Traceback (most recent call last):
  File "C:\Users\kb\Desktop\Kbs Bundle\lock.py", line 136, in <module>
    main()
    ~~~~^^
  File "C:\Users\kb\Desktop\Kbs Bundle\lock.py", line 111, in main
    encrypt_file(password)
    ~~~~~~~~~~~~^^^^^^^^^^
  File "C:\Users\kb\Desktop\Kbs Bundle\lock.py", line 52, in encrypt_file
    with open(FILE_PATH, "rb") as f:
         ~~~~^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\kb\\Desktop\\Kbs Bundle\\locked'

Process finished with exit code 1
rose latch
#

It means you don't have read permission to that file.

graceful cipher
#

idk if the code is right

#

how do i change read permission

rose latch
#

A better question is: why do you need to read it? What's that file for?

graceful cipher
#

i need it to lock

cloud thorn
#

It's a folder, it's trying to open a file

rose latch
#

(Curious: is it a folder?)

graceful cipher
#

how do i make it do folder not file

#

yea

cloud thorn
distant stratus
#

os.walk(directory)

rose latch
#

So lock.py is your code? Maybe you should post it or something.

cloud thorn
graceful cipher
rose latch
#

I'm a bit concerned that yourFILE_PATH and your ENCRYPTED_PATH seem to be the same. So you read the FILE_PATH, overwrite it with the encrypted stuff, and then remove FILE_PATH, which looks like it would remove the encrypted file.

#

And does os.chmod work sanely on windows?

graceful cipher
#

idk what to put as the encrypted

distant stratus
#

it seems ai generated...

rose latch
distant stratus
#

what you actually want to do? cuz the way it is doing is quite not right...

cloud thorn
distant stratus
#

oh i see

cloud thorn
#

More precisely this with changed path

graceful cipher
#
import hashlib
import secrets
import sys
import base64
from cryptography.fernet import Fernet

GUI_AVAILABLE = False
if "DISPLAY" in os.environ or sys.platform == "win32":
    try:
        import tkinter as tk
        from tkinter import messagebox, simpledialog
        GUI_AVAILABLE = True
    except ImportError:
        pass

FILE_PATH = "file.exe" 
ENCRYPTED_PATH = "minecraft.locked"
SALT_FILE = "salt.bin"

def derive_key(password: str, salt: bytes) -> bytes:
    key = hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100000, dklen=32)
    return base64.urlsafe_b64encode(key)

def encrypt_file(password: str):
    if not os.path.exists(FILE_PATH):
        print("Error: File not found!")
        return
    
    salt = secrets.token_bytes(16)
    derived_key = derive_key(password, salt)
    cipher = Fernet(derived_key)
    
    with open(FILE_PATH, "rb") as f:
        data = f.read()
    encrypted_data = cipher.encrypt(data)
    
    with open(ENCRYPTED_PATH, "wb") as f:
        f.write(encrypted_data)
    with open(SALT_FILE, "wb") as f:
        f.write(salt)
    
    os.remove(FILE_PATH)
    print("File locked. Use your password to unlock.")

# Decrypt the file
def decrypt_file(password: str):
    if not os.path.exists(ENCRYPTED_PATH) or not os.path.exists(SALT_FILE):
        print("Error: Required files not found!")
        return
    
    with open(SALT_FILE, "rb") as f:
        salt = f.read()
    
    derived_key = derive_key(password, salt)
    cipher = Fernet(derived_key)
    
    try:
        with open(ENCRYPTED_PATH, "rb") as f:
            encrypted_data = f.read()
        decrypted_data = cipher.decrypt(encrypted_data)
    except:
        print("Error: Decryption failed! Invalid password or corrupted file.")
        return
    
    with open(FILE_PATH, "wb") as f:
        f.write(decrypted_data)
    
    os.remove(ENCRYPTED_PATH)
    os.remove(SALT_FILE)
    print("File unlocked successfully.")

def main():
    if GUI_AVAILABLE:
        root = tk.Tk()
        root.withdraw()
        
        action = simpledialog.askstring("Input", "Type 'lock' to encrypt or 'unlock' to decrypt:")
        if action:
            action = action.strip().lower()
            password = simpledialog.askstring("Password", "Enter password:", show='*')
            
            if not password:
                messagebox.showerror("Error", "Password cannot be empty!")
                return
            
            if action == "lock":
                encrypt_file(password)
            elif action == "unlock":
                decrypt_file(password)
            else:
                messagebox.showerror("Error", "Invalid action.")
    else:
        print("Running in CLI mode (GUI not available).")
        action = input("Type 'lock' to encrypt or 'unlock' to decrypt: ").strip().lower()
        password = input("Enter password: ")
        
        if not password:
            print("Error: Password cannot be empty!")
            return
        
        if action == "lock":
            encrypt_file(password)
        elif action == "unlock":
            decrypt_file(password)
        else:
            print("Error: Invalid action.")

if __name__ == "__main__":
    main()
surreal talonBOT
#

Hey @graceful cipher!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
rose latch
#

At the least I'd want "xb" instead of "wb" for the writes. If the output file should be new.

distant stratus
#

ok so ur just putting the directories wrong

cloud thorn
distant stratus
#

*were, if more precise

graceful cipher
#

im playing fortnite

distant stratus
#

...

graceful cipher
cloud thorn
#

ot

graceful cipher
#

he singing

distant stratus
#

!ot

surreal talonBOT
#
Off-topic channel

#ot2-python-isnt-skynet-thats-silly

Please read our off-topic etiquette before participating in conversations.

distant stratus
#

@graceful cipher

graceful cipher
#

ohh

#

ok

#

so i dont get help nm?

cloud thorn
#

Well, you should only talk about stuff related to what you need help about

distant stratus
cloud thorn
#

That certainly isn't, you can talk about it in the off topic channel

graceful cipher
#

bc i was js showing yall bro

cloud thorn
#

I don't want to know, I don't think anyone else here do

distant stratus
graceful cipher
cloud thorn
#

Rule applied here btw

graceful cipher
#

ur ot

#

help me

distant stratus
#

so what is the problem now? u still getting permissionError?

graceful cipher
#

I dont nee dhelp nm

#

!closeee

surreal talonBOT
#
Did you mean:

!close

graceful cipher
#

!close

surreal talonBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.