|
1 | | -##importing mmodules |
2 | | - |
3 | | -from tkinter import * |
| 1 | +## importing mmodules |
| 2 | +from tkinter import Tk |
| 3 | +from tkinter import Label |
| 4 | +from tkinter import BOTTOM |
| 5 | +from tkinter import StringVar |
| 6 | +from tkinter import Entry |
| 7 | +from tkinter import Button |
4 | 8 | import base64
|
5 | 9 |
|
6 | | -#initialize window |
| 10 | +#initialize window |
7 | 11 | root = Tk()
|
8 | 12 | root.geometry('500x300')
|
9 | | -root.resizable(0,0) |
| 13 | +root.resizable(0,0) |
10 | 14 |
|
11 | | -#title of the window |
| 15 | +#title of the window |
12 | 16 | root.title("Cryptography World")
|
13 | 17 |
|
14 | | - |
15 | | - |
16 | | -#label |
17 | | - |
| 18 | +# label |
18 | 19 | Label(root, text ='ENCODE DECODE', font = 'arial 20 bold').pack()
|
19 | 20 | Label(root, text ='By Anushka Chitranshi', font = 'arial 20 bold').pack(side =BOTTOM)
|
20 | 21 |
|
21 | | - |
22 | | -#define variables |
23 | | - |
| 22 | +# define variables |
24 | 23 | Text = StringVar()
|
25 | 24 | private_key = StringVar()
|
26 | 25 | mode = StringVar()
|
27 | 26 | Result = StringVar()
|
28 | 27 |
|
29 | 28 |
|
30 | | -#######define function##### |
31 | | - |
32 | | -#function to encode |
33 | | - |
34 | | -def Encode(key,message): |
35 | | - enc=[] |
36 | | - for i in range(len(message)): |
| 29 | +## define function ## |
| 30 | +# function to encode |
| 31 | +def Encode(key, message): |
| 32 | + '''Takes the key and encode the message''' |
| 33 | + enc = [] |
| 34 | + for i in enumerate(message): |
37 | 35 | key_c = key[i % len(key)]
|
38 | 36 | enc.append(chr((ord(message[i]) + ord(key_c)) % 256))
|
39 | 37 |
|
40 | 38 | return base64.urlsafe_b64encode("".join(enc).encode()).decode()
|
41 | 39 |
|
42 | | -#function to decode |
43 | 40 |
|
44 | | -def Decode(key,message): |
45 | | - dec=[] |
| 41 | +# function to decode |
| 42 | +def Decode(key, message): |
| 43 | + '''Takes the key and decode the message''' |
| 44 | + dec = [] |
46 | 45 | message = base64.urlsafe_b64decode(message).decode()
|
47 | | - for i in range(len(message)): |
| 46 | + for i in enumerate(message): |
48 | 47 | key_c = key[i % len(key)]
|
49 | | - dec.append(chr((256 + ord(message[i])- ord(key_c)) % 256)) |
| 48 | + dec.append(chr ( (256 + ord(message[i])- ord(key_c ) ) % 256 ) ) |
50 | 49 |
|
51 | 50 | return "".join(dec)
|
52 | 51 |
|
53 | | -#function to set mode |
| 52 | + |
| 53 | +# function to set mode |
54 | 54 | def Mode():
|
55 | | - if(mode.get() == 'e'): |
| 55 | + ''' Takes the mode of cryptography''' |
| 56 | + if mode.get() == 'e': |
56 | 57 | Result.set(Encode(private_key.get(), Text.get()))
|
57 | | - elif(mode.get() == 'd'): |
| 58 | + elifmode.get() == 'd': |
58 | 59 | Result.set(Decode(private_key.get(), Text.get()))
|
59 | 60 | else:
|
60 | 61 | Result.set('Invalid Mode')
|
61 | 62 |
|
62 | | - |
63 | | - |
64 | | -#Function to exit window |
65 | | - |
| 63 | +# Function to exit window |
66 | 64 | def Exit():
|
| 65 | + '''Exit the window''' |
67 | 66 | root.destroy()
|
68 | 67 |
|
69 | | - |
70 | | -#Function to reset |
| 68 | + |
| 69 | +#Function to reset |
71 | 70 | def Reset():
|
| 71 | + ''' Used to reset the screen''' |
72 | 72 | Text.set("")
|
73 | 73 | private_key.set("")
|
74 | 74 | mode.set("")
|
75 | 75 | Result.set("")
|
76 | 76 |
|
77 | 77 |
|
78 | | -#################### Label and Button ############# |
79 | | - |
80 | | -#Message |
81 | | -Label(root, font= 'arial 12 bold', text='MESSAGE').place(x= 60,y=60) |
82 | | -Entry(root, font = 'arial 10', textvariable = Text, bg = 'ghost white').place(x=290, y = 60) |
83 | | - |
84 | | -#key |
85 | | -Label(root, font = 'arial 12 bold', text ='KEY').place(x=60, y = 90) |
86 | | -Entry(root, font = 'arial 10', textvariable = private_key , bg ='ghost white').place(x=290, y = 90) |
87 | | - |
88 | | - |
89 | | -#mode |
90 | | -Label(root, font = 'arial 12 bold', text ='MODE(e-encode, d-decode)').place(x=60, y = 120) |
91 | | -Entry(root, font = 'arial 10', textvariable = mode , bg= 'ghost white').place(x=290, y = 120) |
92 | | - |
93 | | - |
94 | | - |
95 | | -#result |
96 | | -Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='ghost white').place(x=290, y = 150) |
97 | | - |
98 | | -######result button |
99 | | -Button(root, font = 'arial 10 bold', text = 'RESULT' ,padx =2,bg ='LightGray' ,command = Mode).place(x=60, y = 150) |
100 | | - |
101 | | - |
102 | | -#reset button |
103 | | -Button(root, font = 'arial 10 bold' ,text ='RESET' ,width =6, command = Reset,bg = 'LimeGreen', padx=2).place(x=80, y = 190) |
104 | | - |
105 | | -#exit button |
106 | | -Button(root, font = 'arial 10 bold',text= 'EXIT' , width = 6, command = Exit,bg = 'OrangeRed', padx=2, pady=2).place(x=180, y = 190) |
| 78 | +## Label and Button ## |
| 79 | +# Message |
| 80 | +Label( |
| 81 | + root, |
| 82 | + font='arial 12 bold', |
| 83 | + text='MESSAGE' |
| 84 | +).place(x=60, y=60) |
| 85 | +Entry( |
| 86 | + root, |
| 87 | + font='arial 10', |
| 88 | + textvariable=Text, |
| 89 | + bg='ghost white' |
| 90 | +).place(x=290, y=60) |
| 91 | + |
| 92 | +# key |
| 93 | +Label(root, font='arial 12 bold', text='KEY').place(x=60, y=90) |
| 94 | +Entry( |
| 95 | + root, |
| 96 | + font='arial 10', |
| 97 | + textvariable=private_key , |
| 98 | + bg='ghost white' |
| 99 | +).place(x=290, y=90) |
| 100 | + |
| 101 | +# mode |
| 102 | +Label( |
| 103 | + root, |
| 104 | + font='arial 12 bold', |
| 105 | + text='MODE(e-encode, d-decode)' |
| 106 | +).place(x=60, y=120) |
| 107 | +Entry( |
| 108 | + root, |
| 109 | + font='arial 10', |
| 110 | + textvariable=mode , |
| 111 | + bg='ghost white' |
| 112 | +).place(x=290, y =120) |
| 113 | + |
| 114 | +# result |
| 115 | +Entry(root, |
| 116 | + font='arial 10 bold', |
| 117 | + textvariable=Result, |
| 118 | + bg='ghost white' |
| 119 | + ).place(x=290, y=150) |
| 120 | + |
| 121 | +# result button |
| 122 | +Button( |
| 123 | + root, |
| 124 | + font='arial 10 bold', |
| 125 | + text='RESULT' , |
| 126 | + padx=2, |
| 127 | + bg='LightGray' , |
| 128 | + command=Mode |
| 129 | +).place(x=60, y=150) |
| 130 | + |
| 131 | +# reset button |
| 132 | +Button( |
| 133 | + root, |
| 134 | + font='arial 10 bold' , |
| 135 | + text='RESET' , |
| 136 | + width=6, |
| 137 | + command=Reset, |
| 138 | + bg='LimeGreen', |
| 139 | + padx=2 |
| 140 | +).place(x=80, y=190) |
| 141 | + |
| 142 | +# exit button |
| 143 | +Button( |
| 144 | + root, |
| 145 | + font='arial 10 bold', |
| 146 | + text='EXIT' , |
| 147 | + width=6, |
| 148 | + command=Exit, |
| 149 | + bg='OrangeRed', |
| 150 | + padx=2, |
| 151 | + pady=2).place(x=180, y=190) |
107 | 152 | root.mainloop()
|
0 commit comments