当我运行我的Python程序时,我遇到一个错误:PermissionError: [Errno 13] Permission returned,我不知道为什么它不起作用我想知道在运行这个程序时我是否可以以某种方式提升自己。我正在使用 vscode,我正在尝试加密一些文件。
import os
from cryptography.fernet import Fernet
# generate a random encryption key
def generate_key():
return Fernet.generate_key()
# encrypt a file with custom byte size
def encrypt_file(key, filename, byte_size):
with open(filename, 'rb') as f:
data = f.read(byte_size)
cipher = Fernet(key)
encrypted_data = cipher.encrypt(data)
with open(filename + '.encrypted', 'wb') as f:
f.write(encrypted_data)
return key
# save encryption key to a text file
def save_key_to_file(key):
with open('encryption_key.txt', 'wb') as f:
f.write(key)
# decrypt a file with a given key
def decrypt_file(key, filename):
with open(filename, 'rb') as f:
encrypted_data = f.read()
cipher = Fernet(key)
decrypted_data = cipher.decrypt(encrypted_data)
with open('decrypted_' + filename, 'wb') as f:
f.write(decrypted_data)
# me
# ain function
def main():
choice = input("Enter 'E' to encrypt or 'D' to decrypt: ").upper()
if choice == 'E':
byte_size = int(input("Enter the byte size for encryption: "))
filename = input("Enter the name of the file to encrypt: ")
key = generate_key()
encrypt_file(key, filename, byte_size)
save_key_to_file(key)
print("Encryption completed.")
print("Encryption key has been saved to 'encryption_key.txt'.")
elif choice == 'D':
key = input("Enter the decryption key: ")
filename = input("Enter the name of the encrypted file to decrypt: ")
decrypt_file(key, filename)
print("Decryption completed.")
else:
print("Invalid choice. Please enter 'E' or 'D'.")
if __name__ == "__main__":
main()
我还没有尝试过任何事情,因为我什至不知道从哪里开始
我已经使用 vscode 尝试过你的代码,看起来没有问题。 但是,我设法创建您面临的相同错误,如果您尝试加密文件夹,它将显示您提到的错误“PermissionError:[Errno 13]权限被拒绝:'the_folder_name'”,因为代码用于加密文件并且不是文件夹
确保指定了目标文件。