-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar_cipher.py
85 lines (67 loc) · 2.38 KB
/
caesar_cipher.py
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
import tkinter as tk
from tkinter import messagebox
# Encryption/Decryption Function (same for both)
def caesar_cipher(text, s):
result = ""
for i in range(len(text)):
char = text[i]
# Encrypt/Decrypt uppercase characters
if char.isupper():
result += chr((ord(char) + s - 65) % 26 + 65)
# Encrypt/Decrypt lowercase characters
else:
result += chr((ord(char) + s - 97) % 26 + 97)
return result
# Function to handle encryption
def encrypt_text():
text = entry_text.get()
try:
s = int(entry_shift.get())
except ValueError:
messagebox.showerror("Input Error", "Shift value must be an integer.")
return
if s < 0 or s > 25:
messagebox.showerror("Input Error", "Shift value must be between 0 and 25.")
return
encrypted_text = caesar_cipher(text, s)
label_result.config(text=f"Encrypted Text: {encrypted_text}")
# Function to handle decryption
def decrypt_text():
text = entry_text.get()
try:
s = int(entry_shift.get())
except ValueError:
messagebox.showerror("Input Error", "Shift value must be an integer.")
return
if s < 0 or s > 25:
messagebox.showerror("Input Error", "Shift value must be between 0 and 25.")
return
decrypted_text = caesar_cipher(text, 26 - s)
label_result.config(text=f"Decrypted Text: {decrypted_text}")
# Create the main window
window = tk.Tk()
window.title("Caesar Cipher")
window.geometry("800x500")
# Label for instructions
label_instruction = tk.Label(window, text="Enter the text and shift value for encryption/decryption:")
label_instruction.pack(pady=10)
# Text entry field
label_text = tk.Label(window, text="Text:")
label_text.pack()
entry_text = tk.Entry(window, width=40)
entry_text.pack()
# Shift value entry field
label_shift = tk.Label(window, text="Shift Value (0-25):")
label_shift.pack()
entry_shift = tk.Entry(window, width=5)
entry_shift.pack()
# Buttons for encryption and decryption
button_encrypt = tk.Button(window, text="Encrypt", command=encrypt_text)
button_encrypt.pack(pady=10)
button_decrypt = tk.Button(window, text="Decrypt", command=decrypt_text)
button_decrypt.pack(pady=10)
# Label to display the result
label_result = tk.Label(window, text="Result will be shown here")
label_result.pack(pady=20)
# Start the Tkinter event loop
window.mainloop()