From c898f60e6f48ab638bb33046e5f448e6f7a5d5a3 Mon Sep 17 00:00:00 2001 From: lonercode Date: Sun, 6 Oct 2024 23:02:41 +0100 Subject: [PATCH 1/6] added gronsfeld cipher implementation --- ciphers/gronsfeld_cipher.py | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 ciphers/gronsfeld_cipher.py diff --git a/ciphers/gronsfeld_cipher.py b/ciphers/gronsfeld_cipher.py new file mode 100644 index 000000000000..3c46b15a97d7 --- /dev/null +++ b/ciphers/gronsfeld_cipher.py @@ -0,0 +1,40 @@ +""" +Python implementation of the Gronsfeld cipher encryption + +The Gronsfeld cipher is similar to the Vigenere cipher, +with the exception that the key is numeric. + +https://www.dcode.fr/gronsfeld-cipher +""" + +def gronsfeld(text: str, key: str) -> str: + """ + Encrypt plaintext with the Gronsfeld cipher + + >>> gronsfeld('hello', '412') + 'LFNPP' + >>> gronsfeld('', '123') + '' + """ + alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + keys = [int(i) for i in key] + encrypted_text = "" + upper_case_text = text.upper() + + for i, char in enumerate(upper_case_text): + if char in alphabets: + new_position = (alphabets.index(char) + + keys[i % len(keys)]) % len(alphabets) + shifted_letter = alphabets[new_position] + encrypted_text += shifted_letter + else: + encrypted_text += char + + return encrypted_text + +if __name__ == "__main__": + import doctest + + doctest.testmod() + encrypted = gronsfeld("hello world", "123") + print(f"Encrypted text is {encrypted}") From 5a94998a80fe5c01d40a817c59a41ae45ce3a35f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 22:16:23 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/gronsfeld_cipher.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ciphers/gronsfeld_cipher.py b/ciphers/gronsfeld_cipher.py index 3c46b15a97d7..1b1cef73ff48 100644 --- a/ciphers/gronsfeld_cipher.py +++ b/ciphers/gronsfeld_cipher.py @@ -7,6 +7,7 @@ https://www.dcode.fr/gronsfeld-cipher """ + def gronsfeld(text: str, key: str) -> str: """ Encrypt plaintext with the Gronsfeld cipher @@ -16,15 +17,16 @@ def gronsfeld(text: str, key: str) -> str: >>> gronsfeld('', '123') '' """ - alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" keys = [int(i) for i in key] encrypted_text = "" upper_case_text = text.upper() for i, char in enumerate(upper_case_text): if char in alphabets: - new_position = (alphabets.index(char) + - keys[i % len(keys)]) % len(alphabets) + new_position = (alphabets.index(char) + keys[i % len(keys)]) % len( + alphabets + ) shifted_letter = alphabets[new_position] encrypted_text += shifted_letter else: @@ -32,6 +34,7 @@ def gronsfeld(text: str, key: str) -> str: return encrypted_text + if __name__ == "__main__": import doctest From a28298410c35d0ffb2887cb95d82627a68a94bb4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 7 Oct 2024 10:50:40 +0200 Subject: [PATCH 3/6] from string import ascii_uppercase --- ciphers/gronsfeld_cipher.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ciphers/gronsfeld_cipher.py b/ciphers/gronsfeld_cipher.py index 1b1cef73ff48..b1bd7be12a13 100644 --- a/ciphers/gronsfeld_cipher.py +++ b/ciphers/gronsfeld_cipher.py @@ -6,6 +6,7 @@ https://www.dcode.fr/gronsfeld-cipher """ +from string import ascii_uppercase def gronsfeld(text: str, key: str) -> str: @@ -17,17 +18,16 @@ def gronsfeld(text: str, key: str) -> str: >>> gronsfeld('', '123') '' """ - alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - keys = [int(i) for i in key] + ascii_len = len(ascii_uppercase) + key_len = len(key) encrypted_text = "" + keys = [int(char) for char in key] upper_case_text = text.upper() for i, char in enumerate(upper_case_text): if char in alphabets: - new_position = (alphabets.index(char) + keys[i % len(keys)]) % len( - alphabets - ) - shifted_letter = alphabets[new_position] + new_position = (ascii_uppercase.index(char) + keys[i % key_len]) % ascii_len + shifted_letter = ascii_uppercase[new_position] encrypted_text += shifted_letter else: encrypted_text += char From c477aab1e13aa05fc13a394e309cf317806db7c3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 08:51:01 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/gronsfeld_cipher.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ciphers/gronsfeld_cipher.py b/ciphers/gronsfeld_cipher.py index b1bd7be12a13..fd3d82bf20fa 100644 --- a/ciphers/gronsfeld_cipher.py +++ b/ciphers/gronsfeld_cipher.py @@ -6,6 +6,7 @@ https://www.dcode.fr/gronsfeld-cipher """ + from string import ascii_uppercase From 04e977fe1554e1b10a367228ede7daebcadb857c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 7 Oct 2024 11:52:11 +0200 Subject: [PATCH 5/6] Update gronsfeld_cipher.py --- ciphers/gronsfeld_cipher.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/ciphers/gronsfeld_cipher.py b/ciphers/gronsfeld_cipher.py index fd3d82bf20fa..86fe20360e26 100644 --- a/ciphers/gronsfeld_cipher.py +++ b/ciphers/gronsfeld_cipher.py @@ -1,12 +1,3 @@ -""" -Python implementation of the Gronsfeld cipher encryption - -The Gronsfeld cipher is similar to the Vigenere cipher, -with the exception that the key is numeric. - -https://www.dcode.fr/gronsfeld-cipher -""" - from string import ascii_uppercase @@ -16,8 +7,20 @@ def gronsfeld(text: str, key: str) -> str: >>> gronsfeld('hello', '412') 'LFNPP' + >>> gronsfeld('hello', '123') + 'IGOMQ' >>> gronsfeld('', '123') '' + >>> gronsfeld('yes, ¥€$ - _!@#%?', '0') + 'YES, ¥€$ - _!@#%?' + >>> gronsfeld('yes, ¥€$ - _!@#%?', '01') + 'YFS, ¥€$ - _!@#%?' + >>> gronsfeld('yes, ¥€$ - _!@#%?', '012') + 'YFU, ¥€$ - _!@#%?' + >>> gronsfeld('yes, ¥€$ - _!@#%?', '') + Traceback (most recent call last): + ... + ZeroDivisionError: integer modulo by zero """ ascii_len = len(ascii_uppercase) key_len = len(key) @@ -26,7 +29,7 @@ def gronsfeld(text: str, key: str) -> str: upper_case_text = text.upper() for i, char in enumerate(upper_case_text): - if char in alphabets: + if char in ascii_uppercase: new_position = (ascii_uppercase.index(char) + keys[i % key_len]) % ascii_len shifted_letter = ascii_uppercase[new_position] encrypted_text += shifted_letter @@ -37,8 +40,6 @@ def gronsfeld(text: str, key: str) -> str: if __name__ == "__main__": - import doctest + from doctest import modtest - doctest.testmod() - encrypted = gronsfeld("hello world", "123") - print(f"Encrypted text is {encrypted}") + modtest() From 2d94ea5a1f2702dfa0a8535011aac373abf6abe9 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 7 Oct 2024 11:54:42 +0200 Subject: [PATCH 6/6] testmod --- ciphers/gronsfeld_cipher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ciphers/gronsfeld_cipher.py b/ciphers/gronsfeld_cipher.py index 86fe20360e26..8fbeab4307fc 100644 --- a/ciphers/gronsfeld_cipher.py +++ b/ciphers/gronsfeld_cipher.py @@ -40,6 +40,6 @@ def gronsfeld(text: str, key: str) -> str: if __name__ == "__main__": - from doctest import modtest + from doctest import testmod - modtest() + testmod()