From 77f3cb07662702a1d66a1774e9f99bc9bbc8a14c Mon Sep 17 00:00:00 2001 From: Rovers11 Date: Fri, 11 Oct 2024 11:33:19 +0530 Subject: [PATCH 1/2] Add Gronsfeld cipher implementation --- ciphers/gronsfeld.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 ciphers/gronsfeld.py diff --git a/ciphers/gronsfeld.py b/ciphers/gronsfeld.py new file mode 100644 index 000000000000..0c9cb45ed53a --- /dev/null +++ b/ciphers/gronsfeld.py @@ -0,0 +1,63 @@ +def gronsfeld_encrypt(plaintext, key): + """ + Encrypts a plaintext using the Gronsfeld cipher. + + Parameters: + plaintext (str): The message to be encrypted. + key (str): A numeric key used for encryption (e.g., "31415"). + + Returns: + str: The encrypted message. + """ + ciphertext = "" + key = [int(k) for k in key] + key_length = len(key) + + for i, letter in enumerate(plaintext): + if letter.isalpha(): + shift = key[i % key_length] + base = ord('A') if letter.isupper() else ord('a') + # Shift and wrap around the alphabet + ciphertext += chr((ord(letter) - base + shift) % 26 + base) + else: + ciphertext += letter + + return ciphertext + + +def gronsfeld_decrypt(ciphertext, key): + """ + Decrypts a ciphertext using the Gronsfeld cipher. + + Parameters: + ciphertext (str): The message to be decrypted. + key (str): A numeric key used for decryption (e.g., "31415"). + + Returns: + str: The decrypted message. + """ + plaintext = "" + key = [int(k) for k in key] + key_length = len(key) + + for i, letter in enumerate(ciphertext): + if letter.isalpha(): + shift = key[i % key_length] + base = ord('A') if letter.isupper() else ord('a') + # Reverse the shift to decrypt + plaintext += chr((ord(letter) - base - shift) % 26 + base) + else: + plaintext += letter + + return plaintext + + +# Example usage: +plaintext = input("Enter a message to encrypt: ") +key = input("Enter a key (e.g., 31415): ") + +encrypted_message = gronsfeld_encrypt(plaintext, key) +print(f"Encrypted: {encrypted_message}") + +decrypted_message = gronsfeld_decrypt(encrypted_message, key) +print(f"Decrypted: {decrypted_message}") From 22378d16b10723f58345f2865f7cdf67f6dee9ac Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 11 Oct 2024 06:07:34 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/gronsfeld.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ciphers/gronsfeld.py b/ciphers/gronsfeld.py index 0c9cb45ed53a..e73936835727 100644 --- a/ciphers/gronsfeld.py +++ b/ciphers/gronsfeld.py @@ -1,54 +1,54 @@ def gronsfeld_encrypt(plaintext, key): """ Encrypts a plaintext using the Gronsfeld cipher. - + Parameters: plaintext (str): The message to be encrypted. key (str): A numeric key used for encryption (e.g., "31415"). - + Returns: str: The encrypted message. """ ciphertext = "" key = [int(k) for k in key] key_length = len(key) - + for i, letter in enumerate(plaintext): if letter.isalpha(): shift = key[i % key_length] - base = ord('A') if letter.isupper() else ord('a') + base = ord("A") if letter.isupper() else ord("a") # Shift and wrap around the alphabet ciphertext += chr((ord(letter) - base + shift) % 26 + base) else: ciphertext += letter - + return ciphertext def gronsfeld_decrypt(ciphertext, key): """ Decrypts a ciphertext using the Gronsfeld cipher. - + Parameters: ciphertext (str): The message to be decrypted. key (str): A numeric key used for decryption (e.g., "31415"). - + Returns: str: The decrypted message. """ plaintext = "" key = [int(k) for k in key] key_length = len(key) - + for i, letter in enumerate(ciphertext): if letter.isalpha(): shift = key[i % key_length] - base = ord('A') if letter.isupper() else ord('a') + base = ord("A") if letter.isupper() else ord("a") # Reverse the shift to decrypt plaintext += chr((ord(letter) - base - shift) % 26 + base) else: plaintext += letter - + return plaintext