Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions BasicPythonScripts/File Encryptor Decryptor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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 :

- 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, 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)
10 changes: 10 additions & 0 deletions BasicPythonScripts/File Encryptor Decryptor/decrypted/README.md
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1234
1234
1234
1234
1234
1234
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000000000000034t��)�s={�6p��,�p�k�h!��?*�O��2g���=�L�L�s�N�3��1�̮�Y���
Original file line number Diff line number Diff line change
@@ -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()
10 changes: 10 additions & 0 deletions BasicPythonScripts/File Encryptor Decryptor/original/README.md
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions BasicPythonScripts/File Encryptor Decryptor/original/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1234
1234
1234
1234
1234
1234
2 changes: 2 additions & 0 deletions BasicPythonScripts/File Encryptor Decryptor/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pwinput==1.0.2
pycryptodome==3.10.1