156 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
- if myMode == 'encrypt':
- translated = transpositionEncrypt.encryptMessage(myKey, content)
- elif myMode == 'decrypt':
- translated = transpositionDecrypt.decryptMessage(myKey, content)
- totalTime = round(time.time() - startTime, 2)
- print('%sion time: %s seconds' % (myMode.title(), totalTime))
Write out the translated message to the output file.
- outputFileObj = open(outputFilename, 'w')
- outputFileObj.write(translated)
- outputFileObj.close()
- print('Done %sing %s (%s characters).' % (myMode, inputFilename,
len(content))) - print('%sed file is %s.' % (myMode.title(), outputFilename))
If transpositionCipherFile.py is run (instead of imported as a module)
call the main() function.
- if name == 'main':
- main()
In the directory that frankenstein.txt and transpositionFileCipher.py files are in, there will be a
new file named frankenstein.encrypted.txt that contains the content of frankenstein.txt in
encrypted form. If you double-click the file to open it, it should look something like this:
PtFiyedleo a arnvmt eneeGLchongnes Mmuyedlsu0#uiSHTGA r sy,n t ys
s nuaoGeL
sc7s,
(the rest has been cut out for brevity)
To decrypt, make the following changes to the source code (written in bold) and run the
transposition cipher program again:
transpositionFileCipher.py
- inputFilename = 'frankenstein.encrypted.txt'
BE CAREFUL! If a file with the outputFilename name already exists,
this program will overwrite that file.
- outputFilename = 'frankenstein.decrypted.txt'
- myKey = 10
- myMode = 'decrypt' # set to 'encrypt' or 'decrypt'
This time when you run the program a new file will appear in the folder named
frankenstein.decrypted.txt that is identical to the original frankenstein.txt file.