216 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
- if cryptomath.gcd(keyA, len(SYMBOLS)) == 1:
- return keyA * len(SYMBOLS) + keyB
If affineCipher.py is run (instead of imported as a module) call
the main() function.
- if name == 'main':
- 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
Affine Cipher
http://inventwithpython.com/hacking (BSD Licensed)
- import sys, pyperclip, cryptomath, random
- 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.