From 795f2633888ab3e39acd0220b37941638bd3e237 Mon Sep 17 00:00:00 2001 From: piyushmohan01 Date: Tue, 12 Oct 2021 21:03:42 +0530 Subject: [PATCH 1/2] Added File-Encryptor-Decryptor files : #1034 --- .../File Encryptor Decryptor/README.md | 28 ++++++ .../decrypted/README.md | 10 ++ .../decrypted/dec-test.txt | 6 ++ .../encrypted/README.md | 5 + .../encrypted/enc-test.txt | 1 + .../file_encryptor_decryptor.py | 95 +++++++++++++++++++ .../original/README.md | 10 ++ .../original/test.txt | 6 ++ .../File Encryptor Decryptor/requirements.txt | 2 + 9 files changed, 163 insertions(+) create mode 100644 BasicPythonScripts/File Encryptor Decryptor/README.md create mode 100644 BasicPythonScripts/File Encryptor Decryptor/decrypted/README.md create mode 100644 BasicPythonScripts/File Encryptor Decryptor/decrypted/dec-test.txt create mode 100644 BasicPythonScripts/File Encryptor Decryptor/encrypted/README.md create mode 100644 BasicPythonScripts/File Encryptor Decryptor/encrypted/enc-test.txt create mode 100644 BasicPythonScripts/File Encryptor Decryptor/file_encryptor_decryptor.py create mode 100644 BasicPythonScripts/File Encryptor Decryptor/original/README.md create mode 100644 BasicPythonScripts/File Encryptor Decryptor/original/test.txt create mode 100644 BasicPythonScripts/File Encryptor Decryptor/requirements.txt diff --git a/BasicPythonScripts/File Encryptor Decryptor/README.md b/BasicPythonScripts/File Encryptor Decryptor/README.md new file mode 100644 index 000000000..44c0b598d --- /dev/null +++ b/BasicPythonScripts/File Encryptor Decryptor/README.md @@ -0,0 +1,28 @@ +## File Encryption and Decryption : + +### Description : +- The script lets you encrypt or decrypt a file. +- To do so, we use the [pycryptodome](https://pycryptodome.readthedocs.io/en/latest/) library. +- The user passes the function and also the file on which the same is to be performed. +- Once the parameters are passed, the encryptor needs to specify a password which is later required on the decryptor's side to decrypt the file. +- All passwords are hidden with asteriks using the [pwinput](https://github.com/asweigart/pwinput) library. + +### Try it out yourself : + +- Install libraries : ```pip install -r requirements.txt``` +- The parameters that are to be passed are : + * Operation : ```-e/-d``` for encryption/decryption [any one] + * File name : pass ```filename.ext``` after operation +- Place your file in the "original" folder. +- Once file.txt is encrypted, a new encrypted file called enc-file.txt is generated in the "encrypted" folder. +- The encrypted file enc-file.txt when decrypted, results in a new file called dec-file.txt (in the "decrypted" folder) +- Run the script : + * Encryption : ```python file_encryptor_decryptor.py -e file.jpg``` + * Decryption : ```python file_encryptor_decryptor.py -d enc-file.jpg``` + +### Sample results : +- Try opening the encrypted file enc-image.jpg from the encrypted folder. +- Now try opening the decrypted file dec-image.jpg from the decrypted folder. +- Both these files were originally generated from image.jpg. Compare the decrypted file and original file to see if they're the same. + +![Sample Results](https://i.imgur.com/SgERi2K.png) \ No newline at end of file diff --git a/BasicPythonScripts/File Encryptor Decryptor/decrypted/README.md b/BasicPythonScripts/File Encryptor Decryptor/decrypted/README.md new file mode 100644 index 000000000..d5ae14c86 --- /dev/null +++ b/BasicPythonScripts/File Encryptor Decryptor/decrypted/README.md @@ -0,0 +1,10 @@ +## Decrypted Folder : + +- The decrypted files will be generated in this folder. +- All decrypted files have "dec-" added to the start. (test.txt when decrypted becomes dec-test.txt) +- Objective of decryption is achieved when the decrypted file matches with the original file. + +## Sample Image : + +![Sample-Image](https://i.imgur.com/dYlGzNGm.jpg) +- *Source: Self generated* - [*Decrypted image*](https://i.imgur.com/dYlGzNG.jpg) \ No newline at end of file diff --git a/BasicPythonScripts/File Encryptor Decryptor/decrypted/dec-test.txt b/BasicPythonScripts/File Encryptor Decryptor/decrypted/dec-test.txt new file mode 100644 index 000000000..9ed924ea6 --- /dev/null +++ b/BasicPythonScripts/File Encryptor Decryptor/decrypted/dec-test.txt @@ -0,0 +1,6 @@ +1234 +1234 +1234 +1234 +1234 +1234 \ No newline at end of file diff --git a/BasicPythonScripts/File Encryptor Decryptor/encrypted/README.md b/BasicPythonScripts/File Encryptor Decryptor/encrypted/README.md new file mode 100644 index 000000000..0ff2ef98c --- /dev/null +++ b/BasicPythonScripts/File Encryptor Decryptor/encrypted/README.md @@ -0,0 +1,5 @@ +## Encrypted Folder : + +- The encrypted files will be generated in this folder. +- All encrypted files have "enc-" added to the start. (test.txt when encrypted becomes enc-test.txt) +- Objective of encryption is achieved when the file is encrypted and cannot be read. diff --git a/BasicPythonScripts/File Encryptor Decryptor/encrypted/enc-test.txt b/BasicPythonScripts/File Encryptor Decryptor/encrypted/enc-test.txt new file mode 100644 index 000000000..fd4d65485 --- /dev/null +++ b/BasicPythonScripts/File Encryptor Decryptor/encrypted/enc-test.txt @@ -0,0 +1 @@ +0000000000000034t)s={6p,pkh!?*O2g=LLsN31̮Y \ No newline at end of file diff --git a/BasicPythonScripts/File Encryptor Decryptor/file_encryptor_decryptor.py b/BasicPythonScripts/File Encryptor Decryptor/file_encryptor_decryptor.py new file mode 100644 index 000000000..3ccfea31a --- /dev/null +++ b/BasicPythonScripts/File Encryptor Decryptor/file_encryptor_decryptor.py @@ -0,0 +1,95 @@ +import os +import sys +import getopt +from pwinput import pwinput +from Crypto.Cipher import AES +from Crypto.Hash import SHA256 +from Crypto import Random + +# --- SET DEFAULTS --- +is_encrypt = False +is_decrypt = False +org_file_name = '' +argumentList = sys.argv[1:] +options = 'e:d:' + +# --- DEFINE ARG OPERATIONS --- +try: + args, values = getopt.getopt(argumentList, options) + for currentArgument, currentValue in args: + if currentArgument in ('-e'): + is_encrypt = True + org_file_name = currentValue + print('Encrypt :', currentValue) + if currentArgument in ('-d'): + is_decrypt = True + org_file_name = currentValue + print('Decrypt :', currentValue) +except getopt.error as e: + print(str(e)) + +# --- FUNCTION FOR ENCRYPTION --- +def encrypt(key, filename): + chunksize = 64*1024 + outputFile = "encrypted/enc-"+filename + filesize = str(os.path.getsize('original/'+filename)).zfill(16) + IV = Random.new().read(16) + encryptor = AES.new(key, AES.MODE_CBC, IV) + + with open('original/'+filename, 'rb') as infile: + with open(outputFile, 'wb') as outfile: + outfile.write(filesize.encode('utf-8')) + outfile.write(IV) + + while True: + chunk = infile.read(chunksize) + if len(chunk) == 0: + break + elif len(chunk) % 16 != 0: + chunk += b' ' * (16 - (len(chunk) % 16)) + outfile.write(encryptor.encrypt(chunk)) + +# --- FUNCTION FOR DECRYPTION --- +def decrypt(key, filename): + chunksize = 64*1024 + outputFile = "decrypted/dec-"+filename[4:] + + with open('encrypted/'+filename, 'rb') as infile: + filesize = int(infile.read(16)) + IV = infile.read(16) + decryptor = AES.new(key, AES.MODE_CBC, IV) + with open(outputFile, 'wb') as outfile: + while True: + chunk = infile.read(chunksize) + if len(chunk) == 0: + break + outfile.write(decryptor.decrypt(chunk)) + outfile.truncate(filesize) + +# --- PASSWORD HASHING --- +def getKey(password): + hasher = SHA256.new(password.encode('utf-8')) + return hasher.digest() + +def Main(): + + # --- ENCRYPT OPTION SELECTED --- + if is_encrypt: + filename = org_file_name + password = pwinput(prompt='Encryption Password : ') + encrypt(getKey(password), filename) + print('Encryption Done! Encrypted file in "encrypted" folder.') + + # --- DECRYPT OPTION SELECTED --- + elif is_decrypt: + filename = org_file_name + password = pwinput(prompt='Decryption Password : ') + decrypt(getKey(password), filename) + print('Decryption Done! Decrypted file in "decrypted" folder.') + + # --- INVALID OPTIONS PASSED --- + else: + print('Invalid Option! Closing...') + +if __name__ == '__main__': + Main() \ No newline at end of file diff --git a/BasicPythonScripts/File Encryptor Decryptor/original/README.md b/BasicPythonScripts/File Encryptor Decryptor/original/README.md new file mode 100644 index 000000000..35d08943a --- /dev/null +++ b/BasicPythonScripts/File Encryptor Decryptor/original/README.md @@ -0,0 +1,10 @@ +## Original Folder : + +- Place your original files in this folder. +- Files can be of various extensions (.png, .txt, .jpg, etc). +- You can use the test file provided in the folder or the sample image (provided below) for testing. + +## Sample Image : + +![Sample-Image](https://i.imgur.com/kU1evP5m.jpg) +- *Source: Self generated* - [*Test image*](https://i.imgur.com/kU1evP5.jpg) \ No newline at end of file diff --git a/BasicPythonScripts/File Encryptor Decryptor/original/test.txt b/BasicPythonScripts/File Encryptor Decryptor/original/test.txt new file mode 100644 index 000000000..9ed924ea6 --- /dev/null +++ b/BasicPythonScripts/File Encryptor Decryptor/original/test.txt @@ -0,0 +1,6 @@ +1234 +1234 +1234 +1234 +1234 +1234 \ No newline at end of file diff --git a/BasicPythonScripts/File Encryptor Decryptor/requirements.txt b/BasicPythonScripts/File Encryptor Decryptor/requirements.txt new file mode 100644 index 000000000..af7c7c669 --- /dev/null +++ b/BasicPythonScripts/File Encryptor Decryptor/requirements.txt @@ -0,0 +1,2 @@ +pwinput==1.0.2 +pycryptodome==3.10.1 \ No newline at end of file From 62edc97214e7b6881a95d5be4392d94cd27b48de Mon Sep 17 00:00:00 2001 From: Piyush Mohan <60737485+piyushmohan01@users.noreply.github.com> Date: Thu, 14 Oct 2021 20:55:27 +0530 Subject: [PATCH 2/2] Update README.md --- .../File Encryptor Decryptor/README.md | 63 ++++++++++++------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/BasicPythonScripts/File Encryptor Decryptor/README.md b/BasicPythonScripts/File Encryptor Decryptor/README.md index 44c0b598d..4440aeaaf 100644 --- a/BasicPythonScripts/File Encryptor Decryptor/README.md +++ b/BasicPythonScripts/File Encryptor Decryptor/README.md @@ -1,28 +1,43 @@ -## File Encryption and Decryption : +# File Encryptor and Decryptor + +## Aim : + +- To successfully encrypt sensitive files with a password. +- To decrypt them later with the password set by the encryptor. +- Do all the steps mentioned above, right from the terminal. + +## Purpose : + +The purpose of the script is to encrypt sensitive files from the terminal to achieve better security. + +## Short description of package/script : -### Description : - The script lets you encrypt or decrypt a file. - To do so, we use the [pycryptodome](https://pycryptodome.readthedocs.io/en/latest/) library. - The user passes the function and also the file on which the same is to be performed. -- Once the parameters are passed, the encryptor needs to specify a password which is later required on the decryptor's side to decrypt the file. -- All passwords are hidden with asteriks using the [pwinput](https://github.com/asweigart/pwinput) library. - -### Try it out yourself : - -- Install libraries : ```pip install -r requirements.txt``` -- The parameters that are to be passed are : - * Operation : ```-e/-d``` for encryption/decryption [any one] - * File name : pass ```filename.ext``` after operation -- Place your file in the "original" folder. -- Once file.txt is encrypted, a new encrypted file called enc-file.txt is generated in the "encrypted" folder. -- The encrypted file enc-file.txt when decrypted, results in a new file called dec-file.txt (in the "decrypted" folder) -- Run the script : - * Encryption : ```python file_encryptor_decryptor.py -e file.jpg``` - * Decryption : ```python file_encryptor_decryptor.py -d enc-file.jpg``` - -### Sample results : -- Try opening the encrypted file enc-image.jpg from the encrypted folder. -- Now try opening the decrypted file dec-image.jpg from the decrypted folder. -- Both these files were originally generated from image.jpg. Compare the decrypted file and original file to see if they're the same. - -![Sample Results](https://i.imgur.com/SgERi2K.png) \ No newline at end of file +- Once the parameters are passed, a password is required to encrypt or decrypt a file (set by the encryptor) +- All passwords are hidden with asterisk using the [pwinput](https://github.com/asweigart/pwinput) library. + +## Workflow of the Project : + +- When the script is run, it expects few parameters to be passed : function [```-e/-d```] and filename. +- Once the function and filename is defined, the script expects the user to input the password. +- The password along with the filename is passed on to the respective function [```encrypt()/decrypt()```] +- The encrypt function traces the file's path and creates an encrypted file with the password that was passed. +- The encrypted file is stored in the "encrypted" folder which is used by the decrypt function to trace the file. +- Decrypt function checks the input password and the password set earlier and stores the decrypted file in the decrypted folder. + +## Setup instructions : + +- Install required libraries : ```pip install -r requirements.txt``` +- Place your file in the original folder for the script to work on it. +- Encrypt your file : ```python file_encryptor_decryptor.py -e file.extension``` +- Decrypt encrypted file : ```python file_encryptor_decryptor.py -d enc-file.extension``` + +## Output : + +![Sample Results](https://i.imgur.com/SgERi2K.png) + +## Author(s) : + +- [Piyush Mohan](https://github.com/piyushmohan01)