390 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
RSA Key File Format
makeRsaKeys.py
- print()
- print('The public key is a %s and a %s digit number.' %
(len(str(publicKey[0])), len(str(publicKey[1])))) - print('Writing public key to file %s_pubkey.txt...' % (name))
- fo = open('%s_pubkey.txt' % (name), 'w')
- fo.write('%s,%s,%s' % (keySize, publicKey[0], publicKey[1]))
- 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
- print()
- print('The private key is a %s and a %s digit number.' %
(len(str(publicKey[0])), len(str(publicKey[1])))) - print('Writing private key to file %s_privkey.txt...' % (name))
- fo = open('%s_privkey.txt' % (name), 'w')
- fo.write('%s,%s,%s' % (keySize, privateKey[0], privateKey[1]))
- 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
the main() function.
- if name == 'main':
- 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.