Skip to content

Commit

Permalink
Add save logs on Desktop and Typo's
Browse files Browse the repository at this point in the history
  • Loading branch information
Justaus3r authored Jun 7, 2021
1 parent 1ce10e1 commit d61e7e8
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 0 deletions.
110 changes: 110 additions & 0 deletions core/EncFi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""
For Encryption and Decryption of Files
Anfu 2021
*This file is a part of Anfu
"""
from cryptography.fernet import Fernet
import os
from rich import print
import time
import getpass
from tkinter import *

def CheckOs():
if os.name =='nt':
return "Windows"
return "Linux"
class FileEncryption:
def EncryptFile(self,path):
try:
self.path = path
FileName = os.path.basename(self.path)
#Generating an Fernet Encryption Key
Encryption_key = Fernet.generate_key()
with open(self.path,"rb")as ReadFile:
FileContent = ReadFile.read()
ReadFile.close()
#Using the Encryption Key
fernet = Fernet(Encryption_key)
#Encrypting the FileContent
Encrypted_Content = fernet.encrypt(FileContent)
#Writing the Encrypted Content into Orignal File
with open(self.path,"wb") as WriteFile:
WriteFile.write(Encrypted_Content)
WriteFile.close()
#print Message
#check os and make dirs and save keys
if CheckOs() == 'Windows':
if os.path.exists(f'C:\\Users\\{getpass.getuser()}\\Desktop\\ANFU_KEYS') is False:
os.mkdir(f"C:\\Users\\{getpass.getuser()}\\Desktop\\ANFU_KEYS")
with open(f"C:\\Users\\{getpass.getuser()}\\Desktop\\ANFU_KEYS\\{FileName}.FILE.anky","wb") as WriteKey:
WriteKey.write(Encryption_key)
WriteKey.close()
else:
if os.path.exists(f"/home/{getpass.getuser()}/Desktop/ANFU_KEYS") is False:
os.mkdir(f"/home/{getpass.getuser()}/Desktop/ANFU_KEYS")
with open(f"/home/{getpass.getuser()}/Desktop/ANFU_KEYS/{FileName}.FILE.anky","wb") as WriteKey:
WriteKey.write(Encryption_key)
WriteKey.close()
print("[green]Encryption Key File Available at /Desktop/ANFU_KEYS.Keep the Encryption Key safe or you [red]won't[/red] be able to Decrypt Files again[/green]\n")
except Exception as ex:
if CheckOs() == 'Windows':
path = f'C:\\Users\\{getpass.getuser()}\\Desktop\\ANFU_ERROR_LOG.txt'
else:
path = f'/home/{getpass.getuser()}/Desktop/ANFU_ERROR_LOG.txt'
time_now = time.asctime( time.localtime(time.time()) )
_error_message = f'''
------------------------ERROR LOG,TIME:{time_now}--------------------------------
{ex}
---------------------------------------------------------------------------------
'''
with open(path,'a') as write_error_log:
write_error_log.write(_error_message)
write_error_log.close()
print(f"An Error Occured.\n\tError:{ex}")
def DecryptFile(self,keyfile,Encrypted_File):
try:
self.keyfile = keyfile
self.Encrypted_File = Encrypted_File
#Openining File and Reading Key
with open(self.keyfile,"rb") as ReadKey:
Key = ReadKey.read()
ReadKey.close()
#Using key file
fernet = Fernet(Key)
#Opening Encrypted file and Reading it
with open(self.Encrypted_File,"rb") as encrypted_file:
EncryptedContent = encrypted_file.read()
encrypted_file.close()
#Decrypting the content
Decrypted = fernet.decrypt(EncryptedContent)
#Opening the file and writing the Decrypted content again
with open(self.Encrypted_File,"wb") as WriteDecrypted:
WriteDecrypted.write(Decrypted)
WriteDecrypted.close()
except Exception as ex:
ErrWindow = Tk()
ErrWindow.title("ERROR")
Label(ErrWindow,text="An Error Occured While Decrypting the File").pack()
Label(ErrWindow,text="Possible Reasons might be:").pack()
Label(ErrWindow,text="1:File wasn't Encrypted,or was corrupted").pack()
Label(ErrWindow,text="2:Invalid Decryption Key").pack()
Label(ErrWindow,text="3:File Permission Error").pack()
Label(ErrWindow,text="Error log saved in Desktop/ANFU_ERROR_LOG.txt").pack()
ErrWindow.mainloop()
if CheckOs() == 'Windows':
path = f'C:\\Users\\{getpass.getuser()}\\Desktop\\ANFU_ERROR_LOG.txt'
else:
path = f'/home/{getpass.getuser()}/Desktop/ANFU_ERROR_LOG.txt'
time_now = time.asctime( time.localtime(time.time()) )
_error_message = f'''
------------------------ERROR LOG,TIME:{time_now}--------------------------------
{ex}
---------------------------------------------------------------------------------
'''
with open(path,'a') as write_error_log:
write_error_log.write(_error_message)
write_error_log.close()
print("[red]An Error occured.Possible Reasons:\n1:Invalid Key\n2:File wasn't Encrypted\n3:File Permission Error\n4:File was a Directory[/red]")


131 changes: 131 additions & 0 deletions core/EncFo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""
For Encryption and Decryption of Folders
Anfu 2021
"""
import shutil
from cryptography.fernet import Fernet
from zipfile import ZipFile
import os
from rich import print
import time
from tkinter import *
import getpass
#Check Os
def CheckOs():
if os.name =='nt':
return "Windows"
return "Linux"

class FolderEncryption:
def EncryptFolder(self,path):
try:
#Extracting root dir from path(dir where file is present)
_root_dir = os.path.dirname(path)
#Extracting Basename from path
_File_basename = os.path.basename(path)
#Adding Folder to zip so we can encrypt the zip
shutil.make_archive(_File_basename,'zip',path)
#Generating the Encryption Key
Encryption_key = Fernet.generate_key()
#For using the Key
fernet = Fernet(Encryption_key)
#Openinig the zip file to encrypt it
with open(f"{_File_basename}.zip","rb") as ReadContent:
Content = ReadContent.read()
ReadContent.close()
#Encrypting the file content
EncryptedContent = fernet.encrypt(Content)
#Writing Encrypted content to File
with open(f"{_File_basename}.zip","wb") as WriteEncrypted:
WriteEncrypted.write(EncryptedContent)
WriteEncrypted.close()
#check os and make dirs and save keys
if CheckOs() == 'Windows':
if os.path.exists(f'C:\\Users\\{getpass.getuser()}\\Desktop\\ANFU_KEYS') is False:
os.mkdir(f"C:\\Users\\{getpass.getuser()}\\Desktop\\ANFU_KEYS")
with open(f"C:\\Users\\{getpass.getuser()}\\Desktop\\ANFU_KEYS\\{_File_basename}.DIR.anky","wb") as WriteKey:
WriteKey.write(Encryption_key)
WriteKey.close()
else:
if os.path.exists(f"/home/{getpass.getuser()}/Desktop/ANFU_KEYS") is False:
os.mkdir(f"/home/{getpass.getuser()}/Desktop/ANFU_KEYS")
with open(f"/home/{getpass.getuser()}/Desktop/ANFU_KEYS/{_File_basename}.DIR.anky","wb") as WriteKey:
WriteKey.write(Encryption_key)
WriteKey.close()
#Removing the directory and moving the zip to that dir
shutil.rmtree(path)
shutil.move(f"{_File_basename}.zip",_root_dir)
except Exception as error:
if CheckOs() == 'Windows':
path = f'C:\\Users\\{getpass.getuser()}\\Desktop\\ANFU_ERROR_LOG.txt'
else:
path = f'/home/{getpass.getuser()}/Desktop/ANFU_ERROR_LOG.txt'
time_now = time.asctime( time.localtime(time.time()) )
_error_message = f'''
------------------------ERROR LOG,TIME:{time_now}--------------------------------
{error}
---------------------------------------------------------------------------------
'''
with open(path,'a') as write_error_log:
write_error_log.write(_error_message)
write_error_log.close()
print(f"[red]An Error Occured.\nError:\n{error}[/red]")

def DecryptFolder(self,keyfile,Encrypted_Dir):
try:
#Extracting root dir where zip file is present
_root_dir = os.path.dirname(Encrypted_Dir)
#Extracting Directory name from Encrypted Zip file
_zip_file_name = os.path.basename(Encrypted_Dir)
_dir_name = _zip_file_name.split(".")[0]
with open(keyfile,"rb") as KeyFile:
Key = KeyFile.read()
KeyFile.close()
#Using the Encryption Key
fernet = Fernet(Key)
#Opening the Encrypted File and Read it
with open(Encrypted_Dir,"rb") as Encrypted_Content:
Encrypted = Encrypted_Content.read()
Encrypted_Content.close()
#Decrypting the content
Decrypted = fernet.decrypt(Encrypted)
#Opening the file and writing the decrypted content
with open(Encrypted_Dir,"wb") as WriteDecrypted:
WriteDecrypted.write(Decrypted)
WriteDecrypted.close()
#Extracting the zip file to directory and deleting the zip
with ZipFile(Encrypted_Dir,"r") as ZipObj:
if CheckOs() == "Windows":
ZipObj.extractall(f"{_root_dir}\\{_dir_name}")
else:
ZipObj.extractall(f"{_root_dir}/{_dir_name}")
os.remove(Encrypted_Dir)
except Exception as error:
ErrWindow = Tk()
ErrWindow.title("ERROR")
Label(ErrWindow,text="An Error Occured While Decrypting the Directory").pack()
Label(ErrWindow,text="Possible Reasons might be:").pack()
Label(ErrWindow,text="1:Folder wasn't Encrypted,or was corrupted").pack()
Label(ErrWindow,text="2:Invalid Decryption Key").pack()
Label(ErrWindow,text="3:Permission Error").pack()
Label(ErrWindow,text=f"ERROR:\n{error}").pack()
ErrWindow.mainloop()
if CheckOs() == 'Windows':
path = f'C:\\Users\\{getpass.getuser()}\\Desktop\\ANFU_ERROR_LOG.txt'
else:
path = f'/home/{getpass.getuser()}/Desktop/ANFU_ERROR_LOG.txt'
time_now = time.asctime( time.localtime(time.time()) )
_error_message = f'''
------------------------ERROR LOG,TIME:{time_now}--------------------------------
{error}
---------------------------------------------------------------------------------
'''
with open(path,'a') as write_error_log:
write_error_log.write(_error_message)
write_error_log.close()
print("[red]An Error Occured.Possible Reasons:\n1:Permission Error\n2:Invalid Encryption Key[/red]")

# Tests
#op = FolderEncryption()
#op.EncryptFolder('C:\\Users\\Administrator\\Desktop\\My Work')
#op.DecryptFolder('C:\\Users\\Administrator\\Desktop\\ANFU_KEYS\\pp.DIR.anky','C:\\Users\\Administrator\\Desktop\\Programming\\Anfu\\core\\pp.zip')
1 change: 1 addition & 0 deletions core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#Empty __init__.py to recognize this folder as a module

0 comments on commit d61e7e8

Please sign in to comment.