diff --git a/README.md b/README.md index 9c76f96..6828b73 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This is a Python Module For Encryption, Hashing And Other Basic Stuff You Need, With Secure Encryption And Strong Salted Hashing You Can Do Whatever You Want To ## Installation EnroCrypt Is Avaliable On PyPi You Can Install It As Follows:
-```$ pip -m install enrocrypt``` +```$ pip install -U enrocrypt``` ## Manual Installation If For Some Reason You Can't Install EnroCrypt From PyPi You Can Download It Manually Too: * Clone This Repo @@ -14,6 +14,7 @@ After Following All The Steps Mentioned Above (If You Don't Get An Error) EnroCr ## Features * Strong Encryption * Strong Salted Hashing +* File Encryption-Decryption * Some Basic Functions ## Usage ```python diff --git a/enrocrypt/encryption.py b/enrocrypt/encryption.py index 528a703..5f1230e 100644 --- a/enrocrypt/encryption.py +++ b/enrocrypt/encryption.py @@ -1,4 +1,4 @@ -from .error import Error +from enrocrypt.error import Error def _Keyencryption(key:bytes): import base64 bases = base64.standard_b64encode(key) @@ -54,4 +54,43 @@ def Decrypt_List(Data:list): d_key = _Keydecryption(Key) d_data = _datadecryption(e_Data) based = _BaseDecryption(d_key,d_data) - return based \ No newline at end of file + return based +def FileEncryption(Path:str,KeyFilePath:str): + '''Make Sure The Path You Give Has "\\" insted of just "\". KeyFilePath Takes the path where you have to keep the key for the encrypted file, Path Takes the path of the file which has to be encrypted + Note: You Can Use The Key File To Store Multiple Keys But The Keys Must Not Be of The Same File Else You Will Get A Wrong Key Error''' + file1 = open(Path,'r') + data = (file1.read()).encode() + with open(Path,'w') as file: + e_file = Encrypt(bytes(data)) + key = str(e_file[1]) + e_data = str(e_file[3]) + file.write(e_data) + if KeyFilePath is not None: + with open(KeyFilePath,'a') as keyf: + keyf.write(Path+':'+';'+':'+';') + keyf.write(key) + keyf.write('\n') + if KeyFilePath is None: + Error.NoKeyFile() + file1.close() +def FileDecryption(Path:str,KeyFilePath:str): + '''Path: The Path Of The Encrypted File + KeyFilePath: Path Of That key File Where The Decryption Key Is Stored For The File ''' + import ast + file1 = open(Path,'r') + if KeyFilePath is not None: + with open(KeyFilePath,'r') as keyf: + e_data = ast.literal_eval(file1.read()) + alls = keyf.read() + splited =alls.split('\n') + for char,i in enumerate(splited): + a = i.split(':;:;') + if a[char] == Path: + key = ast.literal_eval(a[char+1]) + break + n_data = str(Decrypt(key,e_data).decode()) + file2 = open(Path,'w') + file2.write(n_data) + + if KeyFilePath is None: + Error.NoKeyFile() \ No newline at end of file diff --git a/enrocrypt/error.py b/enrocrypt/error.py index d2a6a13..c210a7b 100644 --- a/enrocrypt/error.py +++ b/enrocrypt/error.py @@ -3,3 +3,9 @@ def ModifiedError(): raise ValueError('The List Provided To The Function Is Modified') def ListIndexError(): raise IndexError('Returned List Must Only Have 4 Elements') + def NoKeyFile(): + raise ValueError('No Path For The Key File was Provided') + def WrongKey(): + from cryptography.fernet import InvalidToken + raise InvalidToken('The Key Provided Is Not Correct') +