Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions BasicPythonScripts/File Encryptor Decryptor/README.md
Original file line number Diff line number Diff line change
@@ -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)
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