Amazing-Python-Scripts
117 строк · 2.8 Кб
1from tkinter import Tk
2from tkinter import Label
3from tkinter import BOTTOM
4from tkinter import StringVar
5from tkinter import Entry
6from tkinter import Button
7import base64
8
9# initialize window
10root = Tk()
11root.geometry('500x300')
12root.resizable(0, 0)
13
14# title of the window
15root.title("Cryptography World")
16
17# label
18Label(root, text='ENCODE DECODE', font='arial 20 bold').pack()
19Label(root, text='By Anushka Chitranshi',
20font='arial 20 bold').pack(side=BOTTOM)
21
22# define variables
23Text = StringVar()
24private_key = StringVar()
25mode = StringVar()
26Result = StringVar()
27
28
29# function to encode
30def Encode(key, message):
31"""Encode the message."""
32enc = []
33for i in enumerate(message):
34key_c = key[i % len(key)]
35enc.append(chr((ord(message[i]) + ord(key_c)) % 256))
36return base64.urlsafe_b64encode("".join(enc).encode()).decode()
37
38
39# function to decode
40def Decode(key, message):
41"""Decode the message."""
42dec = []
43message = base64.urlsafe_b64decode(message).decode()
44for i in enumerate(message):
45key_c = key[i % len(key)]
46dec.append(chr((256 + ord(message[i]) - ord(key_c)) % 256))
47return "".join(dec)
48
49
50# function to set mode
51def Mode():
52"""Take mode of cryptography."""
53if mode.get() == 'e':
54Result.set(Encode(private_key.get(), Text.get()))
55elif mode.get() == 'd':
56Result.set(Decode(private_key.get(), Text.get()))
57else:
58Result.set('Invalid Mode')
59
60
61# Function to exit window
62def Exit():
63"""Exit the window."""
64root.destroy()
65
66
67# Function to reset
68def Reset():
69"""Reset the screen."""
70Text.set("")
71private_key.set("")
72mode.set("")
73Result.set("")
74
75
76Label(
77root, font='arial 12 bold', text='MESSAGE'
78).place(x=60, y=60)
79Entry(
80root, font='arial 10', textvariable=Text, bg='ghost white'
81).place(x=290, y=60)
82
83
84# key
85Label(root, font='arial 12 bold', text='KEY').place(x=60, y=90)
86Entry(
87root, font='arial 10', textvariable=private_key, bg='ghost white'
88).place(x=290, y=90)
89
90# mode
91Label(
92root, font='arial 12 bold', text='MODE(e-encode, d-decode)'
93).place(x=60, y=120)
94Entry(
95root, font='arial 10', textvariable=mode, bg='ghost white'
96).place(x=290, y=120)
97
98# result
99Entry(
100root, font='arial 10 bold', textvariable=Result, bg='ghost white'
101).place(x=290, y=150)
102
103# result button
104Button(
105root, font='arial 10 bold', text='RESULT', padx=2, bg='LightGray', command=Mode
106).place(x=60, y=150)
107
108# reset button
109Button(
110root, font='anson', text='RESET', width=6, command=Reset, bg='Green', padx=2
111).place(x=80, y=190)
112
113# exit button
114Button(
115root, font='anson', text='EXIT', width=6, command=Exit, bg='Red', padx=2, pady=2
116).place(x=180, y=190)
117root.mainloop()
118