From da9520012c16cf7c766ed9f361254a28b0d7b138 Mon Sep 17 00:00:00 2001 From: LuigiDotSRC Date: Sat, 18 Jan 2025 14:04:40 -0500 Subject: [PATCH 1/3] implement `create_square()` --- python/solution.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/python/solution.py b/python/solution.py index e69de29..615f734 100644 --- a/python/solution.py +++ b/python/solution.py @@ -0,0 +1,24 @@ +def create_square(key: str): + square = [['']*5 for i in range(5)] + + i, j = 0, 0 + inserted = set() + # J = I + for c in key + "ABCDEFGHIKLMNOPQRSTUVWXYZ": + if c in inserted: + continue + square[i][j] = c + inserted.add(c) + j += 1 + if j % 5 == 0: + j = 0 + i += 1 + + return square + +if __name__ == "__main__": + + key = "SUPERSPY" + ciphertext = "IKEWENENXLNQLPZSLERUMRHEERYBOFNEINCHCV" + + print(create_square(key)) \ No newline at end of file From a18eb618b06a1bb1fad6391993a5569f496eafc1 Mon Sep 17 00:00:00 2001 From: LuigiDotSRC Date: Sat, 18 Jan 2025 14:33:27 -0500 Subject: [PATCH 2/3] implement decryption, result: `HIPPOPOTOMONSTROSESQUIPPEDALIOPHOBIA` --- python/solution.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/python/solution.py b/python/solution.py index 615f734..15867a0 100644 --- a/python/solution.py +++ b/python/solution.py @@ -1,4 +1,6 @@ -def create_square(key: str): +from typing import List + +def create_square(key: str) -> List[List[str]]: square = [['']*5 for i in range(5)] i, j = 0, 0 @@ -16,9 +18,38 @@ def create_square(key: str): return square -if __name__ == "__main__": +def find_char(char: str, square: List[List[str]]) -> tuple[int, int]: + for i in range(5): + for j in range(5): + if square[i][j] == char: + return (i,j) + +def decrypt(ciphertext: str, square: List[List[str]]) -> str: + bigraphs = [ciphertext[i:i+2] for i in range(0, len(ciphertext), 2)] + result = [] + + for b in bigraphs: + r1, c1 = find_char(b[0], square) + r2, c2 = find_char(b[1], square) + + if r1 == r2: + result.append(square[r1][(c1 - 1) % 5]) + result.append(square[r2][(c2 - 1) % 5]) + + elif c1 == c2: + result.append(square[(r1 - 1) % 5][c1]) + result.append(square[(r2 - 1) % 5][c2]) + else: + result.append(square[r1][c2]) + result.append(square[r2][c1]) + + result = [c for c in result if c != 'X'] + return "".join(result) + +if __name__ == "__main__": key = "SUPERSPY" ciphertext = "IKEWENENXLNQLPZSLERUMRHEERYBOFNEINCHCV" - print(create_square(key)) \ No newline at end of file + square = create_square(key) + print(decrypt(ciphertext, square)) From 8c8078931048a8561045ea1ed0b43e6ce9aec04b Mon Sep 17 00:00:00 2001 From: LuigiDotSRC Date: Sat, 18 Jan 2025 14:47:57 -0500 Subject: [PATCH 3/3] formatting and docstrings --- python/solution.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/python/solution.py b/python/solution.py index 15867a0..902fb3f 100644 --- a/python/solution.py +++ b/python/solution.py @@ -1,6 +1,12 @@ from typing import List def create_square(key: str) -> List[List[str]]: + """ + Create a 5x5 Polybius Square + + @param key: The key used to encode the Polybius square + @return: 5x5 Matrix representation of the generated square + """ square = [['']*5 for i in range(5)] i, j = 0, 0 @@ -19,12 +25,26 @@ def create_square(key: str) -> List[List[str]]: return square def find_char(char: str, square: List[List[str]]) -> tuple[int, int]: + """ + Finds the location of a character inside the Polybius Square + + @param char: The character to search for inside the square + @param square: The Polybius square generated from the `create_square()` function + @return: (row, col) location of the character within the square + """ for i in range(5): for j in range(5): if square[i][j] == char: return (i,j) def decrypt(ciphertext: str, square: List[List[str]]) -> str: + """ + Decrypts the input ciphertext https://en.wikipedia.org/wiki/Playfair_cipher + + @param ciphertext: The input ciphertext + @param square: The Polybius square generated from the `create_square()` function + @return: Plaintext + """ bigraphs = [ciphertext[i:i+2] for i in range(0, len(ciphertext), 2)] result = [] @@ -52,4 +72,5 @@ def decrypt(ciphertext: str, square: List[List[str]]) -> str: ciphertext = "IKEWENENXLNQLPZSLERUMRHEERYBOFNEINCHCV" square = create_square(key) + print(decrypt(ciphertext, square))