Amazing-Python-Scripts

Форк
0
117 строк · 2.8 Кб
1
from tkinter import Tk
2
from tkinter import Label
3
from tkinter import BOTTOM
4
from tkinter import StringVar
5
from tkinter import Entry
6
from tkinter import Button
7
import base64
8

9
# initialize window
10
root = Tk()
11
root.geometry('500x300')
12
root.resizable(0, 0)
13

14
# title of the window
15
root.title("Cryptography World")
16

17
# label
18
Label(root, text='ENCODE DECODE', font='arial 20 bold').pack()
19
Label(root, text='By Anushka Chitranshi',
20
      font='arial 20 bold').pack(side=BOTTOM)
21

22
# define variables
23
Text = StringVar()
24
private_key = StringVar()
25
mode = StringVar()
26
Result = StringVar()
27

28

29
# function to encode
30
def Encode(key, message):
31
    """Encode the message."""
32
    enc = []
33
    for i in enumerate(message):
34
        key_c = key[i % len(key)]
35
        enc.append(chr((ord(message[i]) + ord(key_c)) % 256))
36
    return base64.urlsafe_b64encode("".join(enc).encode()).decode()
37

38

39
# function to decode
40
def Decode(key, message):
41
    """Decode the message."""
42
    dec = []
43
    message = base64.urlsafe_b64decode(message).decode()
44
    for i in enumerate(message):
45
        key_c = key[i % len(key)]
46
        dec.append(chr((256 + ord(message[i]) - ord(key_c)) % 256))
47
    return "".join(dec)
48

49

50
# function to set mode
51
def Mode():
52
    """Take mode of cryptography."""
53
    if mode.get() == 'e':
54
        Result.set(Encode(private_key.get(), Text.get()))
55
    elif mode.get() == 'd':
56
        Result.set(Decode(private_key.get(), Text.get()))
57
    else:
58
        Result.set('Invalid Mode')
59

60

61
# Function to exit window
62
def Exit():
63
    """Exit the window."""
64
    root.destroy()
65

66

67
# Function to reset
68
def Reset():
69
    """Reset the screen."""
70
    Text.set("")
71
    private_key.set("")
72
    mode.set("")
73
    Result.set("")
74

75

76
Label(
77
    root, font='arial 12 bold', text='MESSAGE'
78
).place(x=60, y=60)
79
Entry(
80
    root, font='arial 10', textvariable=Text, bg='ghost white'
81
).place(x=290, y=60)
82

83

84
# key
85
Label(root, font='arial 12 bold', text='KEY').place(x=60, y=90)
86
Entry(
87
    root, font='arial 10', textvariable=private_key, bg='ghost white'
88
).place(x=290, y=90)
89

90
# mode
91
Label(
92
    root, font='arial 12 bold', text='MODE(e-encode, d-decode)'
93
).place(x=60, y=120)
94
Entry(
95
    root, font='arial 10', textvariable=mode, bg='ghost white'
96
).place(x=290, y=120)
97

98
# result
99
Entry(
100
    root, font='arial 10 bold', textvariable=Result, bg='ghost white'
101
).place(x=290, y=150)
102

103
# result button
104
Button(
105
    root, font='arial 10 bold', text='RESULT', padx=2, bg='LightGray', command=Mode
106
).place(x=60, y=150)
107

108
# reset button
109
Button(
110
    root, font='anson', text='RESET', width=6, command=Reset, bg='Green', padx=2
111
).place(x=80, y=190)
112

113
# exit button
114
Button(
115
    root, font='anson', text='EXIT', width=6, command=Exit, bg='Red', padx=2, pady=2
116
).place(x=180, y=190)
117
root.mainloop()
118

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.