Skip to content

Commit

Permalink
added file-encryptor example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgermduarte committed Nov 18, 2021
1 parent 8693014 commit 4f82b24
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@

[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/danstarns/project.md) [![Ask Me Anything !](https://img.shields.io/badge/Ask%20me-anything-1abc9c.svg)](https://github.com/danstarns/project.md/issues)

# file-encryptor
Encrypt and Decrypt files with file-encryptor.
A example project for encrypt and decrypt files.

# Tech Stack
- Python

# Contributing
All contribution's welcome 🍺 Make a PR or issue & let's take it from there.

# Features
- Encrypt file
- Decrypt file
- Generate Encryption key

# Project Dependencies
- cryptography

# Build
The project can be built creating a .exe file based on the program.py using pyinstaller.
If you want to test it out fell free for it !
86 changes: 86 additions & 0 deletions program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from cryptography.fernet import Fernet
import uuid

fernet = ""

def decryptfile():
location = input("Please provide the file location to decrypt: ")
try:
with open(location, 'rb') as enc_file:
encrypted = enc_file.read()
decrypted = fernet.decrypt(encrypted)
with open(location, 'wb') as dec_file:
dec_file.write(decrypted)
print("File decrypted successfully")
print("=============================================")
except:
print("Failed to decrypt the file. Please verify the file location or the privatekey provided.")
menu()

def encryptfile():
location = input("Please provide the file location to encrypt: ")
try:
with open(location, 'rb') as file:
original = file.read()
encrypted = fernet.encrypt(original)
with open(location, 'wb') as encrypted_file:
encrypted_file.write(encrypted)
print("File encrypted successfully");
print("=============================================")
except:
print("Failed to detect or encrypt the file provided -> " + location)
menu()

def menu():
print("0 - Exit");
print("1 - Encrypt File")
print("2 - Decrypt File")

user_option = input("Your option: ")
if(user_option == "0"):
return

switcher = {
"1": encryptfile,
"2": decryptfile
}
try:
switcher.get(user_option, "Invalid option")()
except:
print("Invalid option provided")

def readprivatekey():
location = input("Please provide your key location: ")
global fernet
try:
with open(location, 'rb') as filekey:
key = filekey.read()
fernet = Fernet(key)
print("Private key read successfully.");
menu()
except:
print("failed to read the private key provided.")
print("========================================")
initialize()

def initialize():
print("Please provide your option:\n")
print("1 - Generate key")
print("2 - Encrypt/Decrypt File")
user_option_input = input("Your option: ")
switcher = {
"1" : generatenewkey,
"2" : readprivatekey
}
try:
switcher.get(user_option_input,"invalid option provided")()
except:
print("invalid option provided")

def generatenewkey():
key = Fernet.generate_key()
file_name = str(uuid.uuid4())
with open(file_name +'_private.key', 'wb') as filekey:
filekey.write(key)

initialize()

0 comments on commit 4f82b24

Please sign in to comment.