Repository navigation
Expand file tree
/
Copy pathMessageEncodeDecode.py
More file actions
100 lines (78 loc) · 3.4 KB
/
Copy pathMessageEncodeDecode.py
File metadata and controls
100 lines (78 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""DataCrypter - a small Tkinter app to encode and decode messages with a key.
Encoding adds each character of the key (repeated as needed) to each character
of the message modulo 256, then Base64 (URL-safe) encodes the result. Decoding
reverses these steps with the same key.
This is a simple obfuscation technique, not strong encryption.
"""
import base64
import binascii
import tkinter as tk
def Encode(key, message):
"""Encode ``message`` using ``key`` and return a URL-safe Base64 string."""
if not key:
raise ValueError('A key is required.')
enc = []
for i in range(len(message)):
key_c = key[i % len(key)]
enc.append(chr((ord(message[i]) + ord(key_c)) % 256))
return base64.urlsafe_b64encode("".join(enc).encode()).decode()
def Decode(key, message):
"""Decode a string produced by :func:`Encode` using the same ``key``."""
if not key:
raise ValueError('A key is required.')
dec = []
message = base64.urlsafe_b64decode(message).decode()
for i in range(len(message)):
key_c = key[i % len(key)]
dec.append(chr((256 + ord(message[i]) - ord(key_c)) % 256))
return "".join(dec)
def main():
"""Build and run the Tkinter user interface."""
root = tk.Tk()
root.geometry('500x300')
root.resizable(False, False)
root.title("DataCrypter - Message Encode and Decode")
tk.Label(root, text='ENCODE DECODE', font='arial 20 bold').pack()
tk.Label(root, text='Diganta Datta', font='arial 20 bold').pack(side=tk.BOTTOM)
text = tk.StringVar()
private_key = tk.StringVar()
mode = tk.StringVar()
result = tk.StringVar()
def run_mode():
selected = mode.get().strip().lower()
if selected not in ('e', 'd'):
result.set('Invalid Mode')
return
if not private_key.get():
result.set('Key is required')
return
try:
if selected == 'e':
result.set(Encode(private_key.get(), text.get()))
else:
result.set(Decode(private_key.get(), text.get()))
except (binascii.Error, UnicodeDecodeError, ValueError):
result.set('Invalid input for decoding')
def exit_app():
root.destroy()
def reset():
text.set("")
private_key.set("")
mode.set("")
result.set("")
tk.Label(root, font='arial 12 bold', text='MESSAGE').place(x=60, y=60)
tk.Entry(root, font='arial 10', textvariable=text, bg='ghost white').place(x=290, y=60)
tk.Label(root, font='arial 12 bold', text='KEY').place(x=60, y=90)
tk.Entry(root, font='arial 10', textvariable=private_key, bg='ghost white').place(x=290, y=90)
tk.Label(root, font='arial 12 bold', text='MODE(e-encode, d-decode)').place(x=60, y=120)
tk.Entry(root, font='arial 10', textvariable=mode, bg='ghost white').place(x=290, y=120)
tk.Entry(root, font='arial 10 bold', textvariable=result, bg='ghost white').place(x=290, y=150)
tk.Button(root, font='arial 10 bold', text='RESULT', padx=2, bg='LightGray',
command=run_mode).place(x=60, y=150)
tk.Button(root, font='arial 10 bold', text='RESET', width=6, command=reset,
bg='LimeGreen', padx=2).place(x=80, y=190)
tk.Button(root, font='arial 10 bold', text='EXIT', width=6, command=exit_app,
bg='OrangeRed', padx=2, pady=2).place(x=180, y=190)
root.mainloop()
if __name__ == '__main__':
main()