-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchiff_Cesar.py
82 lines (63 loc) · 2.31 KB
/
chiff_Cesar.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
import tkinter as tk
def caesar_cipher(message, key, mode):
result = ""
if mode == "decrypt":
key = -key
for char in message:
if char.isalpha():
ascii_val = ord(char)
shifted_ascii_val = ascii_val + key
if char.islower():
if shifted_ascii_val < ord("a"):
shifted_ascii_val += 26
elif shifted_ascii_val > ord("z"):
shifted_ascii_val -= 26
elif char.isupper():
if shifted_ascii_val < ord("A"):
shifted_ascii_val += 26
elif shifted_ascii_val > ord("Z"):
shifted_ascii_val -= 26
result += chr(shifted_ascii_val)
else:
result += char
return result
def encrypt_message():
message = entry.get()
key = int(key_entry.get())
encrypted_message = caesar_cipher(message, key, "encrypt")
output.config(state="normal")
output.delete(1.0, tk.END)
output.insert(tk.END, encrypted_message)
output.config(state="disabled")
def decrypt_message():
message = entry.get()
key = int(key_entry.get())
decrypted_message = caesar_cipher(message, key, "decrypt")
output.config(state="normal")
output.delete(1.0, tk.END)
output.insert(tk.END, decrypted_message)
output.config(state="disabled")
# Créer l'interface graphique
window = tk.Tk()
window.title("Chiffrement de César")
# Entrée pour le message
label = tk.Label(window, text="Message à chiffrer/déchiffrer :")
label.grid(row=0, column=0)
entry = tk.Entry(window)
entry.grid(row=0, column=1)
# Entrée pour la clé
key_label = tk.Label(window, text="Clé de chiffrement/déchiffrement :")
key_label.grid(row=1, column=0)
key_entry = tk.Entry(window)
key_entry.grid(row=1, column=1)
# Boutons de chiffrement et de déchiffrement
encrypt_button = tk.Button(window, text="Chiffrer", command=encrypt_message)
encrypt_button.grid(row=2, column=0)
decrypt_button = tk.Button(window, text="Déchiffrer", command=decrypt_message)
decrypt_button.grid(row=2, column=1)
# Sortie pour le résultat
output_label = tk.Label(window, text="Résultat :")
output_label.grid(row=3, column=0)
output = tk.Text(window, height=5, width=50, state="disabled")
output.grid(row=4, column=0, columnspan=2)
window.mainloop()