2019-12-20 22:52:43 +00:00
|
|
|
import random
|
|
|
|
import sys
|
2016-08-07 15:48:46 +00:00
|
|
|
|
2020-09-28 21:41:04 +00:00
|
|
|
from . import cryptomath_module as cryptomath
|
2019-12-20 22:52:43 +00:00
|
|
|
|
2019-12-26 11:50:12 +00:00
|
|
|
SYMBOLS = (
|
|
|
|
r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
|
|
|
|
r"""abcdefghijklmnopqrstuvwxyz{|}~"""
|
|
|
|
)
|
2016-08-07 15:48:46 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2020-10-16 06:11:52 +00:00
|
|
|
def check_keys(keyA: int, keyB: int, mode: str) -> None:
|
2019-12-20 22:52:43 +00:00
|
|
|
if mode == "encrypt":
|
|
|
|
if keyA == 1:
|
|
|
|
sys.exit(
|
|
|
|
"The affine cipher becomes weak when key "
|
|
|
|
"A is set to 1. Choose different key"
|
|
|
|
)
|
|
|
|
if keyB == 0:
|
|
|
|
sys.exit(
|
|
|
|
"The affine cipher becomes weak when key "
|
|
|
|
"B is set to 0. Choose different key"
|
|
|
|
)
|
2016-08-07 15:48:46 +00:00
|
|
|
if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1:
|
2019-10-05 05:14:13 +00:00
|
|
|
sys.exit(
|
2019-12-20 22:52:43 +00:00
|
|
|
"Key A must be greater than 0 and key B must "
|
|
|
|
f"be between 0 and {len(SYMBOLS) - 1}."
|
2019-10-05 05:14:13 +00:00
|
|
|
)
|
2019-12-20 22:52:43 +00:00
|
|
|
if cryptomath.gcd(keyA, len(SYMBOLS)) != 1:
|
2019-10-05 05:14:13 +00:00
|
|
|
sys.exit(
|
2019-12-20 22:52:43 +00:00
|
|
|
f"Key A {keyA} and the symbol set size {len(SYMBOLS)} "
|
|
|
|
"are not relatively prime. Choose a different key."
|
2019-10-05 05:14:13 +00:00
|
|
|
)
|
|
|
|
|
2016-08-07 15:48:46 +00:00
|
|
|
|
2019-12-20 22:52:43 +00:00
|
|
|
def encrypt_message(key: int, message: str) -> str:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2020-06-16 08:09:19 +00:00
|
|
|
>>> encrypt_message(4545, 'The affine cipher is a type of monoalphabetic '
|
|
|
|
... 'substitution cipher.')
|
2016-08-07 15:48:46 +00:00
|
|
|
'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi'
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-12-20 22:52:43 +00:00
|
|
|
keyA, keyB = divmod(key, len(SYMBOLS))
|
|
|
|
check_keys(keyA, keyB, "encrypt")
|
2019-10-05 05:14:13 +00:00
|
|
|
cipherText = ""
|
2016-08-07 15:48:46 +00:00
|
|
|
for symbol in message:
|
|
|
|
if symbol in SYMBOLS:
|
|
|
|
symIndex = SYMBOLS.find(symbol)
|
|
|
|
cipherText += SYMBOLS[(symIndex * keyA + keyB) % len(SYMBOLS)]
|
|
|
|
else:
|
|
|
|
cipherText += symbol
|
|
|
|
return cipherText
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-12-20 22:52:43 +00:00
|
|
|
def decrypt_message(key: int, message: str) -> str:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2020-06-16 08:09:19 +00:00
|
|
|
>>> decrypt_message(4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF'
|
|
|
|
... '{xIp~{HL}Gi')
|
2016-08-07 15:48:46 +00:00
|
|
|
'The affine cipher is a type of monoalphabetic substitution cipher.'
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-12-20 22:52:43 +00:00
|
|
|
keyA, keyB = divmod(key, len(SYMBOLS))
|
|
|
|
check_keys(keyA, keyB, "decrypt")
|
2019-10-05 05:14:13 +00:00
|
|
|
plainText = ""
|
2021-03-22 06:59:51 +00:00
|
|
|
modInverseOfkeyA = cryptomath.find_mod_inverse(keyA, len(SYMBOLS))
|
2016-08-07 15:48:46 +00:00
|
|
|
for symbol in message:
|
|
|
|
if symbol in SYMBOLS:
|
|
|
|
symIndex = SYMBOLS.find(symbol)
|
|
|
|
plainText += SYMBOLS[(symIndex - keyB) * modInverseOfkeyA % len(SYMBOLS)]
|
|
|
|
else:
|
|
|
|
plainText += symbol
|
|
|
|
return plainText
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2020-10-16 06:11:52 +00:00
|
|
|
def get_random_key() -> int:
|
2016-08-07 15:48:46 +00:00
|
|
|
while True:
|
|
|
|
keyA = random.randint(2, len(SYMBOLS))
|
|
|
|
keyB = random.randint(2, len(SYMBOLS))
|
2020-10-14 09:53:37 +00:00
|
|
|
if cryptomath.gcd(keyA, len(SYMBOLS)) == 1 and keyB % len(SYMBOLS) != 0:
|
2018-10-17 21:28:57 +00:00
|
|
|
return keyA * len(SYMBOLS) + keyB
|
2016-08-07 15:48:46 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-03-22 06:59:51 +00:00
|
|
|
def main() -> None:
|
|
|
|
"""
|
|
|
|
>>> key = get_random_key()
|
|
|
|
>>> msg = "This is a test!"
|
|
|
|
>>> decrypt_message(key, encrypt_message(key, msg)) == msg
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
message = input("Enter message: ").strip()
|
|
|
|
key = int(input("Enter key [2000 - 9000]: ").strip())
|
|
|
|
mode = input("Encrypt/Decrypt [E/D]: ").strip().lower()
|
|
|
|
|
|
|
|
if mode.startswith("e"):
|
|
|
|
mode = "encrypt"
|
|
|
|
translated = encrypt_message(key, message)
|
|
|
|
elif mode.startswith("d"):
|
|
|
|
mode = "decrypt"
|
|
|
|
translated = decrypt_message(key, message)
|
|
|
|
print(f"\n{mode.title()}ed text: \n{translated}")
|
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2016-08-07 15:48:46 +00:00
|
|
|
import doctest
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2016-08-07 15:48:46 +00:00
|
|
|
doctest.testmod()
|
2020-07-02 14:32:15 +00:00
|
|
|
# main()
|