Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 22378d1

Browse files
committedOct 11, 2024·
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 77f3cb0 commit 22378d1

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed
 

‎ciphers/gronsfeld.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
11
def gronsfeld_encrypt(plaintext, key):
22
"""
33
Encrypts a plaintext using the Gronsfeld cipher.
4-
4+
55
Parameters:
66
plaintext (str): The message to be encrypted.
77
key (str): A numeric key used for encryption (e.g., "31415").
8-
8+
99
Returns:
1010
str: The encrypted message.
1111
"""
1212
ciphertext = ""
1313
key = [int(k) for k in key]
1414
key_length = len(key)
15-
15+
1616
for i, letter in enumerate(plaintext):
1717
if letter.isalpha():
1818
shift = key[i % key_length]
19-
base = ord('A') if letter.isupper() else ord('a')
19+
base = ord("A") if letter.isupper() else ord("a")
2020
# Shift and wrap around the alphabet
2121
ciphertext += chr((ord(letter) - base + shift) % 26 + base)
2222
else:
2323
ciphertext += letter
24-
24+
2525
return ciphertext
2626

2727

2828
def gronsfeld_decrypt(ciphertext, key):
2929
"""
3030
Decrypts a ciphertext using the Gronsfeld cipher.
31-
31+
3232
Parameters:
3333
ciphertext (str): The message to be decrypted.
3434
key (str): A numeric key used for decryption (e.g., "31415").
35-
35+
3636
Returns:
3737
str: The decrypted message.
3838
"""
3939
plaintext = ""
4040
key = [int(k) for k in key]
4141
key_length = len(key)
42-
42+
4343
for i, letter in enumerate(ciphertext):
4444
if letter.isalpha():
4545
shift = key[i % key_length]
46-
base = ord('A') if letter.isupper() else ord('a')
46+
base = ord("A") if letter.isupper() else ord("a")
4747
# Reverse the shift to decrypt
4848
plaintext += chr((ord(letter) - base - shift) % 26 + base)
4949
else:
5050
plaintext += letter
51-
51+
5252
return plaintext
5353

5454

0 commit comments

Comments
 (0)
Please sign in to comment.