9
9
)
10
10
11
11
12
- def check_keys (keyA : int , keyB : int , mode : str ) -> None :
12
+ def check_keys (key_a : int , key_b : int , mode : str ) -> None :
13
13
if mode == "encrypt" :
14
- if keyA == 1 :
14
+ if key_a == 1 :
15
15
sys .exit (
16
16
"The affine cipher becomes weak when key "
17
17
"A is set to 1. Choose different key"
18
18
)
19
- if keyB == 0 :
19
+ if key_b == 0 :
20
20
sys .exit (
21
21
"The affine cipher becomes weak when key "
22
22
"B is set to 0. Choose different key"
23
23
)
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 :
25
25
sys .exit (
26
26
"Key A must be greater than 0 and key B must "
27
27
f"be between 0 and { len (SYMBOLS ) - 1 } ."
28
28
)
29
- if cryptomath .gcd (keyA , len (SYMBOLS )) != 1 :
29
+ if cryptomath .gcd (key_a , len (SYMBOLS )) != 1 :
30
30
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 )} "
32
32
"are not relatively prime. Choose a different key."
33
33
)
34
34
@@ -39,16 +39,16 @@ def encrypt_message(key: int, message: str) -> str:
39
39
... 'substitution cipher.')
40
40
'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi'
41
41
"""
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 = ""
45
45
for symbol in message :
46
46
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 )]
49
49
else :
50
- cipherText += symbol
51
- return cipherText
50
+ cipher_text += symbol
51
+ return cipher_text
52
52
53
53
54
54
def decrypt_message (key : int , message : str ) -> str :
@@ -57,25 +57,27 @@ def decrypt_message(key: int, message: str) -> str:
57
57
... '{xIp~{HL}Gi')
58
58
'The affine cipher is a type of monoalphabetic substitution cipher.'
59
59
"""
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 ))
64
64
for symbol in message :
65
65
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
+ ]
68
70
else :
69
- plainText += symbol
70
- return plainText
71
+ plain_text += symbol
72
+ return plain_text
71
73
72
74
73
75
def get_random_key () -> int :
74
76
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
79
81
80
82
81
83
def main () -> None :
0 commit comments