From 2f201d1327d6ee20810a105d0ec5599ce9c51525 Mon Sep 17 00:00:00 2001 From: Keyboard-1 <142900182+Keyboard-1@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:46:19 +0530 Subject: [PATCH 1/5] added doctest to playfair_cipher.py --- ciphers/playfair_cipher.py | 58 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/ciphers/playfair_cipher.py b/ciphers/playfair_cipher.py index 7279fb23ecb2..dea41fc31977 100644 --- a/ciphers/playfair_cipher.py +++ b/ciphers/playfair_cipher.py @@ -1,3 +1,24 @@ +""" +https://en.wikipedia.org/wiki/Playfair_cipher#Description + +The Playfair cipher was developed by Charles Wheatstone in 1854 +It's use was heavily promotedby Lord Playfair, hence its name + +Some features of the Playfair cipher are: + +1) It was the first literal diagram substitution cipher +2) It is a manual symmetric encryption technique +3) It is a multiple letter encryption cipher + +The implementation in the code below encodes alphabets only. +It removes spaces, special characters and numbers from the +code. + +Playfair is no longer used by military forces because of known +insecurities and of the advent of automated encryption devices. +This cipher is regarded as insecure since before World War I. +""" + import itertools import string from collections.abc import Generator, Iterable @@ -60,11 +81,27 @@ def generate_table(key: str) -> list[str]: def encode(plaintext: str, key: str) -> str: + + """encode function encodes the given plaintext by taking + two strings, the plaintext and the key, as input + and returns an encoded string + + >>> print(encode("Hello", "MONARCHY")) + CFSUPM + >>> print(encode("attack on the left flank", "EMERGENCY")) + DQZSBYFSDZFMFNLOHFDRSG + >>> print(encode("Sorry!", "SPECIAL")) + AVXETX + >>> print(encode("Number 1", "NUMBER")) + UMBENF + >>> print(encode("Photosynthesis!", "THE SUN")) + OEMHQHVCHESUKE + """ + table = generate_table(key) plaintext = prepare_input(plaintext) ciphertext = "" - # https://en.wikipedia.org/wiki/Playfair_cipher#Description for char1, char2 in chunker(plaintext, 2): row1, col1 = divmod(table.index(char1), 5) row2, col2 = divmod(table.index(char2), 5) @@ -83,10 +120,20 @@ def encode(plaintext: str, key: str) -> str: def decode(ciphertext: str, key: str) -> str: + """The decode function decodes the input string + using the provided key and returns a decoded string + + >>> print(decode("BMZFAZRZDH", "HAZARD")) + FIREHAZARD + >>> print(decode("HNBWBPQT", "AUTOMOBILE")) + DRIVINGX + >>> print(decode("SLYSSAQS", "CASTLE")) + ATXTACKX + """ + table = generate_table(key) plaintext = "" - # https://en.wikipedia.org/wiki/Playfair_cipher#Description for char1, char2 in chunker(ciphertext, 2): row1, col1 = divmod(table.index(char1), 5) row2, col2 = divmod(table.index(char2), 5) @@ -102,3 +149,10 @@ def decode(ciphertext: str, key: str) -> str: plaintext += table[row2 * 5 + col1] return plaintext + +if __name__ == "__main__": + import doctest + doctest.testmod() + + print("Encoded:",encode("BYE AND THANKS", "GREETING")) + print("Decoded:",decode("CXRBANRLBALQ", "GREETING")) \ No newline at end of file From 9d3ccb30433123fb2df753393d682a6ce64d9e6b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Oct 2023 18:20:03 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/playfair_cipher.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ciphers/playfair_cipher.py b/ciphers/playfair_cipher.py index dea41fc31977..f74dbe96860d 100644 --- a/ciphers/playfair_cipher.py +++ b/ciphers/playfair_cipher.py @@ -11,11 +11,11 @@ 3) It is a multiple letter encryption cipher The implementation in the code below encodes alphabets only. -It removes spaces, special characters and numbers from the +It removes spaces, special characters and numbers from the code. -Playfair is no longer used by military forces because of known -insecurities and of the advent of automated encryption devices. +Playfair is no longer used by military forces because of known +insecurities and of the advent of automated encryption devices. This cipher is regarded as insecure since before World War I. """ @@ -81,7 +81,6 @@ def generate_table(key: str) -> list[str]: def encode(plaintext: str, key: str) -> str: - """encode function encodes the given plaintext by taking two strings, the plaintext and the key, as input and returns an encoded string @@ -122,7 +121,7 @@ def encode(plaintext: str, key: str) -> str: def decode(ciphertext: str, key: str) -> str: """The decode function decodes the input string using the provided key and returns a decoded string - + >>> print(decode("BMZFAZRZDH", "HAZARD")) FIREHAZARD >>> print(decode("HNBWBPQT", "AUTOMOBILE")) @@ -150,9 +149,11 @@ def decode(ciphertext: str, key: str) -> str: return plaintext + if __name__ == "__main__": import doctest + doctest.testmod() - print("Encoded:",encode("BYE AND THANKS", "GREETING")) - print("Decoded:",decode("CXRBANRLBALQ", "GREETING")) \ No newline at end of file + print("Encoded:", encode("BYE AND THANKS", "GREETING")) + print("Decoded:", decode("CXRBANRLBALQ", "GREETING")) From 10c212c9341c6fc21a61c67096b67387cb346e49 Mon Sep 17 00:00:00 2001 From: Keyboard-1 <142900182+Keyboard-1@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:52:11 +0530 Subject: [PATCH 3/5] Added newline to EOF andremoved trailing whitespace --- ciphers/playfair_cipher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ciphers/playfair_cipher.py b/ciphers/playfair_cipher.py index dea41fc31977..1ae9728edf09 100644 --- a/ciphers/playfair_cipher.py +++ b/ciphers/playfair_cipher.py @@ -122,7 +122,7 @@ def encode(plaintext: str, key: str) -> str: def decode(ciphertext: str, key: str) -> str: """The decode function decodes the input string using the provided key and returns a decoded string - + >>> print(decode("BMZFAZRZDH", "HAZARD")) FIREHAZARD >>> print(decode("HNBWBPQT", "AUTOMOBILE")) @@ -155,4 +155,4 @@ def decode(ciphertext: str, key: str) -> str: doctest.testmod() print("Encoded:",encode("BYE AND THANKS", "GREETING")) - print("Decoded:",decode("CXRBANRLBALQ", "GREETING")) \ No newline at end of file + print("Decoded:",decode("CXRBANRLBALQ", "GREETING")) From 96d334fdf2c6d02f3ce711cbea511f2b7eca8345 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Oct 2023 18:25:01 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/playfair_cipher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ciphers/playfair_cipher.py b/ciphers/playfair_cipher.py index 31d12c83db73..f74dbe96860d 100644 --- a/ciphers/playfair_cipher.py +++ b/ciphers/playfair_cipher.py @@ -155,5 +155,5 @@ def decode(ciphertext: str, key: str) -> str: doctest.testmod() - print("Encoded:",encode("BYE AND THANKS", "GREETING")) - print("Decoded:",decode("CXRBANRLBALQ", "GREETING")) + print("Encoded:", encode("BYE AND THANKS", "GREETING")) + print("Decoded:", decode("CXRBANRLBALQ", "GREETING")) From 8b379f247bb5e7bfaae9eafd2bd8e8fd553d78e9 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 22 Oct 2023 17:34:52 -0400 Subject: [PATCH 5/5] Apply suggestions from code review --- ciphers/playfair_cipher.py | 46 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/ciphers/playfair_cipher.py b/ciphers/playfair_cipher.py index f74dbe96860d..86b45bc4fb6a 100644 --- a/ciphers/playfair_cipher.py +++ b/ciphers/playfair_cipher.py @@ -81,20 +81,20 @@ def generate_table(key: str) -> list[str]: def encode(plaintext: str, key: str) -> str: - """encode function encodes the given plaintext by taking - two strings, the plaintext and the key, as input - and returns an encoded string - - >>> print(encode("Hello", "MONARCHY")) - CFSUPM - >>> print(encode("attack on the left flank", "EMERGENCY")) - DQZSBYFSDZFMFNLOHFDRSG - >>> print(encode("Sorry!", "SPECIAL")) - AVXETX - >>> print(encode("Number 1", "NUMBER")) - UMBENF - >>> print(encode("Photosynthesis!", "THE SUN")) - OEMHQHVCHESUKE + """ + Encode the given plaintext using the Playfair cipher. + Takes the plaintext and the key as input and returns the encoded string. + + >>> encode("Hello", "MONARCHY") + 'CFSUPM' + >>> encode("attack on the left flank", "EMERGENCY") + 'DQZSBYFSDZFMFNLOHFDRSG' + >>> encode("Sorry!", "SPECIAL") + 'AVXETX' + >>> encode("Number 1", "NUMBER") + 'UMBENF' + >>> encode("Photosynthesis!", "THE SUN") + 'OEMHQHVCHESUKE' """ table = generate_table(key) @@ -119,15 +119,15 @@ def encode(plaintext: str, key: str) -> str: def decode(ciphertext: str, key: str) -> str: - """The decode function decodes the input string - using the provided key and returns a decoded string - - >>> print(decode("BMZFAZRZDH", "HAZARD")) - FIREHAZARD - >>> print(decode("HNBWBPQT", "AUTOMOBILE")) - DRIVINGX - >>> print(decode("SLYSSAQS", "CASTLE")) - ATXTACKX + """ + Decode the input string using the provided key. + + >>> decode("BMZFAZRZDH", "HAZARD") + 'FIREHAZARD' + >>> decode("HNBWBPQT", "AUTOMOBILE") + 'DRIVINGX' + >>> decode("SLYSSAQS", "CASTLE") + 'ATXTACKX' """ table = generate_table(key)