Skip to content

Commit 04e977f

Browse files
authored
Update gronsfeld_cipher.py
1 parent c477aab commit 04e977f

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

ciphers/gronsfeld_cipher.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
"""
2-
Python implementation of the Gronsfeld cipher encryption
3-
4-
The Gronsfeld cipher is similar to the Vigenere cipher,
5-
with the exception that the key is numeric.
6-
7-
https://www.dcode.fr/gronsfeld-cipher
8-
"""
9-
101
from string import ascii_uppercase
112

123

@@ -16,8 +7,20 @@ def gronsfeld(text: str, key: str) -> str:
167
178
>>> gronsfeld('hello', '412')
189
'LFNPP'
10+
>>> gronsfeld('hello', '123')
11+
'IGOMQ'
1912
>>> gronsfeld('', '123')
2013
''
14+
>>> gronsfeld('yes, ¥€$ - _!@#%?', '0')
15+
'YES, ¥€$ - _!@#%?'
16+
>>> gronsfeld('yes, ¥€$ - _!@#%?', '01')
17+
'YFS, ¥€$ - _!@#%?'
18+
>>> gronsfeld('yes, ¥€$ - _!@#%?', '012')
19+
'YFU, ¥€$ - _!@#%?'
20+
>>> gronsfeld('yes, ¥€$ - _!@#%?', '')
21+
Traceback (most recent call last):
22+
...
23+
ZeroDivisionError: integer modulo by zero
2124
"""
2225
ascii_len = len(ascii_uppercase)
2326
key_len = len(key)
@@ -26,7 +29,7 @@ def gronsfeld(text: str, key: str) -> str:
2629
upper_case_text = text.upper()
2730

2831
for i, char in enumerate(upper_case_text):
29-
if char in alphabets:
32+
if char in ascii_uppercase:
3033
new_position = (ascii_uppercase.index(char) + keys[i % key_len]) % ascii_len
3134
shifted_letter = ascii_uppercase[new_position]
3235
encrypted_text += shifted_letter
@@ -37,8 +40,6 @@ def gronsfeld(text: str, key: str) -> str:
3740

3841

3942
if __name__ == "__main__":
40-
import doctest
43+
from doctest import modtest
4144

42-
doctest.testmod()
43-
encrypted = gronsfeld("hello world", "123")
44-
print(f"Encrypted text is {encrypted}")
45+
modtest()

0 commit comments

Comments
 (0)