Hacking Secret Ciphers with Python

(Ann) #1

216 http://inventwithpython.com/hacking


Email questions to the author: [email protected]



  1. if cryptomath.gcd(keyA, len(SYMBOLS)) == 1:

  2. return keyA * len(SYMBOLS) + keyB






  3. If affineCipher.py is run (instead of imported as a module) call




  4. the main() function.



  5. if name == 'main':

  6. main()


Sample Run of the Affine Cipher Program


When you press F5 from the file editor to run this program, the output will look like this:


Key: 2023
Encrypted text:
fX<h>}(rTH<Rh()?<?T]TH=T<rh<tT<))T?<ISrT))I~TSr<Ii<Ir<h()?<?TTI=T<<4(>_S<
ISrh<tT)IT=IS~<r4r<Ir<R]<4(>_SEf<0X)_S<
k(HIS~
Full encrypted text copied to clipboard.


The message “"A computer would deserve to be called intelligent if it could deceive a human into
believing that it was human." -Alan Turing” gets encrypted with the key 2023 into the above
ciphertext.


To decrypt, paste this text as the new value to be stored in myMessage and change myMode to
the string 'decrypt'.


Practice Exercises, Chapter 15, Set A


Practice exercises can be found at http://invpy.com/hackingpractice 15 A.


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


affineCipher.py



  1. Affine Cipher




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





  3. import sys, pyperclip, cryptomath, random

  4. SYMBOLS = """ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]
    ^_`abcdefghijklmnopqrstuvwxyz{|}~""" # note the space at the front


Lines 1 and 2 are the usual comments describing what the program is. There is also an import
statement for the modules used in this program.

Free download pdf