Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 73 lines (53 sloc) 4.083 kb
f16b361b »
2012-08-19 Initial commit.
1 # Vigenere Cipher (Polyalphabetic Cipher)
2 # http://inventwithpython.com/codebreaker (BSD Licensed)
91f8abbb »
2012-08-24 First round of personal fixes. This is an unhelpful log message.
3
c4fd6ab2 »
2012-08-25 Pylint-suggested fixes.
4 import pyperclip
f16b361b »
2012-08-19 Initial commit.
5
91f8abbb »
2012-08-24 First round of personal fixes. This is an unhelpful log message.
6 LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
f16b361b »
2012-08-19 Initial commit.
7
8 def main():
9 message = """Alan Mathison Turing was a British mathematician, logician, cryptanalyst, and computer scientist. He was highly influential in the development of computer science, providing a formalisation of the concepts of "algorithm" and "computation" with the Turing machine. Turing is widely considered to be the father of computer science and artificial intelligence. During World War II, Turing worked for the Government Code and Cypher School (GCCS) at Bletchley Park, Britain's codebreaking centre. For a time he was head of Hut 8, the section responsible for German naval cryptanalysis. He devised a number of techniques for breaking German ciphers, including the method of the bombe, an electromechanical machine that could find settings for the Enigma machine. After the war he worked at the National Physical Laboratory, where he created one of the first designs for a stored-program computer, the ACE. In 1948 Turing joined Max Newman's Computing Laboratory at Manchester University, where he assisted in the development of the Manchester computers and became interested in mathematical biology. He wrote a paper on the chemical basis of morphogenesis, and predicted oscillating chemical reactions such as the Belousov-Zhabotinsky reaction, which were first observed in the 1960s. Turing's homosexuality resulted in a criminal prosecution in 1952, when homosexual acts were still illegal in the United Kingdom. He accepted treatment with female hormones (chemical castration) as an alternative to prison. Turing died in 1954, just over two weeks before his 42nd birthday, from cyanide poisoning. An inquest determined that his death was suicide; his mother and some others believed his death was accidental. On 10 September 2009, following an Internet campaign, British Prime Minister Gordon Brown made an official public apology on behalf of the British government for "the appalling way he was treated." As of May 2012 a private member's bill was before the House of Lords which would grant Turing a statutory pardon if enacted."""
10 key = 'ASIMOV'
11 mode = 'encrypt' # set to 'encrypt' or 'decrypt'
12
13 message = message.upper()
14 key = key.upper()
15
16 if mode == 'encrypt':
17 translated = translateMessage(message, key, 'encrypt')
18 elif mode == 'decrypt':
19 translated = translateMessage(message, key, 'decrypt')
20
21 print('%sed message:' % (mode.title()))
22 print(translated)
23 pyperclip.copy(translated)
91f8abbb »
2012-08-24 First round of personal fixes. This is an unhelpful log message.
24 print()
f16b361b »
2012-08-19 Initial commit.
25 print('The message has been copied to the clipboard.')
26
27
28 def encryptMessage(message, key):
29 return translateMessage(message, key, 'encrypt')
30
31
32 def decryptMessage(message, key):
33 return translateMessage(message, key, 'decrypt')
34
35
36 def translateMessage(message, key, mode):
37 translated = [] # stores the encrypted/decrypted message string
38
39 keyIndex = 0
40 key = key.upper()
41 message = message.upper()
42
43 for symbol in message: # loop through each character in message
91f8abbb »
2012-08-24 First round of personal fixes. This is an unhelpful log message.
44 num = LETTERS.find(symbol)
f16b361b »
2012-08-19 Initial commit.
45 if num != -1:
46 if mode == 'encrypt':
c4fd6ab2 »
2012-08-25 Pylint-suggested fixes.
47 num += LETTERS.find(key[keyIndex]) # add if encrypting
f16b361b »
2012-08-19 Initial commit.
48 elif mode == 'decrypt':
c4fd6ab2 »
2012-08-25 Pylint-suggested fixes.
49 num -= LETTERS.find(key[keyIndex]) # subtract if decrypting
f16b361b »
2012-08-19 Initial commit.
50
51 # handle the potential wrap around
91f8abbb »
2012-08-24 First round of personal fixes. This is an unhelpful log message.
52 if num >= len(LETTERS):
53 num -= len(LETTERS)
f16b361b »
2012-08-19 Initial commit.
54 elif num < 0:
91f8abbb »
2012-08-24 First round of personal fixes. This is an unhelpful log message.
55 num += len(LETTERS)
f16b361b »
2012-08-19 Initial commit.
56
57 # add the encrypted/decrypted symbol to the end of translated.
91f8abbb »
2012-08-24 First round of personal fixes. This is an unhelpful log message.
58 translated.append(LETTERS[num])
f16b361b »
2012-08-19 Initial commit.
59
60 keyIndex += 1
61 if keyIndex == len(key):
62 keyIndex = 0
63 else:
91f8abbb »
2012-08-24 First round of personal fixes. This is an unhelpful log message.
64 # The symbol was not in LETTERS, so add it to translated as is.
f16b361b »
2012-08-19 Initial commit.
65 translated.append(symbol)
66
67 return ''.join(translated)
68
69
91f8abbb »
2012-08-24 First round of personal fixes. This is an unhelpful log message.
70 # If vigenereCipher.py is run (instead of imported as a module) call
71 # the main() function.
f16b361b »
2012-08-19 Initial commit.
72 if __name__ == '__main__':
c4fd6ab2 »
2012-08-25 Pylint-suggested fixes.
73 main()
Something went wrong with that request. Please try again.