-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Variable renamings, moving code around in a better order to be explai…
…ned in the book, and simplifying code.
- Loading branch information
Showing
3 changed files
with
117 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,69 @@ | ||
# Vigenere Cipher (Polyalphabetic Substitution Cipher) | ||
# http://inventwithpython.com/hacking (BSD Licensed) | ||
|
||
import pyperclip | ||
|
||
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
|
||
def main(): | ||
# This text can be copy/pasted from http://invpy.com/vigenereCipher.py | ||
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.""" | ||
key = 'ASIMOV' | ||
mode = 'encrypt' # set to 'encrypt' or 'decrypt' | ||
|
||
if mode == 'encrypt': | ||
translated = encryptMessage(key, message) | ||
elif mode == 'decrypt': | ||
translated = decryptMessage(key, message) | ||
|
||
print('%sed message:' % (mode.title())) | ||
print(translated) | ||
pyperclip.copy(translated) | ||
print() | ||
print('The message has been copied to the clipboard.') | ||
|
||
|
||
def encryptMessage(key, message): | ||
return translateMessage(key, message, 'encrypt') | ||
|
||
|
||
def decryptMessage(key, message): | ||
return translateMessage(key, message, 'decrypt') | ||
|
||
|
||
def translateMessage(key, message, mode): | ||
translated = [] # stores the encrypted/decrypted message string | ||
|
||
keyIndex = 0 | ||
key = key.upper() | ||
|
||
for symbol in message: # loop through each character in message | ||
num = LETTERS.find(symbol.upper()) | ||
if num != -1: | ||
if mode == 'encrypt': | ||
num += LETTERS.find(key[keyIndex]) # add if encrypting | ||
elif mode == 'decrypt': | ||
num -= LETTERS.find(key[keyIndex]) # subtract if decrypting | ||
|
||
# handle the potential wrap around | ||
if num >= len(LETTERS): | ||
num -= len(LETTERS) | ||
elif num < 0: | ||
num += len(LETTERS) | ||
|
||
# add the encrypted/decrypted symbol to the end of translated. | ||
if symbol.isupper(): | ||
translated.append(LETTERS[num]) | ||
elif symbol.islower(): | ||
translated.append(LETTERS[num].lower()) | ||
|
||
keyIndex += 1 | ||
if keyIndex == len(key): | ||
keyIndex = 0 | ||
else: | ||
# The symbol was not in LETTERS, so add it to translated as is. | ||
translated.append(symbol) | ||
|
||
return ''.join(translated) | ||
|
||
|
||
# If vigenereCipher.py is run (instead of imported as a module) call | ||
# the main() function. | ||
if __name__ == '__main__': | ||
# Vigenere Cipher (Polyalphabetic Substitution Cipher) | ||
# http://inventwithpython.com/hacking (BSD Licensed) | ||
|
||
import pyperclip | ||
|
||
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
|
||
def main(): | ||
# This text can be copy/pasted from http://invpy.com/vigenereCipher.py | ||
myMessage = """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.""" | ||
myKey = 'ASIMOV' | ||
myMode = 'encrypt' # set to 'encrypt' or 'decrypt' | ||
|
||
if myMode == 'encrypt': | ||
translated = encryptMessage(myKey, myMessage) | ||
elif myMode == 'decrypt': | ||
translated = decryptMessage(myKey, myMessage) | ||
|
||
print('%sed message:' % (myMode.title())) | ||
print(translated) | ||
pyperclip.copy(translated) | ||
print() | ||
print('The message has been copied to the clipboard.') | ||
|
||
|
||
def encryptMessage(key, message): | ||
return translateMessage(key, message, 'encrypt') | ||
|
||
|
||
def decryptMessage(key, message): | ||
return translateMessage(key, message, 'decrypt') | ||
|
||
|
||
def translateMessage(key, message, mode): | ||
translated = [] # stores the encrypted/decrypted message string | ||
|
||
keyIndex = 0 | ||
key = key.upper() | ||
|
||
for symbol in message: # loop through each character in message | ||
num = LETTERS.find(symbol.upper()) | ||
if num != -1: # -1 means symbol.upper() was not found in LETTERS | ||
if mode == 'encrypt': | ||
num += LETTERS.find(key[keyIndex]) # add if encrypting | ||
elif mode == 'decrypt': | ||
num -= LETTERS.find(key[keyIndex]) # subtract if decrypting | ||
|
||
num %= len(LETTERS) # handle the potential wrap around | ||
|
||
# add the encrypted/decrypted symbol to the end of translated. | ||
if symbol.isupper(): | ||
translated.append(LETTERS[num]) | ||
elif symbol.islower(): | ||
translated.append(LETTERS[num].lower()) | ||
|
||
keyIndex += 1 # move to the next letter in the key | ||
if keyIndex == len(key): | ||
keyIndex = 0 | ||
else: | ||
# The symbol was not in LETTERS, so add it to translated as is. | ||
translated.append(symbol) | ||
|
||
return ''.join(translated) | ||
|
||
|
||
# If vigenereCipher.py is run (instead of imported as a module) call | ||
# the main() function. | ||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters