#π what this mean
62 messages Β· Page 1 of 1 (latest)
@graceful cipher
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.
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
It means you don't have read permission to that file.
A better question is: why do you need to read it? What's that file for?
i need it to lock
It's a folder, it's trying to open a file
(Curious: is it a folder?)
Zip the folder
recursively walk the directory
os.walk(directory)
So lock.py is your code? Maybe you should post it or something.
According to file structure from the IDE, yes
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?
idk what to put as the encrypted
it seems ai generated...
Sigh.
what you actually want to do? cuz the way it is doing is quite not right...
It was a python file from a GitHub repo in #python-discussion
oh i see
More precisely this with changed path
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()
Hey @graceful cipher!
Add a py after the three backticks.
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
At the least I'd want "xb" instead of "wb" for the writes. If the output file should be new.
ok so ur just putting the directories wrong
Hmm, but people might want to just overwrite the file when they update something, it dependsπ€ but it have UI so it would nice to show warning about it
*were, if more precise
im playing fortnite
...
ot
he singing
!ot
#ot2-python-isnt-skynet-thats-silly
Please read our off-topic etiquette before participating in conversations.
@graceful cipher
Well, you should only talk about stuff related to what you need help about
if u want to get help, why are you showing fortnite...
That certainly isn't, you can talk about it in the off topic channel
bc i was js showing yall bro
I don't want to know, I don't think anyone else here do
yes, and the right place is !ot
i was js showing i didnt need ur opinion if u wanted to see or no
Rule applied here btw
so what is the problem now? u still getting permissionError?
!close
!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.