MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / checkKey

Function checkKey

src/gamesbyexample/affinecipher.py:79–96  ·  view source on GitHub ↗

Return True if key is a valid encryption key for this mode. Otherwise return False.

(key, mode)

Source from the content-addressed store, hash-verified

77
78
79def checkKey(key, mode):
80 """Return True if key is a valid encryption key for this mode.
81 Otherwise return False."""
82 keyA, keyB = getKeyPartsFromKey(key)
83 if mode == 'encrypt' and keyA == 1 and keyB == 0:
84 print('This key effectively does not do any encryption on the')
85 print('message. Choose a different key.')
86 return False
87 elif keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1:
88 print('Key A must be greater than 0 and Key B must be between')
89 print('0 and {}.'.format(len(SYMBOLS) - 1))
90 return False
91 elif gcd(keyA, len(SYMBOLS)) != 1:
92 print('Key A ({}) and the symbol set'.format(keyA))
93 print('size ({}) are not relatively prime.'.format(len(SYMBOLS)))
94 print('Choose a different key.')
95 return False
96 return True
97
98def encryptMessage(key, message):
99 """Encrypt the message using the key."""

Callers 3

mainFunction · 0.70
encryptMessageFunction · 0.70
decryptMessageFunction · 0.70

Calls 2

getKeyPartsFromKeyFunction · 0.85
gcdFunction · 0.85

Tested by

no test coverage detected