2019-11-04 07:45:29 +00:00
|
|
|
alphabet = {
|
|
|
|
"A": ("ABCDEFGHIJKLM", "NOPQRSTUVWXYZ"),
|
|
|
|
"B": ("ABCDEFGHIJKLM", "NOPQRSTUVWXYZ"),
|
|
|
|
"C": ("ABCDEFGHIJKLM", "ZNOPQRSTUVWXY"),
|
|
|
|
"D": ("ABCDEFGHIJKLM", "ZNOPQRSTUVWXY"),
|
|
|
|
"E": ("ABCDEFGHIJKLM", "YZNOPQRSTUVWX"),
|
|
|
|
"F": ("ABCDEFGHIJKLM", "YZNOPQRSTUVWX"),
|
|
|
|
"G": ("ABCDEFGHIJKLM", "XYZNOPQRSTUVW"),
|
|
|
|
"H": ("ABCDEFGHIJKLM", "XYZNOPQRSTUVW"),
|
|
|
|
"I": ("ABCDEFGHIJKLM", "WXYZNOPQRSTUV"),
|
|
|
|
"J": ("ABCDEFGHIJKLM", "WXYZNOPQRSTUV"),
|
|
|
|
"K": ("ABCDEFGHIJKLM", "VWXYZNOPQRSTU"),
|
|
|
|
"L": ("ABCDEFGHIJKLM", "VWXYZNOPQRSTU"),
|
|
|
|
"M": ("ABCDEFGHIJKLM", "UVWXYZNOPQRST"),
|
|
|
|
"N": ("ABCDEFGHIJKLM", "UVWXYZNOPQRST"),
|
|
|
|
"O": ("ABCDEFGHIJKLM", "TUVWXYZNOPQRS"),
|
|
|
|
"P": ("ABCDEFGHIJKLM", "TUVWXYZNOPQRS"),
|
|
|
|
"Q": ("ABCDEFGHIJKLM", "STUVWXYZNOPQR"),
|
|
|
|
"R": ("ABCDEFGHIJKLM", "STUVWXYZNOPQR"),
|
|
|
|
"S": ("ABCDEFGHIJKLM", "RSTUVWXYZNOPQ"),
|
|
|
|
"T": ("ABCDEFGHIJKLM", "RSTUVWXYZNOPQ"),
|
|
|
|
"U": ("ABCDEFGHIJKLM", "QRSTUVWXYZNOP"),
|
|
|
|
"V": ("ABCDEFGHIJKLM", "QRSTUVWXYZNOP"),
|
|
|
|
"W": ("ABCDEFGHIJKLM", "PQRSTUVWXYZNO"),
|
|
|
|
"X": ("ABCDEFGHIJKLM", "PQRSTUVWXYZNO"),
|
|
|
|
"Y": ("ABCDEFGHIJKLM", "OPQRSTUVWXYZN"),
|
|
|
|
"Z": ("ABCDEFGHIJKLM", "OPQRSTUVWXYZN"),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
def generate_table(key: str) -> list[tuple[str, str]]:
|
2019-11-04 07:45:29 +00:00
|
|
|
"""
|
|
|
|
>>> generate_table('marvin') # doctest: +NORMALIZE_WHITESPACE
|
|
|
|
[('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'),
|
|
|
|
('ABCDEFGHIJKLM', 'STUVWXYZNOPQR'), ('ABCDEFGHIJKLM', 'QRSTUVWXYZNOP'),
|
|
|
|
('ABCDEFGHIJKLM', 'WXYZNOPQRSTUV'), ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST')]
|
|
|
|
"""
|
|
|
|
return [alphabet[char] for char in key.upper()]
|
|
|
|
|
|
|
|
|
2020-10-16 06:11:52 +00:00
|
|
|
def encrypt(key: str, words: str) -> str:
|
2019-11-04 07:45:29 +00:00
|
|
|
"""
|
|
|
|
>>> encrypt('marvin', 'jessica')
|
|
|
|
'QRACRWU'
|
|
|
|
"""
|
|
|
|
cipher = ""
|
|
|
|
count = 0
|
|
|
|
table = generate_table(key)
|
|
|
|
for char in words.upper():
|
|
|
|
cipher += get_opponent(table[count], char)
|
|
|
|
count = (count + 1) % len(table)
|
|
|
|
return cipher
|
|
|
|
|
|
|
|
|
2020-10-16 06:11:52 +00:00
|
|
|
def decrypt(key: str, words: str) -> str:
|
2019-11-04 07:45:29 +00:00
|
|
|
"""
|
|
|
|
>>> decrypt('marvin', 'QRACRWU')
|
|
|
|
'JESSICA'
|
|
|
|
"""
|
|
|
|
return encrypt(key, words)
|
|
|
|
|
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
def get_position(table: tuple[str, str], char: str) -> tuple[int, int]:
|
2019-11-04 07:45:29 +00:00
|
|
|
"""
|
2021-04-04 05:22:12 +00:00
|
|
|
>>> get_position(generate_table('marvin')[0], 'M')
|
|
|
|
(0, 12)
|
2019-11-04 07:45:29 +00:00
|
|
|
"""
|
2021-04-04 05:22:12 +00:00
|
|
|
# `char` is either in the 0th row or the 1st row
|
|
|
|
row = 0 if char in table[0] else 1
|
|
|
|
col = table[row].index(char)
|
|
|
|
return row, col
|
2019-11-04 07:45:29 +00:00
|
|
|
|
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
def get_opponent(table: tuple[str, str], char: str) -> str:
|
2019-11-04 07:45:29 +00:00
|
|
|
"""
|
2021-04-04 05:22:12 +00:00
|
|
|
>>> get_opponent(generate_table('marvin')[0], 'M')
|
|
|
|
'T'
|
2019-11-04 07:45:29 +00:00
|
|
|
"""
|
|
|
|
row, col = get_position(table, char.upper())
|
|
|
|
if row == 1:
|
|
|
|
return table[0][col]
|
|
|
|
else:
|
|
|
|
return table[1][col] if row == 0 else char
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod() # Fist ensure that all our tests are passing...
|
|
|
|
"""
|
2021-04-04 05:22:12 +00:00
|
|
|
Demo:
|
|
|
|
|
|
|
|
Enter key: marvin
|
|
|
|
Enter text to encrypt: jessica
|
|
|
|
Encrypted: QRACRWU
|
|
|
|
Decrypted with key: JESSICA
|
2019-11-04 07:45:29 +00:00
|
|
|
"""
|
2021-04-04 05:22:12 +00:00
|
|
|
key = input("Enter key: ").strip()
|
|
|
|
text = input("Enter text to encrypt: ").strip()
|
2019-11-04 07:45:29 +00:00
|
|
|
cipher_text = encrypt(key, text)
|
|
|
|
|
2021-04-04 05:22:12 +00:00
|
|
|
print(f"Encrypted: {cipher_text}")
|
|
|
|
print(f"Decrypted with key: {decrypt(key, cipher_text)}")
|