1
- import sys , random , cryptomath_module as cryptoMath
1
+ import random
2
+ import sys
2
3
3
- SYMBOLS = r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""
4
+ import cryptomath_module as cryptomath
5
+
6
+ SYMBOLS = (r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
7
+ r"""abcdefghijklmnopqrstuvwxyz{|}~""" )
4
8
5
9
6
10
def main ():
7
- message = input ("Enter message: " )
8
- key = int (input ("Enter key [2000 - 9000]: " ))
9
- mode = input ("Encrypt/Decrypt [E/D]: " )
11
+ """
12
+ >>> key = get_random_key()
13
+ >>> msg = "This is a test!"
14
+ >>> decrypt_message(key, encrypt_message(key, msg)) == msg
15
+ True
16
+ """
17
+ message = input ("Enter message: " ).strip ()
18
+ key = int (input ("Enter key [2000 - 9000]: " ).strip ())
19
+ mode = input ("Encrypt/Decrypt [E/D]: " ).strip ().lower ()
10
20
11
- if mode .lower (). startswith ("e" ):
21
+ if mode .startswith ("e" ):
12
22
mode = "encrypt"
13
- translated = encryptMessage (key , message )
14
- elif mode .lower (). startswith ("d" ):
23
+ translated = encrypt_message (key , message )
24
+ elif mode .startswith ("d" ):
15
25
mode = "decrypt"
16
- translated = decryptMessage (key , message )
17
- print ("\n %sed text: \n %s" % (mode .title (), translated ))
18
-
19
-
20
- def getKeyParts (key ):
21
- keyA = key // len (SYMBOLS )
22
- keyB = key % len (SYMBOLS )
23
- return (keyA , keyB )
24
-
25
-
26
- def checkKeys (keyA , keyB , mode ):
27
- if keyA == 1 and mode == "encrypt" :
28
- sys .exit (
29
- "The affine cipher becomes weak when key A is set to 1. Choose different key"
30
- )
31
- if keyB == 0 and mode == "encrypt" :
32
- sys .exit (
33
- "The affine cipher becomes weak when key A is set to 1. Choose different key"
34
- )
26
+ translated = decrypt_message (key , message )
27
+ print (f"\n { mode .title ()} ed text: \n { translated } " )
28
+
29
+
30
+ def check_keys (keyA , keyB , mode ):
31
+ if mode == "encrypt" :
32
+ if keyA == 1 :
33
+ sys .exit (
34
+ "The affine cipher becomes weak when key "
35
+ "A is set to 1. Choose different key"
36
+ )
37
+ if keyB == 0 :
38
+ sys .exit (
39
+ "The affine cipher becomes weak when key "
40
+ "B is set to 0. Choose different key"
41
+ )
35
42
if keyA < 0 or keyB < 0 or keyB > len (SYMBOLS ) - 1 :
36
43
sys .exit (
37
- "Key A must be greater than 0 and key B must be between 0 and %s. "
38
- % ( len (SYMBOLS ) - 1 )
44
+ "Key A must be greater than 0 and key B must "
45
+ f"be between 0 and { len (SYMBOLS ) - 1 } ."
39
46
)
40
- if cryptoMath .gcd (keyA , len (SYMBOLS )) != 1 :
47
+ if cryptomath .gcd (keyA , len (SYMBOLS )) != 1 :
41
48
sys .exit (
42
- "Key A %s and the symbol set size %s are not relatively prime. Choose a different key. "
43
- % ( keyA , len ( SYMBOLS ))
49
+ f "Key A { keyA } and the symbol set size { len ( SYMBOLS ) } "
50
+ "are not relatively prime. Choose a different key."
44
51
)
45
52
46
53
47
- def encryptMessage (key , message ) :
54
+ def encrypt_message (key : int , message : str ) -> str :
48
55
"""
49
- >>> encryptMessage (4545, 'The affine cipher is a type of monoalphabetic substitution cipher.')
56
+ >>> encrypt_message (4545, 'The affine cipher is a type of monoalphabetic substitution cipher.')
50
57
'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi'
51
58
"""
52
- keyA , keyB = getKeyParts (key )
53
- checkKeys (keyA , keyB , "encrypt" )
59
+ keyA , keyB = divmod (key , len ( SYMBOLS ) )
60
+ check_keys (keyA , keyB , "encrypt" )
54
61
cipherText = ""
55
62
for symbol in message :
56
63
if symbol in SYMBOLS :
@@ -61,15 +68,15 @@ def encryptMessage(key, message):
61
68
return cipherText
62
69
63
70
64
- def decryptMessage (key , message ) :
71
+ def decrypt_message (key : int , message : str ) -> str :
65
72
"""
66
- >>> decryptMessage (4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi')
73
+ >>> decrypt_message (4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi')
67
74
'The affine cipher is a type of monoalphabetic substitution cipher.'
68
75
"""
69
- keyA , keyB = getKeyParts (key )
70
- checkKeys (keyA , keyB , "decrypt" )
76
+ keyA , keyB = divmod (key , len ( SYMBOLS ) )
77
+ check_keys (keyA , keyB , "decrypt" )
71
78
plainText = ""
72
- modInverseOfkeyA = cryptoMath .findModInverse (keyA , len (SYMBOLS ))
79
+ modInverseOfkeyA = cryptomath .findModInverse (keyA , len (SYMBOLS ))
73
80
for symbol in message :
74
81
if symbol in SYMBOLS :
75
82
symIndex = SYMBOLS .find (symbol )
@@ -79,11 +86,11 @@ def decryptMessage(key, message):
79
86
return plainText
80
87
81
88
82
- def getRandomKey ():
89
+ def get_random_key ():
83
90
while True :
84
91
keyA = random .randint (2 , len (SYMBOLS ))
85
92
keyB = random .randint (2 , len (SYMBOLS ))
86
- if cryptoMath .gcd (keyA , len (SYMBOLS )) == 1 :
93
+ if cryptomath .gcd (keyA , len (SYMBOLS )) == 1 :
87
94
return keyA * len (SYMBOLS ) + keyB
88
95
89
96
0 commit comments