forked from asweigart/codebreaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleSubKeyword.py
51 lines (36 loc) · 1.35 KB
/
simpleSubKeyword.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Simple Substitution Keyword Cipher
# http://inventwithpython.com/codebreaker (BSD Licensed)
import pyperclip, simpleSubCipher
def main():
myMessage = r"""This is my secret message."""
myKey = 'alphanumeric'
myMode = 'encrypt' # set to 'encrypt' or 'decrypt'
print('The key used is:')
print(makeSimpleSubKey(myKey))
if myMode == 'encrypt':
translated = encryptMessage(myKey, myMessage)
elif myMode == 'decrypt':
translated = decryptMessage(myKey, myMessage)
print('The %sed message is:' % (myMode))
print(translated)
pyperclip.copy(translated)
print()
print('This message has been copied to the clipboard.')
def encryptMessage(key, message):
key = makeSimpleSubKey(key)
return simpleSubCipher.encryptMessage(key, message)
def decryptMessage(key, message):
key = makeSimpleSubKey(key)
return simpleSubCipher.decryptMessage(key, message)
def makeSimpleSubKey(keyword):
# create the key from the keyword
newKey = ''
keyAlphabet = list(' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~')
for i in range(len(keyword)):
if keyword[i] not in newKey:
newKey += keyword[i]
keyAlphabet.remove(keyword[i])
key = newKey + ''.join(keyAlphabet)
return key
if __name__ == '__main__':
main()