Hacking Secret Ciphers with Python

(Ann) #1
Chapter 24 – Public Key Cryptography and the RSA Cipher 395

size. Did you specify the correct key file and encrypted file?' % (blockSize *
8, keySize))
147.




  1. Convert the encrypted message into large int values.



  2. encryptedBlocks = []

  3. for block in encryptedMessage.split(','):

  4. encryptedBlocks.append(int(block))




  5. Decrypt the large int values.



  6. return decryptMessage(encryptedBlocks, messageLength, (n, d),
    blockSize)






  7. If rsaCipher.py is run (instead of imported as a module) call




  8. the main() function.



  9. if name == 'main':

  10. main()


Sample Run of the RSA Cipher Program


Once you have a public and private key file, you can send anyone your public file (or post it
somewhere online) so others can send you messages. If you want to send a secret message to
someone, first get their public key file and place it in the same directory as the rsaCipher.py
program. Set the message variable on line 19 to the string of the message to encrypt.


Make sure the mode variable is set to the string 'encrypt' on line 16, and set the
pubKeyFilename variable to the public key file’s filename on line 20. The filename
variable holds a string of the file that the ciphertext will be written to.


When you run the program, the output will look like this:


Encrypting and writing to encrypted_file.txt...
Encrypted text:
262128 99261588918914129248869521413561361425429438626950729912505980066002708
9830015533870663668185646157509007528457226336261821873976954531347724960840148
5234147843064609273929706353514554444810285427183303767133366827434264155196422
0917826499299282445350219039270525853857169256807439317455881433369973441896615
9641434946805896304802494813292321784924727694126957902732539670170912919151008
4539012275457327046892059514600198713235394985023008043572425418307615110483262
2796568393228930000619315738939341534920563203314816419962044702016227849752350
41470244964996075123464854629954207517620745550909143567815440815430367,6684261
3553841756289795361296785769122909029892643608575548034344009592725547265584325
2331933112765122922637923600156910575424723444966430139306688707256391991191466
4504822721492217530056774346964092597494522555496959638903763181124233744530745

Free download pdf