Hacking Secret Ciphers with Python

(Ann) #1

326 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


Source Code for the Vigenère Hacking Program


Open a new file editor window by clicking on File ► New Window. Type in the following code
into the file editor, and then save it as vigenereHacker.py. Press F5 to run the program. Note that
first you will need to download the pyperclip.py module and place this file in the same directory
as the vigenereHacker.py file. You can download this file from http://invpy.com/pyperclip.py.


The ciphertext in this program may be difficult to copy from the book, but you can copy & paste
it from http://invpy.com/vigenereHacking.py. You can see if there are any differences between
the text in your program to the text of the program in this book by using the online diff tool at
http://invpy.com/hackingdiff.


Source code for vigenereHacker.py




  1. Vigenere Cipher Hacker




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





  3. import itertools, re

  4. import vigenereCipher, pyperclip, freqAnalysis, detectEnglish



  5. LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

  6. SILENT_MODE = False # if set to True, program doesn't print attempts

  7. NUM_MOST_FREQ_LETTERS = 4 # attempts this many letters per subkey

  8. MAX_KEY_LENGTH = 16 # will not attempt keys longer than this

  9. NONLETTERS_PATTERN = re.compile('[^A-Z]')





  10. def main():


  11. Instead of typing this ciphertext out, you can copy & paste it




  12. from http://invpy.com/vigenereHacker.py



  13. ciphertext = """Adiz Avtzqeci Tmzubb wsa m Pmilqev halpqavtakuoi,
    lgouqdaf, kdmktsvmztsl, izr xoexghzr kkusitaaf. Vz wsa twbhdg ubalmmzhdad qz
    hce vmhsgohuqbo ox kaakulmd gxiwvos, krgdurdny i rcmmstugvtawz ca tzm ocicwxfg
    jf "stscmilpy" oid "uwydptsbuci" wabt hce Lcdwig eiovdnw. Bgfdny qe kddwtk
    qjnkqpsmev ba pz tzm roohwz at xoexghzr kkusicw izr vrlqrwxist uboedtuuznum.
    Pimifo Icmlv Emf DI, Lcdwig owdyzd xwd hce Ywhsmnemzh Xovm mby Cqxtsm Supacg
    (GUKE) oo Bdmfqclwg Bomk, Tzuhvif'a ocyetzqofifo ositjm. Rcm a lqys ce oie vzav
    wr Vpt 8, lpq gzclqab mekxabnittq tjr Ymdavn fihog cjgbhvnstkgds. Zm psqikmp o
    iuejqf jf lmoviiicqg aoj jdsvkavs Uzreiz qdpzmdg, dnutgrdny bts helpar jf lpq
    pjmtm, mb zlwkffjmwktoiiuix avczqzs ohsb ocplv nuby swbfwigk naf ohw Mzwbms
    umqcifm. Mtoej bts raj pq kjrcmp oo tzm Zooigvmz Khqauqvl Dincmalwdm, rhwzq vz
    cjmmhzd gvq ca tzm rwmsl lqgdgfa rcm a kbafzd-hzaumae kaakulmd, hce SKQ. Wi
    1948 Tmzubb jgqzsy Msf Zsrmsv'e Qjmhcfwig Dincmalwdm vt Eizqcekbqf Pnadqfnilg,
    ivzrw pq onsaafsy if bts yenmxckmwvf ca tzm Yoiczmehzr uwydptwze oid tmoohe
    avfsmekbqr dn eifvzmsbuqvl tqazjgq. Pq kmolm m dvpwz ab ohw ktshiuix pvsaa at
    hojxtcbefmewn, afl bfzdakfsy okkuzgalqzu xhwuuqvl jmmqoigve gpcz ie hce

Free download pdf