Skip to content

Added miznitskiy cipher implementation #11902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions ciphers/miznitskiy_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
Loading