forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiznitskiy_cipher.py
49 lines (40 loc) · 1.35 KB
/
miznitskiy_cipher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from string import ascii_uppercase
def miznitskiy(text: str, key: str) -> str:
"""
Encrypt plaintext with the Miznitskiy cipher
>>> miznitskiy('hello', 'KEY')
'XQKKZ'
>>> miznitskiy('hello', 'ABCD')
'HFMMP'
>>> miznitskiy('', 'KEY')
''
>>> miznitskiy('yes, ¥€$ - _!@#%?', 'KEY')
'YFV, ¥€$ - _!@#%?'
>>> miznitskiy('yes, ¥€$ - _!@#%?', 'K')
'YDV, ¥€$ - _!@#%?'
>>> miznitskiy('yes, ¥€$ - _!@#%?', 'KEYWORD')
'YHV, ¥€$ - _!@#%?'
>>> miznitskiy('yes, ¥€$ - _!@#%?', '')
Traceback (most recent call last):
...
ZeroDivisionError: integer modulo by zero
"""
ascii_len = len(ascii_uppercase)
key_len = len(key)
encrypted_text = ""
keys = [ord(char) - ord("A") for char in key.upper()]
if key_len == 0:
raise ZeroDivisionError("integer modulo by zero")
upper_case_text = text.upper()
for i, char in enumerate(upper_case_text):
if char in ascii_uppercase:
shift_amount = keys[i % key_len]
new_position = (ascii_uppercase.index(char) + shift_amount) % ascii_len
shifted_letter = ascii_uppercase[new_position]
encrypted_text += shifted_letter
else:
encrypted_text += char
return encrypted_text
if __name__ == "__main__":
from doctest import testmod
testmod()