Skip to content

Instantly share code, notes, and snippets.

@etahamad
Created May 9, 2023 19:14
Show Gist options
  • Save etahamad/e6f9cc484d104c71269e002102a8b916 to your computer and use it in GitHub Desktop.
Save etahamad/e6f9cc484d104c71269e002102a8b916 to your computer and use it in GitHub Desktop.
import tkinter as tk
from tkinter import messagebox
import re
def playfair_encrypt(plainText, key):
# Remove non-alphabetic characters from plaintext and key
plainText = re.sub("[^a-z]", "", plainText.lower().replace('j', 'i'))
key = re.sub("[^a-z]", "", key.lower().replace('j', 'i'))
# Check if plaintext and key are empty after removing non-alphabetic characters
if not plainText:
messagebox.showerror('Error', 'Plaintext must contain at least one alphabetic character')
return ''
if not key:
messagebox.showerror('Error', 'Key must contain at least one alphabetic character')
return ''
# Check that all characters in plaintext and key are allowed in the Playfair cipher
allowed_chars = 'abcdefghiklmnopqrstuvwxyz'
for c in plainText + key:
if c not in allowed_chars:
messagebox.showerror('Error', f'Character "{c}" is not allowed in the Playfair cipher')
return ''
# Create Playfair matrix
key = key.replace('j', 'i')
key += allowed_chars
matrix = []
for c in key:
if c not in matrix:
matrix.append(c)
# Split plaintext into digraphs
digraphs = re.findall('[a-z][a-z]', plainText)
if len(digraphs) * 2 < len(plainText):
digraphs.append(plainText[-1] + 'x')
# Encrypt each digraph
ciphertext = ''
for d in digraphs:
row0, col0 = divmod(matrix.index(d[0]), 5)
row1, col1 = divmod(matrix.index(d[1]), 5)
if row0 == row1:
ciphertext += matrix[row0*5 + (col0+1)%5]
ciphertext += matrix[row1*5 + (col1+1)%5]
elif col0 == col1:
ciphertext += matrix[((row0+1)%5)*5 + col0]
ciphertext += matrix[((row1+1)%5)*5 + col1]
else:
ciphertext += matrix[row0*5 + col1]
ciphertext += matrix[row1*5 + col0]
return ciphertext
def playfair_decrypt(cipherText, key):
# Remove non-alphabetic characters from ciphertext and key
cipherText = re.sub("[^a-z]", "", cipherText.lower().replace('j', 'i'))
key = re.sub("[^a-z]", "", key.lower().replace('j', 'i'))
# Check if ciphertext and key are empty after removing non-alphabetic characters
if not cipherText:
messagebox.showerror('Error', 'Ciphertext must contain at least one alphabetic character')
return ''
if not key:
messagebox.showerror('Error', 'Key must contain at least one alphabetic character')
return ''
# Check that all characters in ciphertext and key are allowed in the Playfair cipher
allowed_chars = 'abcdefghiklmnopqrstuvwxyz'
for c in cipherText + key:
if c not in allowed_chars:
messagebox.showerror('Error', f'Character "{c}" is not allowed in the Playfair cipher')
return ''
# Create Playfair matrix
key = key.replace('j', 'i')
key += allowed_chars
matrix = []
for c in key:
if c not in matrix:
matrix.append(c)
# Decrypt each digraph
plaintext = ''
for i in range(0, len(cipherText), 2):
row0, col0 = divmod(matrix.index(cipherText[i]), 5)
row1, col1 = divmod(matrix.index(cipherText[i+1]), 5)
if row0 == row1:
plaintext += matrix[row0*5 + (col0-1)%5]
plaintext += matrix[row1*5 + (col1-1)%5]
elif col0 == col1:
plaintext += matrix[((row0-1)%5)*5 + col0]
plaintext += matrix[((row1-1)%5)*5 + col1]
else:
plaintext += matrix[row0*5 + col1]
plaintext += matrix[row1*5 + col0]
return plaintext
def caesar_encrypt(plainText, shift):
# Remove non-alphabetic characters from plaintext
plainText = re.sub("[^a-zA-Z]", "", plainText)
# Check if plaintext is empty after removing non-alphabetic characters
if not plainText:
messagebox.showerror('Error', 'Plaintext must contain at least one alphabetic character')
return ''
# Encrypt plaintext using Caesar cipher
cipherText = ''
for c in plainText:
if c.islower():
cipherText += chr((ord(c)-97+shift) % 26 + 97)
else:
cipherText += chr((ord(c)-65+shift) % 26 + 65)
return cipherText
def caesar_decrypt(cipherText, shift):
# Remove non-alphabetic characters from ciphertext
cipherText = re.sub("[^a-zA-Z]", "", cipherText)
# Check if ciphertext is empty after removing non-alphabetic characters
if not cipherText:
messagebox.showerror('Error', 'Ciphertext must contain at least one alphabetic character')
return ''
# Decrypt ciphertext using Caesar cipher
plainText = ''
for c in cipherText:
if c.islower():
plainText += chr((ord(c)-97-shift) % 26 + 97)
else:
plainText += chr((ord(c)-65-shift) % 26 + 65)
return plainText
class App:
def init(self, master):
self.master = master
master.title('Cipher Tool')
tk.Label(master, text='Enter Plain/Cipher Text:').grid(row=0, column=0, sticky='w')
self.input_entry = tk.Entry(master, width=50)
self.input_entry.grid(row=1, column=0, padx=10, pady=5)
tk.Label(master, text='Enter Key/Shift Value:').grid(row=2, column=0, sticky='w')
self.key_entry = tk.Entry(master, width=20)
self.key_entry.grid(row=3, column=0, padx=10, pady=5)
self.playfair_encrypt_button = tk.Button(master, text='Playfair Encrypt', command=self.playfair_encrypt)
self.playfair_encrypt_button.grid(row=4, column=0, padx=10, pady=5)
self.playfair_decrypt_button = tk.Button(master, text='Playfair Decrypt', command=self.playfair_decrypt)
self.playfair_decrypt_button.grid(row=4, column=1, padx=10, pady=5)
self.caesar_encrypt_button = tk.Button(master, text='Caesar Encrypt', command=self.caesar_encrypt)
self.caesar_encrypt_button.grid(row=5, column=0, padx=10, pady=5)
self.caesar_decrypt_button = tk.Button(master, text='Caesar Decrypt', command=self.caesar_decrypt)
self.caesar_decrypt_button.grid(row=5, column=1, padx=10, pady=5)
tk.Label(master, text='Result:').grid(row=6, column=0, sticky='w')
self.result_text = tk.Text(master, height=6, width=50)
self.result_text.grid(row=7, column=0, padx=10, pady=5)
def playfair_encrypt(self):
plaintext = self.input_entry.get()
key = self.key_entry.get()
ciphertext = playfair_encrypt(plaintext, key)
if ciphertext:
self.result_text.delete('1.0', 'end')
self.result_text.insert('1.0', ciphertext)
def playfair_decrypt(self):
ciphertext = self.input_entry.get()
key = self.key_entry.get()
plaintext = playfair_decrypt(ciphertext, key)
if plaintext:
self.result_text.delete('1.0', 'end')
self.result_text.insert('1.0', plaintext)
def caesar_encrypt(self):
plaintext = self.input_entry.get()
try:
shift = int(self.key_entry.get())
except ValueError:
messagebox.showerror('Error', 'Shift value must be an integer')
return
ciphertext = caesar_encrypt(plaintext, shift)
if ciphertext:
self.result_text.delete('1.0', 'end')
self.result_text.insert('1.0', ciphertext)
def caesar_decrypt(self):
ciphertext = self.input_entry.get()
try:
shift = int(self.key_entry.get())
except ValueError:
messagebox.showerror('Error', 'Shift value must be an integer')
return
plaintext = caesar_decrypt(ciphertext, shift)
if plaintext:
self.result_text.delete('1.0', 'end')
self.result_text.insert('1.0', plaintext)
root = tk.Tk()
app = App(root)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment