Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Playfair Cipher by dig
from collections import OrderedDict
from string import ascii_lowercase, digits
def generate_key_table(keyword):
unique_chars = "".join(OrderedDict.fromkeys(keyword))
remaining_chars = [char for char in ascii_lowercase + digits if char not in unique_chars]
key_table = unique_chars + "".join(remaining_chars)
return [key_table[i:i+6] for i in range(0, 36, 6)]
def process_text(text):
text = text.lower().replace(" ", "")
processed_text = "".join(char for char in text if char.isalnum())
digraphs = []
index = 0
while index < len(processed_text):
pair = processed_text[index]
if index + 1 < len(processed_text):
next_char = processed_text[index + 1]
if pair == next_char:
pair += 'x' if next_char != 'x' else 'z'
index += 1
else:
pair += next_char
index += 2
else:
pair += 'z' if pair != 'z' else 'x'
index += 1
digraphs.append(pair)
return digraphs
def find_position(key_table, char):
for row, row_values in enumerate(key_table):
if char in row_values:
col = row_values.index(char)
return row, col
return None, None
def encode(message, keyword):
key_table = generate_key_table(keyword)
digraphs = process_text(message)
encoded_message = ""
for pair in digraphs:
row1, col1 = find_position(key_table, pair[0])
row2, col2 = find_position(key_table, pair[1])
if row1 == row2:
encoded_message += key_table[row1][(col1 + 1) % 6] + key_table[row2][(col2 + 1) % 6]
elif col1 == col2:
encoded_message += key_table[(row1 + 1) % 6][col1] + key_table[(row2 + 1) % 6][col2]
else:
encoded_message += key_table[row1][col2] + key_table[row2][col1]
return encoded_message
def decode(encoded_message, keyword):
key_table = generate_key_table(keyword)
digraphs = process_text(encoded_message)
decoded_message = ""
for pair in digraphs:
row1, col1 = find_position(key_table, pair[0])
row2, col2 = find_position(key_table, pair[1])
if row1 == row2:
decoded_message += key_table[row1][(col1 - 1) % 6] + key_table[row2][(col2 - 1) % 6]
elif col1 == col2:
decoded_message += key_table[(row1 - 1) % 6][col1] + key_table[(row2 - 1) % 6][col2]
else:
decoded_message += key_table[row1][col2] + key_table[row2][col1]
return decoded_message
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert encode("Fizz Buzz is x89 XX.", "checkio101") == 'do2y7mt22kry94y2y2', "Encode fizz buzz"
assert decode("do2y7mt22kry94y2y2", "checkio101") == 'fizxzbuzzisx89xzxz', "Decode fizz buzz"
assert encode("How are you?", "hello") == 'ea2imb1ht0', "Encode How are you"
assert decode("ea2imb1ht0", "hello") == 'howareyouz', "Decode How are you"
assert encode("My name is Alex!!!", "alexander") == 'i1dlkxjqlexn', "Encode Alex"
assert decode("i1dlkxjqlexn", "alexander") == 'mynameisalex', "Decode Alex"
assert encode("Who are you?", "human") == 'rnvftc1jd5', "Encode WHo"
assert decode("rnvftc1jd5", "human") == 'whoareyouz', "Decode Who"
assert encode("ATTACK AT DAWN", "general") == 'ewwektewhnua', "Encode attack"
assert decode("ewwektewhnua", "general") == 'attackatdawn', "Decode attack"
April 27, 2023