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

Function main

src/gamesbyexample/affinecipher.py:20–69  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

18
19
20def main():
21 print('''Affine Cipher, by Al Sweigart al@inventwithpython.com
22The affine cipher is a simple substitution cipher that uses addition and
23multiplication to encrypt and decrypt symbols.''')
24
25 # Let the user specify if they are encrypting or decrypting:
26 while True: # Keep asking until the user enters e or d.
27 print('Do you want to (e)ncrypt or (d)ecrypt?')
28 response = input('> ').lower()
29 if response.startswith('e'):
30 myMode = 'encrypt'
31 break
32 elif response.startswith('d'):
33 myMode = 'decrypt'
34 break
35 print('Please enter the letter e or d.')
36
37 # Let the user specify the key to use:
38 while True: # Keep asking until the user enters a valid key.
39 print('Please specify the key to use,')
40 print('or RANDOM to have one generated for you:')
41 response = input('> ').upper()
42 if response == 'RANDOM':
43 myKey = generateRandomKey()
44 print('The key is {}. KEEP THIS SECRET!'.format(myKey))
45 break
46 else:
47 if not response.isdecimal():
48 print('This key is not a number.')
49 continue
50 if checkKey(int(response), myMode):
51 myKey = int(response)
52 break
53
54 # Let the user specify the message to encrypt/decrypt:
55 print('Enter the message to {}.'.format(myMode))
56 myMessage = input('> ')
57
58 if myMode == 'encrypt':
59 translated = encryptMessage(myKey, myMessage)
60 elif myMode == 'decrypt':
61 translated = decryptMessage(myKey, myMessage)
62 print('%sed text:' % (myMode.title()))
63 print(translated)
64
65 try:
66 pyperclip.copy(translated)
67 print('Full %sed text copied to clipboard.' % (myMode))
68 except:
69 pass # Do nothing if pyperclip wasn't installed.
70
71
72def getKeyPartsFromKey(key):

Callers 1

affinecipher.pyFile · 0.70

Calls 4

generateRandomKeyFunction · 0.70
checkKeyFunction · 0.70
encryptMessageFunction · 0.70
decryptMessageFunction · 0.70

Tested by

no test coverage detected