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
-
10
1
from string import ascii_uppercase
11
2
12
3
@@ -16,8 +7,20 @@ def gronsfeld(text: str, key: str) -> str:
16
7
17
8
>>> gronsfeld('hello', '412')
18
9
'LFNPP'
10
+ >>> gronsfeld('hello', '123')
11
+ 'IGOMQ'
19
12
>>> gronsfeld('', '123')
20
13
''
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
21
24
"""
22
25
ascii_len = len (ascii_uppercase )
23
26
key_len = len (key )
@@ -26,7 +29,7 @@ def gronsfeld(text: str, key: str) -> str:
26
29
upper_case_text = text .upper ()
27
30
28
31
for i , char in enumerate (upper_case_text ):
29
- if char in alphabets :
32
+ if char in ascii_uppercase :
30
33
new_position = (ascii_uppercase .index (char ) + keys [i % key_len ]) % ascii_len
31
34
shifted_letter = ascii_uppercase [new_position ]
32
35
encrypted_text += shifted_letter
@@ -37,8 +40,6 @@ def gronsfeld(text: str, key: str) -> str:
37
40
38
41
39
42
if __name__ == "__main__" :
40
- import doctest
43
+ from doctest import modtest
41
44
42
- doctest .testmod ()
43
- encrypted = gronsfeld ("hello world" , "123" )
44
- print (f"Encrypted text is { encrypted } " )
45
+ modtest ()
0 commit comments