Encryption And Decryption Of Files(part 2)

HOW TO ENCRYPT AND DECRYPT A FILE

HELLO,FRIENDS. I AM OLATUNJI DAPO RAPHAEL.I AM A CYBERSECURITY FELLOW AT 3MTT NIGERIA,I AM A TEACHER AND I AM ALSO A FULL STACK ENGINEER.I WANT TO SHOW YOU HOW TO MAKE AN APP THAT CAN ENCRYPT AND DECRYPT ANY FILE-WHETHER ITS A VIDEO,MUSIC,PHOTO,OR PDF FILE.

STEP TO MAKE THE ENCRYPT APP

I CALLED THIS APP "ENCRYPT".

STEP1 : YOU MUST INSTALL PYTHON CORRECTLY ON YOUR PC

STEP2: YOU MUST INSTALL ALL OTHER DEPENDENCIES AND MODULES ,WITHOUT EXCEPTION FOR THIS APP TO WORK(OTHER WISE THE APP WILL NOT WORK).THE MODULES ARE CRYPTOGRAPHY ,TKINTER ,AND OTHER DEPENDENCIES THAT ARE NEEDED

STEP3: CREATE A FOLDER ON YOUR PC USING YOUR CMD COMMAND LINE.

OPEN YOUR CMD. TYPE, mkdir encrypt

STEP4 :OPEN THE FOLDER WITH THE NAME encrypt WITH A VSCODE TEXT EDITOR CREATE A PYTHON FILE WITH THE "main.py" . COPY THE CODE BELOW AND PASTE INTO THE PYTHON FILE WHICH IS "main.py".SAVE THE FILE WITH CONTROL PLUS S COMMAND ON THE VSCODE EDITOR.

STEP5 .OPEN YOUR CMD COMMAND LINE ON YOUR PC.

STEP6.TYPE "cd encrypt" .(remember encrypt is the name of the folder that contains the python file-main.py

STEP 7.TYPE "python main.py

Please watch the video below to learn more .

from cryptography.fernet import Fernet
from tkinter import *
from tkinter import filedialog
from functools import partial

global filename
button_height = 2
button_width = 25

def browseFiles():
    browseFiles.filename = filedialog.askopenfilename(initialdir="/", title="Select a File",)
    label_file_explorer.configure(text="File Opened: " + browseFiles.filename)

    pass_label.pack()
    password.pack()
    temp_label.pack()
    button_encrypt.pack()
    button_decrypt.pack()

def encrypt_file(p_word):
    temp_key = p_word.get()
    temp_key = ''.join(e for e in temp_key if e.isalnum())
    key = temp_key + ("s" * (43 - len(temp_key)) + "=")

    fernet = Fernet(key)

    with open(browseFiles.filename, 'rb') as file:  original = file.read()
    encrypted = fernet.encrypt(original)

    with open(browseFiles.filename, 'wb') as encrypted_file:    encrypted_file.write(encrypted)

    status_label.configure(text="Encrypted")
    status_label.pack()

def decrypt_file(p_word):
    temp_key = p_word.get()
    temp_key = ''.join(e for e in temp_key if e.isalnum())
    key = temp_key + ("s" * (43 - len(temp_key)) + "=")

    fernet = Fernet(key)

    with open(browseFiles.filename, 'rb') as enc_file:  encrypted = enc_file.read()
    decrypted = fernet.decrypt(encrypted)

    with open(browseFiles.filename, 'wb') as dec_file:  dec_file.write(decrypted)

    status_label.configure(text="Decrypted")
    status_label.pack()


window = Tk()

window.title('File Explorer')
window.geometry("940x740")
window.config(background="black")

main_title = Label(window, text="File Encryptor and Decryptor", width=100, height=2, fg="white", bg="black",font =("",30))
passwd = StringVar()

submit_para_en = partial(encrypt_file, passwd)
submit_para_de = partial(decrypt_file, passwd)

credit = Label(window,text = "Developed by OLATUNJI DAPO R", bg="Green",height=2,  fg = "white", font =("",15))
label_file_explorer = Label(window, text="File Name : ", width=100, height=2, fg="white", bg="black",font =("",20))
pass_label = Label(window, text="Password for encryption/decryption : ", width=100, height=2, fg="white", bg="black",font =("",20))
temp_label = Label(window, text="", height=3, bg="black")

button_explore = Button(window, text="Browse File", command=browseFiles, width=button_width, height=button_height, font =("",15))

password = Entry(window, textvariable=passwd,show="*")

button_encrypt = Button(window, text="Encrypt", command=submit_para_en, width=button_width, height=button_height, font =("",15))
button_decrypt = Button(window, text="Decrypt", command=submit_para_de, width=button_width, height=button_height, font =("",15))

status_label = Label(window, text="", width=100, height=4, fg="white", bg="black",font =("",17))

credit.pack()
main_title.pack()
label_file_explorer.pack()
button_explore.pack()
window.mainloop()
6
Subscribe to my newsletter

Read articles from RAPHAEL DAPO OLATUNJI directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

RAPHAEL DAPO OLATUNJI
RAPHAEL DAPO OLATUNJI