File tree 1 file changed +6
-6
lines changed
1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change 6
6
7
7
https://www.dcode.fr/gronsfeld-cipher
8
8
"""
9
+ from string import ascii_uppercase
9
10
10
11
11
12
def gronsfeld (text : str , key : str ) -> str :
@@ -17,17 +18,16 @@ def gronsfeld(text: str, key: str) -> str:
17
18
>>> gronsfeld('', '123')
18
19
''
19
20
"""
20
- alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
21
- keys = [ int ( i ) for i in key ]
21
+ ascii_len = len ( ascii_uppercase )
22
+ key_len = len ( key )
22
23
encrypted_text = ""
24
+ keys = [int (char ) for char in key ]
23
25
upper_case_text = text .upper ()
24
26
25
27
for i , char in enumerate (upper_case_text ):
26
28
if char in alphabets :
27
- new_position = (alphabets .index (char ) + keys [i % len (keys )]) % len (
28
- alphabets
29
- )
30
- shifted_letter = alphabets [new_position ]
29
+ new_position = (ascii_uppercase .index (char ) + keys [i % key_len ]) % ascii_len
30
+ shifted_letter = ascii_uppercase [new_position ]
31
31
encrypted_text += shifted_letter
32
32
else :
33
33
encrypted_text += char
You can’t perform that action at this time.
0 commit comments