Skip to content

Commit 3688476

Browse files
authored
Create miznitskiy_cipher.py
1 parent 7d61f9b commit 3688476

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

ciphers/miznitskiy_cipher.py

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

0 commit comments

Comments
 (0)