Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 51 lines (36 sloc) 1.38 kb
f16b361b »
2012-08-19 Initial commit.
1 # Simple Substitution Keyword Cipher
2 # http://inventwithpython.com/codebreaker (BSD Licensed)
3
4 import pyperclip, simpleSubCipher
5
6 def main():
7 myMessage = r"""This is my secret message."""
8 myKey = 'alphanumeric'
9 myMode = 'encrypt' # set to 'encrypt' or 'decrypt'
10
11
12 print('The key used is:')
13 print(makeSimpleSubKey(myKey))
14
15 if myMode == 'encrypt':
16 translated = encryptMessage(myKey, myMessage)
17 elif myMode == 'decrypt':
18 translated = decryptMessage(myKey, myMessage)
19
20 print('The %sed message is:' % (myMode))
21 print(translated)
22
23 pyperclip.copy(translated)
24 print()
25 print('This message has been copied to the clipboard.')
26
27
28 def encryptMessage(key, message):
29 key = makeSimpleSubKey(key)
30 return simpleSubCipher.encryptMessage(key, message)
31
32
33 def decryptMessage(key, message):
34 key = makeSimpleSubKey(key)
35 return simpleSubCipher.decryptMessage(key, message)
36
37
38 def makeSimpleSubKey(keyword):
39 # create the key from the keyword
40 newKey = ''
41 keyAlphabet = list(' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~')
42 for i in range(len(keyword)):
43 if keyword[i] not in newKey:
44 newKey += keyword[i]
45 keyAlphabet.remove(keyword[i])
46 key = newKey + ''.join(keyAlphabet)
47 return key
48
49
50 if __name__ == '__main__':
51 main()
Something went wrong with that request. Please try again.