File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ def gronsfeld (text : str , key : str ) -> str :
11
+ """
12
+ Encrypt plaintext with the Gronsfeld cipher
13
+
14
+ >>> gronsfeld('hello', '412')
15
+ 'LFNPP'
16
+ >>> gronsfeld('', '123')
17
+ ''
18
+ """
19
+ alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
20
+ keys = [int (i ) for i in key ]
21
+ encrypted_text = ""
22
+ upper_case_text = text .upper ()
23
+
24
+ for i , char in enumerate (upper_case_text ):
25
+ if char in alphabets :
26
+ new_position = (alphabets .index (char ) +
27
+ keys [i % len (keys )]) % len (alphabets )
28
+ shifted_letter = alphabets [new_position ]
29
+ encrypted_text += shifted_letter
30
+ else :
31
+ encrypted_text += char
32
+
33
+ return encrypted_text
34
+
35
+ if __name__ == "__main__" :
36
+ import doctest
37
+
38
+ doctest .testmod ()
39
+ encrypted = gronsfeld ("hello world" , "123" )
40
+ print (f"Encrypted text is { encrypted } " )
You can’t perform that action at this time.
0 commit comments