Skip to content

Instantly share code, notes, and snippets.

@fastfingertips
Created March 13, 2024 09:59
Show Gist options
  • Save fastfingertips/62c85e6b78e6ae1bdd75562d4d3a7b73 to your computer and use it in GitHub Desktop.
Save fastfingertips/62c85e6b78e6ae1bdd75562d4d3a7b73 to your computer and use it in GitHub Desktop.
This application allows users to save and encrypt their secret notes using a master key. It provides functionalities to both save notes by encrypting them and decrypt previously encrypted notes using the same master key.
from tkinter import PhotoImage, messagebox, Canvas, Button, Entry, Label, Text, END, Tk
import base64
def encode(key, clear):
"""
Encrypts the given text using Vigenere cipher with the provided key.
Args:
key (str): The encryption key.
clear (str): The text to be encrypted.
Returns:
str: The encrypted text.
"""
enc = []
for i in range(len(clear)):
key_c = key[i % len(key)]
enc_c = chr((ord(clear[i]) + ord(key_c)) % 256)
enc.append(enc_c)
return base64.urlsafe_b64encode("".join(enc).encode()).decode()
def decode(key, enc):
"""
Decrypts the given text using Vigenere cipher with the provided key.
Args:
key (str): The decryption key.
enc (str): The text to be decrypted.
Returns:
str: The decrypted text.
"""
dec = []
enc = base64.urlsafe_b64decode(enc).decode()
for i in range(len(enc)):
key_c = key[i % len(key)]
dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
def save_and_encrypt_notes():
"""
Saves and encrypts the entered notes using the provided master key.
"""
title = title_entry.get()
message = input_text.get("1.0", END)
master_secret = master_secret_input.get()
if len(title) and len(message) and len(master_secret):
message_encrypted = encode(master_secret, message)
try:
with open("mysecret.txt", "a") as data_file:
data_file.write(f'\n{title}\n{message_encrypted}')
except FileNotFoundError:
with open("mysecret.txt", "w") as data_file:
data_file.write(f'\n{title}\n{message_encrypted}')
finally:
title_entry.delete(0, END)
master_secret_input.delete(0, END)
input_text.delete("1.0", END)
else:
messagebox.showinfo(title="Error!", message="Please enter all information.")
def decrypt_notes():
"""
Decrypts the entered encrypted notes using the provided master key.
"""
message_encrypted = input_text.get("1.0", END)
master_secret = master_secret_input.get()
if len(message_encrypted) == 0 or len(master_secret) == 0:
messagebox.showinfo(title="Error!", message="Please enter all information.")
else:
try:
decrypted_message = decode(master_secret, message_encrypted)
input_text.delete("1.0", END)
input_text.insert("1.0", decrypted_message)
except:
messagebox.showinfo(title="Error!", message="Please make sure of encrypted info.")
# UI setup
window = Tk()
window.title("Secret Notes")
window.config(padx=30, pady=30)
canvas = Canvas(height=200, width=200)
logo = PhotoImage(file="topsecret.png")
canvas.create_image(100, 100, image=logo)
title_info_label = Label(text="Enter your title", font=("Verdena", 20, "normal"))
title_entry = Entry(width=30)
input_info_label = Label(text="Enter your secret", font=("Verdena", 20, "normal"))
input_text = Text(width=50, height=25)
master_secret_label = Label(text="Enter master key", font=("Verdena", 20, "normal"))
master_secret_input = Entry(width=30)
save_button = Button(text="Save & Encrypt", command=save_and_encrypt_notes)
decrypt_button = Button(text="Decrypt", command=decrypt_notes)
canvas.pack()
title_info_label.pack()
title_entry.pack()
input_info_label.pack()
input_text.pack()
master_secret_label.pack()
master_secret_input.pack()
save_button.pack()
decrypt_button.pack()
window.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment