Hacking Secret Ciphers with Python

(Ann) #1

390 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


RSA Key File Format


makeRsaKeys.py


  1. print()

  2. print('The public key is a %s and a %s digit number.' %
    (len(str(publicKey[0])), len(str(publicKey[1]))))

  3. print('Writing public key to file %s_pubkey.txt...' % (name))

  4. fo = open('%s_pubkey.txt' % (name), 'w')

  5. fo.write('%s,%s,%s' % (keySize, publicKey[0], publicKey[1]))

  6. fo.close()


Line 57 prints some information about the public key. It can tell how many digits are in the
integer in publicKey[0] and publicKey[1] by converting those values to strings with the
str() function, and then finding the length of the string with the len() function.


The key file’s contents will be the key size, a comma, the n integer, another comma, and the e (or
d) integer. The file’s contents will look like: ,,


Lines 59 to 61 open a file in write mode, as you can tell from the 'w' string passed to open().


makeRsaKeys.py


  1. print()

  2. print('The private key is a %s and a %s digit number.' %
    (len(str(publicKey[0])), len(str(publicKey[1]))))

  3. print('Writing private key to file %s_privkey.txt...' % (name))

  4. fo = open('%s_privkey.txt' % (name), 'w')

  5. fo.write('%s,%s,%s' % (keySize, privateKey[0], privateKey[1]))

  6. fo.close()


Lines 63 to 68 do the exact same thing as lines 56 and 61, except for writing the private key out to
a file.


makeRsaKeys.py
7 1. # If makeRsaKeys.py is run (instead of imported as a module) call




  1. the main() function.



  2. if name == 'main':

  3. main()


Lines 73 and 74 are at the bottom of the program, and call main() if makeRsaKeys.py is being
run as a program instead of imported as a module by another program.

Free download pdf