Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import stat
- from cryptography.fernet import Fernet
- def change_permissions(file_path):
- try:
- os.chmod(file_path, stat.S_IRUSR | stat.S_IWUSR)
- print(f"Permissões modificadas para: {file_path}")
- except Exception as e:
- print(f"Falha ao alterar permissões para {file_path}: {e}")
- def generate_key():
- key = Fernet.generate_key()
- with open("filekey.key", "wb") as key_file:
- key_file.write(key)
- return key
- def load_key():
- return open("filekey.key", "rb").read()
- def encrypt_files(key):
- fernet = Fernet(key)
- for root, dirs, files in os.walk("."):
- for file in files:
- if file.endswith(".py") or file.endswith(".key"):
- continue
- file_path = os.path.join(root, file)
- change_permissions(file_path)
- try:
- with open(file_path, "rb") as original_file:
- original_data = original_file.read()
- encrypted_data = fernet.encrypt(original_data)
- with open(file_path, "wb") as encrypted_file:
- encrypted_file.write(encrypted_data)
- print(f"Arquivo criptografado: {file_path}")
- except Exception as e:
- print(f"Falha ao criptografar {file_path}: {e}")
- def decrypt_files(key):
- fernet = Fernet(key)
- for root, dirs, files in os.walk("."):
- for file in files:
- if file.endswith(".py") or file.endswith(".key"):
- continue
- file_path = os.path.join(root, file)
- change_permissions(file_path)
- try:
- with open(file_path, "rb") as encrypted_file:
- encrypted_data = encrypted_file.read()
- decrypted_data = fernet.decrypt(encrypted_data)
- with open(file_path, "wb") as decrypted_file:
- decrypted_file.write(decrypted_data)
- print(f"Arquivo descriptografado: {file_path}")
- except Exception as e:
- print(f"Falha ao descriptografar {file_path}: {e}")
- def main():
- print("Escolha uma opção: \n1. Criptografar arquivos\n2. Descriptografar arquivos")
- choice = input("Opção: ")
- if choice == "1":
- print("Gerando chave e criptografando arquivos...")
- key = generate_key()
- encrypt_files(key)
- print("Arquivos criptografados. Guarde a chave com segurança!")
- elif choice == "2":
- print("Descriptografando arquivos...")
- key = load_key()
- decrypt_files(key)
- print("Arquivos descriptografados com sucesso!")
- else:
- print("Opção inválida. Tente novamente.")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement