Skip to content

Instantly share code, notes, and snippets.

@hasanparasteh
Created June 4, 2019 13:28
Show Gist options
  • Select an option

  • Save hasanparasteh/8733c43defff27969c47412522c94a2c to your computer and use it in GitHub Desktop.

Select an option

Save hasanparasteh/8733c43defff27969c47412522c94a2c to your computer and use it in GitHub Desktop.
key module is an encryption/decryption platform for your daily usage.
# -*- coding: utf-8 -*-
"""key module is an encryption/decryption platform for your daily usage.
This module helps people protect they personal files from hackers, unauthorized people and etc. We protect your files
by using AES 256 encryption methods which no one would break. This program won't cache your passwords or backup your files
after encryption so all the responsibility of losing any data or anythong like that is yours...
Example:
key = Key("passowrd")
key.run(['filepath', 'another filepath'])
* You need to run this in your command line before running the module : $ pip install pyAesCrypt
Todo:
Add other encryption methods to program
"""
import pyAesCrypt as AesEncrypt
import os
from tkinter import messagebox
class Key:
""" Encrypt and decrypt files based on the file type in AES 256 methods.
Parameters:
password (str) : used for encryption and decryption
buffersize (int) : genrated 64k buffersize of file
encrypted_extention (str) : file extension to add on after encryption.
"""
def __init__(self, password):
self.buffersize = 64 * 1024
self.password = password
self.encrypted_extention = ".encrypted"
def is_encrypted(self, file):
""" Check if a file is encrypted or not by using self.encrypted_extention.
Parameters:
file (str) : the file location path
Returns:
bool : the return value: True for encrypted files and false for un-encrypted!
"""
if self.encrypted_extention in file:
return True
else:
return False
def remove_extention_from_file(self, file):
""" Removes the self.encrypted_extention after the filename.
Parameters:
file (str) : the file location path
Returns:
org_name (str) : the original filename before encryption
"""
org_name = file.replace(self.encrypted_extention, "")
return org_name
def remove_file(self, file):
"""removes file using os module"""
os.remove(file)
def processor(self, file, password, method):
""" Do the encryption process based on the method that it gives.
Parameters:
file (str) : the file location path
password (str) : the password for encryption/decryption
method (char) : a character to decide what the function do
Returns:
None : if method isn't the excepted value
"""
if method == "E" or method == "e":
AesEncrypt.encryptFile(file, (file + self.encrypted_extention), password, self.buffersize)
elif method == "D" or method == "d":
AesEncrypt.decryptFile(file, self.remove_extention_from_file(file), password, self.buffersize)
else:
return None
def encrypt_file(self, file):
""" Encrypt files with given file path and remove the un-encrypted file after.
Parameters:
file (str) : the file location path
Returns:
bool : the return value : True if nothing goes wrong and false for exceptions
"""
try:
self.processor(file, self.password, "E")
self.remove_file(file)
return True
except:
return False
def decrypt_file(self, file):
""" Decrypt files with given file path and remove the un-encrypted file after.
Parameters:
file (str) : the file location path
Returns:
bool : the return value : True if nothing goes wrong and false for exceptions
"""
try:
self.processor(file, self.password, "D")
self.remove_file(file)
return True
except:
return False
def run(self, file):
""" Run all of the functions and do tasks one by one to achive the purpose of the module.
* : You should only run this function in order to encrypt/decrypt your data!
Parameters:
file (str) : the file location path
Returns:
str : shows the info to the user
"""
if self.is_encrypted(file):
if self.decrypt_file(file):
return "all files were decrypted!"
else:
if self.encrypt_file(file):
return "all files were encrypted!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment