Hacking Secret Ciphers with Python

(Ann) #1

396 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


2041948917261094688708004245747998030244635761849845611609053856921438831555343
2751213283486646600584040245146570901217502941710992503572482408074196762322544
6680099823178790059243202224297039960462494558200472899766913932921695002362188
1996217713713494770944644417894970293643840346744192412614346008019737829011867
03144271104078294839144290043228508639879193883889311384,7277016624458973047704
0806680156575455285570435553143790299815533233656061333313422971390933175290260
5817773458688756774589737014227054621841244485285514206025269405528441594535085
0536174716382559790627193026256934316461174349640238168693204610463496242533658
4736211406286896178786120454116459075038688037119234659059503824465257190001591
9094263967757274610514128826270203357049019841335033192183418122067029417580137
3024013553583624428117568253845170657841567369678118510784447456725765265002966
2854459043617323327066630863887606386875040688709377112851144150781493772858323
25922978358897651126143551277531003851780


At the start of the text is 262 (which is the original message length), followed by an underscore
and then 128 (which is the “block size”; block sizes are explained later). If you look carefully at
the long string of digits after that, you will find two commas. The message is encrypted into three
very large integers, separated by commas. These integers are the encrypted form of the string in
the message variable on line 19.


To decrypt, change the mode variable to 'decrypt' and run the program again. Make sure
privKeyFilename on line 28 is set to the filename of the private key file and that this file is
in the same folder as rsaCipher.py. When you run the program, the output on the screen will look
like this:


Reading from encrypted_file.txt and decrypting...
Decrypted text:
"Journalists belong in the gutter because that is where the ruling classes
throw their guilty secrets." -Gerald Priestland "The Founding Fathers gave the
free press the protection it must have to bare the secrets of government and
inform the people." -Hugo Black


Note that the way the RSA cipher program is implemented, it can only encrypt and decrypt
plaintext files. A plaintext file (not to be confused with “plaintext” in the cryptography sense) is
a file that only contains text characters (like the kind that you can type on your keyboard). For
example, the .py files that you type for your Python programs are plaintext files. Plaintext files
are the type created with text editor software such as Notepad (on Windows), TextMate (on OS
X), or Gedit (on Ubuntu). Specifically, plaintext files are files that only contain ASCII values
(which are described later in this chapter).


Files such as images, videos, executable programs, or word processor files are called binary


files.(Word processor files are binary files because their text has font, color, and size information

Free download pdf