Python Security Encryption/Decryption Software

Introduction: Python Security Encryption/Decryption Software

In this Instructable I will show you how with some simple Python, you can keep your files secure using industry standard AES.


- Python 3.7

- PyAesCrypt library

- hashlib library

If you do not have these libraries, you can install easily by typing:

pip3 install hashlib

pip3 install PyAesCrypt

in the terminal (or CMD)

You should already have these:

- random library

- os library

- system library

I am using OS X, but it shouldn't matter too much, except for the direction of the slashes in the file paths (OS X: /, Windows: \)

Please note: Because of some glitch, the indentations in the code don't show for some reason. There will consequently be no indentations in the displayed code, however they are present in the Python files I have attached at the end, and in the enclosed pictures. Just don't take the code directly from the displayed text, because it will not work due to the lack of indentations.

If you have all the dependancies installed, let's move on to Step 1.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Writing the Setup File

One of the factors that makes this so secure is the use of hashes to check the password. The setup file (I am calling mine is going to:

- Create a folder and dummy files for the password

- Set the password

- Set the file number

- Hash the password

First off, we are going to import our dependancies:

from sys import *

import os

import random

import hashlib

Next we will create a folder to house the password hash and the dummy files:

if not os.path.exists('desktop/safesetup'):


except OSError:

print("Error creating folder")

This code will create a folder called safesetup (unless it already exists).

After this, we are going to set the password, and generate a random number between 1 and 100 as our way to navigate the dummy files:

global password
password = argv[1].encode('utf-8')

n = random.randint(1, 101)

Now that we have our password and our file number, we are going to create 99 dummy files inside safesetup, and one real file that will contain our password hash:

for x in range(101):
if(x != n):

f = open(("desktop/safesetup/"+str(x)), "w+")



password = hashlib.sha256(password).hexdigest()

f = open(("desktop/safesetup/"+str(x)), "w+")




The real file is called whatever integer n is. This file contains our password, after being hashed using the sha256 algorithm (this hash algorithm is used widely in cryptocurrencies, most notably Bitcoin).

Remember what n is (it will be printed in the console), as it is just as important as the password.

That's all we need for our setup program, so we will now move onto the encryption/decryption program.

Step 2: The Encryption/decryption File

The setup section of the main file imports the dependancies, hashes the inputted password, and retrieves the real password hash using the inputted file number.

First, the dependancies:

from sys import *
import os

import pyAesCrypt

import hashlib

Next, the hashing of the inputted password:

password = argv[1].encode('utf-8')
password = hashlib.sha256(password).hexdigest()

Finally, the hashed password retrieval:

file_key = str(argv[2])
hash = open(("desktop/safesetup/" + file_key), ("r+")).read()

The second section of the encryption file compares the hashes, determines the verity of the comparison, and uses the AESCrypt python library to encrypt or decrypt your file of choice. This is a rather large chunk of code, but I will break it down:

if(password == hash):
print("Password accepted")

bufferSize = 64 * 1024

operation = str(input("Are you retrieving or encrypting files? (r or e) "))

if(operation == 'r'):

file_name = str(input("File to retrieve: "))

pyAesCrypt.decryptFile((file_name + ".aes"), file_name, password, bufferSize)

os.remove((file_name + ".aes"))

elif(operation == 'e'):

file_name = str(input("File to encrypt: "))

pyAesCrypt.encryptFile(file_name, (file_name + ".aes"), password, bufferSize)



print("Error: Incorrect input")


print("Access denied")

The first if statement determines whether the hashed passwords match. If they do, it then goes on to ask if you want to encrypt files or retrieve encrypted files. Depending on your input, it will either encrypt or decrypt the provided file. When prompted to give the name of the file, make sure to specify the path unless the file is in the same directory as the python program. The program deletes the file in its previous state, replacing it with an encrypted .aes file, or decrypting it and replacing it with the original file.

In the future, I might update this to include facial recognition using the Python OpenCV library, but for now passwords will have to suffice.

Step 3: Operation of Files

To run the setup file, follow these steps:

1. Type in the terminal:

python3 directory/ password (replacing directory, setupname, and password with their respective values)

2. The terminal will output your file number. Keep this.

To run the encryption/decryption program, follow these steps:

1. Type in the terminal:

python3 directory/ password filenumber (replacing directory, filename, password, and filenumber with their respective values)

2. The terminal will then either accept or reject your password. If rejected, try again and make sure you type in the right values. Once access has been granted, the terminal will ask you if you want to encrypt a file or retrieve a file. To encrypt a file, type e, and to retrieve an encrypted file, type r.

3. You will then be asked to provide the file name. Remember to provide the directory of the file as well as the name, and also the file extension. However, if you are decrypting a file, do not type the .aes part of the extension, as the code accounts for that.

4. The program then encrypts or decrypts the provided file, and deletes the file in its previous state (retaining the encrypted or decrypted file).

Voila! Thanks for making it this far into the instructable, I know reading code tutorials is not the most entertaining thing. The python files are enclosed in this step, for those of you that want to give this a shot. Once again, thanks for reading, and I wish you the best of luck in your future coding endeavours.

Safe and Secure Challenge

Participated in the
Safe and Secure Challenge

Be the First to Share


    • Backyard Contest

      Backyard Contest
    • Silly Hats Speed Challenge

      Silly Hats Speed Challenge
    • Finish It Already Speed Challenge

      Finish It Already Speed Challenge