Generating RSA Public Modulus, Public Exponent & Private Exponent As HexDecimals

I was messing around the other day with RSA encryption and came across the site http://nmichaels.org/rsa.py. It demonstrates how to use RSA encryption to encrypt/decrypt a text string. I was interested in the key generation for this, this page just has a generate button however, I wanted to understand how I could generate my own Public Modulus, Public Exponent & Private Exponent in the hexdecimal format.

On most linux distros the command openssl is available, or the package can easily be installed.

Generating the a 2048bit RSA PrivateKey which stores in a PEM file of private.pem  can be done by typing

openssl genrsa -out private.pem 2048

To see the Public Modulus, Public Exponent, & Private Exponent type:

openssl rsa -in private.pem -text -noout

This will generate the information required but in hexdecimal form seperated by a colon between each byte, which for the site above is not quite what is required without a lot of editing and search and replacing.  I found this which parses this information in the format required, albeit a little cryptic way:

openssl asn1parse < private.pem

Gives the following:

0:d=0 hl=4 l=1187 cons: SEQUENCE
4:d=1 hl=2 l= 1 prim: INTEGER :00
7:d=1 hl=4 l= 257 prim: INTEGER :
B0EC5B7FCD0FB865A386E70B1A8C5B71
57B655BA16C4E5434982179136BFBD73
127FB4CE5D592718D0F87DF685FC4C8F
07765F02963F8C7AC9BCF98967DC86E2
2D9A0A9A1A9C86E72D0BA00A2BAB887E
50FC7EABA65B491B1852EA6651A62724
6E3B5ECAB12D8CDD6735B02BEE0C7C1B
ABBD445E2DDC3C33715BE91A0E5AA7A7
980E2C43750A9DDE9F71C167B8305887
32E81C2D4F38B9343492DC9FB17C53C2
D653A3EF7A48918A67874FEE505C2F43
9EAC6E80EA1A313049E0B2724769F28F
B48E3B06D6841DA40D64771B5653E63F
D91A7DC5A5AF77C7346BDEE56DDA4142
D35032CDEB2CB9A54B464D53ADFF5242
D7D7E44FC885AE6DCE76E00C4B56A053
268:d=1 hl=2 l= 3 prim: INTEGER :010001
273:d=1 hl=4 l= 256 prim: INTEGER :
29E892F3FEFB0E4EE5217B7121E4C574
5FB68D2FFB5CC081E3D3B10970D29269
C3A4048C2D8884BE5821C494B4C042D2
4E8E378B4153E44EE1A2A5E96A74CE6E
9D26B40434F186F313B48140677FC661
4585C9EB97086094CE7A3593EF1B5082
4CAA3B95FFCA2E67F39EC01067DF0536
F8C36882769288A016E6129D5D7113ED
3639ADC192076AD874E381EB5D09C568
204DA636F3D1E46A3B234A8E74CD6675
EB6F31CDE19B25D851D713717D0C9942
61863B1F6741AED89E9E82925BAB2A3E
D52AA244A1DBCE9D3B63659AD48A795F
F1B6EEA29DE4B744774EB8E686BA34C4
F5D7F3C33D4CAFD73B0A676E321E9CB3
508734B98DB2B94AC29FC69370541BE1
533:d=1 hl=3 l= 129 prim: INTEGER :
D9BFA0D5E3130D31A3EAC326ED45AE44
A3DC3DF479069C524C4D0A4EC95E05A8
8417B6E88F8D1EC60F1E3CD204DC04B4
8EBEDEEECACA47855A80D6505B28A33D
676B0209F47BDD6381B4EC93EECB2A4B
4545417CAED237DBE46679F9F2491A8D
9B5E12F9C3263538AC0ED0150537DB63
D73DE627F424A20C01E32CF920D61FF5

Text lines highlighted above are hexdecimal representations the Public Modulus (lines 4-19), Public Exponent (line 20), Private Exponent (lines 22-37).  Copying and pasting those values into appropriate boxes in http://nmichaels.org/rsa.py works nicely.

Reference:
http://security.stackexchange.com/questions/29786/generating-private-key-from-hex-string-with-openssl