Hacking Secret Ciphers with Python

(Ann) #1
Chapter 17 – The Simple Substitution Cipher 239

Sample Run of the Simple Substitution Cipher Program


When you run this program, the output will look like this:


Using key LFWOAYUISVKMNXPBDCRJTQEGHZ
The encrypted message is:
Sy l nlx sr pyyacao l ylwj eiswi upar lulsxrj isr sxrjsxwjr, ia esmm rwctjsxsza
sj wmpramh, lxo txmarr jia aqsoaxwa sr pqaceiamnsxu, ia esmm caytra jp famsaqa
sj. Sy, px jia pjiac ilxo, ia sr pyyacao rpnajisxu eiswi lyypcor l calrpx ypc
lwjsxu sx lwwpcolxwa jp isr sxrjsxwjr, ia esmm lwwabj sj aqax px jia rmsuijarj
aqsoaxwa. Jia pcsusx py nhjir sr agbmlsxao sx jisr elh. -Facjclxo Ctrramm


This message has been copied to the clipboard.


Notice that if the letter in the plaintext was lowercase, it will be lowercase in the ciphertext. If the
letter was uppercase in the plaintext, it will be uppercase in the ciphertext. The simple
substitution cipher does not encrypt spaces or punctuation marks. (Although the end of this
chapter explains how to modify the program to encrypt those characters too.)


To decrypt this ciphertext, paste it as the value for the myMessage variable on line 10 and
change myMode to the string 'decrypt'. Then run the program again. The output will look
like this:


Using key LFWOAYUISVKMNXPBDCRJTQEGHZ
The decrypted message is:
If a man is offered a fact which goes against his instincts, he will scrutinize
it closely, and unless the evidence is overwhelming, he will refuse to believe
it. If, on the other hand, he is offered something which affords a reason for
acting in accordance to his instincts, he will accept it even on the slightest
evidence. The origin of myths is explained in this way. -Bertrand Russell


This message has been copied to the clipboard.


How the Program Works..........................................................................................................................................


simpleSubCipher.py



  1. Simple Substitution Cipher




  2. http://inventwithpython.com/hacking (BSD Licensed)





  3. import pyperclip, sys, random





  4. LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

Free download pdf