99)
1010
1111
12- def check_keys (keyA : int , keyB : int , mode : str ) -> None :
12+ def check_keys (key_a : int , key_b : int , mode : str ) -> None :
1313 if mode == "encrypt" :
14- if keyA == 1 :
14+ if key_a == 1 :
1515 sys .exit (
1616 "The affine cipher becomes weak when key "
1717 "A is set to 1. Choose different key"
1818 )
19- if keyB == 0 :
19+ if key_b == 0 :
2020 sys .exit (
2121 "The affine cipher becomes weak when key "
2222 "B is set to 0. Choose different key"
2323 )
24- if keyA < 0 or keyB < 0 or keyB > len (SYMBOLS ) - 1 :
24+ if key_a < 0 or key_b < 0 or key_b > len (SYMBOLS ) - 1 :
2525 sys .exit (
2626 "Key A must be greater than 0 and key B must "
2727 f"be between 0 and { len (SYMBOLS ) - 1 } ."
2828 )
29- if cryptomath .gcd (keyA , len (SYMBOLS )) != 1 :
29+ if cryptomath .gcd (key_a , len (SYMBOLS )) != 1 :
3030 sys .exit (
31- f"Key A { keyA } and the symbol set size { len (SYMBOLS )} "
31+ f"Key A { key_a } and the symbol set size { len (SYMBOLS )} "
3232 "are not relatively prime. Choose a different key."
3333 )
3434
@@ -39,16 +39,16 @@ def encrypt_message(key: int, message: str) -> str:
3939 ... 'substitution cipher.')
4040 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi'
4141 """
42- keyA , keyB = divmod (key , len (SYMBOLS ))
43- check_keys (keyA , keyB , "encrypt" )
44- cipherText = ""
42+ key_a , key_b = divmod (key , len (SYMBOLS ))
43+ check_keys (key_a , key_b , "encrypt" )
44+ cipher_text = ""
4545 for symbol in message :
4646 if symbol in SYMBOLS :
47- symIndex = SYMBOLS .find (symbol )
48- cipherText += SYMBOLS [(symIndex * keyA + keyB ) % len (SYMBOLS )]
47+ sym_index = SYMBOLS .find (symbol )
48+ cipher_text += SYMBOLS [(sym_index * key_a + key_b ) % len (SYMBOLS )]
4949 else :
50- cipherText += symbol
51- return cipherText
50+ cipher_text += symbol
51+ return cipher_text
5252
5353
5454def decrypt_message (key : int , message : str ) -> str :
@@ -57,25 +57,27 @@ def decrypt_message(key: int, message: str) -> str:
5757 ... '{xIp~{HL}Gi')
5858 'The affine cipher is a type of monoalphabetic substitution cipher.'
5959 """
60- keyA , keyB = divmod (key , len (SYMBOLS ))
61- check_keys (keyA , keyB , "decrypt" )
62- plainText = ""
63- modInverseOfkeyA = cryptomath .find_mod_inverse (keyA , len (SYMBOLS ))
60+ key_a , key_b = divmod (key , len (SYMBOLS ))
61+ check_keys (key_a , key_b , "decrypt" )
62+ plain_text = ""
63+ mod_inverse_of_key_a = cryptomath .find_mod_inverse (key_a , len (SYMBOLS ))
6464 for symbol in message :
6565 if symbol in SYMBOLS :
66- symIndex = SYMBOLS .find (symbol )
67- plainText += SYMBOLS [(symIndex - keyB ) * modInverseOfkeyA % len (SYMBOLS )]
66+ sym_index = SYMBOLS .find (symbol )
67+ plain_text += SYMBOLS [
68+ (sym_index - key_b ) * mod_inverse_of_key_a % len (SYMBOLS )
69+ ]
6870 else :
69- plainText += symbol
70- return plainText
71+ plain_text += symbol
72+ return plain_text
7173
7274
7375def get_random_key () -> int :
7476 while True :
75- keyA = random .randint (2 , len (SYMBOLS ))
76- keyB = random .randint (2 , len (SYMBOLS ))
77- if cryptomath .gcd (keyA , len (SYMBOLS )) == 1 and keyB % len (SYMBOLS ) != 0 :
78- return keyA * len (SYMBOLS ) + keyB
77+ key_b = random .randint (2 , len (SYMBOLS ))
78+ key_b = random .randint (2 , len (SYMBOLS ))
79+ if cryptomath .gcd (key_b , len (SYMBOLS )) == 1 and key_b % len (SYMBOLS ) != 0 :
80+ return key_b * len (SYMBOLS ) + key_b
7981
8082
8183def main () -> None :
0 commit comments