1

Some libraries require that a public key is represented by an integer modulus and exponent. It's surprisingly hard to quickly convert a certificate or public key into that format. How can I convert that using bash?

1 Answer 1

9

Convert Certificate to Modulus, Exponent

The process for taking a certificate or a public key and turning it into integers is a surprisingly tough task as first glance. There's a long chain of decoding and encoding to various format that this page will describe.

1. Certificate to Public Key

$ CERTIFICATE_PATH=/path/to/file.cer
$ OUTPUT_PUBLIC_KEY_PATH=public_key.pem
$ openssl x509 -pubkey -noout -in $CERTIFICATE_PATH > $OUTPUT_PUBLIC_KEY_PATH

Creates the a public key (example output below, don't worry this is a sample cert, not real).

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArDMy/rsEEUfMQrrCSQmc
Lo9CSTvoL2jijXN1yi8jlx86yorxxm8Njdo0R2N6U0QRBLjglabeJJJZLJxQqPpU
RRFB7euTmmdLsctHxYVn2UDglIXV1Q5CGkSi9nFRupkL7ZMUflP0hqvPFRagUHaG
exJSlsO+32Gy6TxVIQKxAacjoYFJcMDGlDV4oTPelgMJ7enA7s4Bnz2tr6KJyoD3
N9fhcmFR0lGR7W7wqR79ePaxmPcCEl6UGAIbPny8WSQluRGzjsdB56dwsvdIZtlS
ChYr4tzoQYOB7cWc9gnJlQFOhocc/7tFPVXelT2Ff95Kp4eWGUK5JmzoU720B8Yk
EQIDAQAB
-----END PUBLIC KEY-----

2. Get Hexidecimal Modulus from Public Ke

$ HEX_MODULUS=$(openssl rsa -pubin -in $OUTPUT_PUBLIC_KEY_PATH  -modulus | grep 'Modulus=' |  cut -d'=' -f 2)

Creates the a hexidecimal representation of the modulus (example output below).

DD6DBDF880FAD7DE1B1FA7A32EB202E216F6520A3CBFA642F8CADC93674D60C34F8DC38A001BF1C44B416A69D269E53F218EC50BF82237ADB62C4B55FF7A0372BB9AD3EC96B9569FCB1999C932946F8FC652069F4503DFFDE897F6EAD6BABB482BB5E034614D52360FAB87522503CF87008713F2CA0329169D905746B5F40EAE17C80A4D92ED08A632231171FEF22C44D76C07F30B7B0C4BDD3BB4F737709F51B6884E5D6A057F8D9B667AAB8020FEEE6B97C3497D783BD599970375CE8FBCC5BE9C9AA5121970F9A4BD9627ED2302A7C757C971CF7694A22162F6B81DCA88EE09AD462FB761B32C1513869FA535265A67F437C8E68001490EC7ED61D3CDBCE4F8BE3FC94EF87D9789CE12BCCAB5C6D2E0D9B3683C2E4A9DB45FB853EE503DBFDDD4A28AB6A027AB980CB3B25890E2BCA1ADFFBD8E55310F00BF68E93DA9199AF06D0BA2146AC64CC64EBD6312A50B4D97EB42097953E265AA243470B8C1AB2380E79C6CEDDC82AA3704B8432A3D2AA8CC20FC275D902658F9B714E29EE2C1707397E96B028ED352597B00EC6130F1563F9CC17C05C5B136C81885CF61401F07E8A70687DF9A770BA9647203F693FCE00259C196ECC009423E30A27F1B482FFEE0218F5387250DCBEA49F54A9BD0E35FEE7818E5BA7131A904980FB1AD6752A0F2E39CAB6AFE588484DD073D3294051645159659A0586C180EE37766C7B3F799

3. Convert Hexadecimal Modulus to Decimal

$ ruby<<EOF
p "$HEX_MODULUS".to_i(16).to_s(10)
EOF

Converts the hexadecimal representation of the modulus to decimal (example output below).

"903350200741846433103490275702318092987711221795080780487705515793447642462352293930005029082816456324966429635984183642976753224939100182934586764830322547353276613511778555156314685902120045785748740652102149499802458636594115145098376676532039811405280029484180531080897744797671946602535748106482573745640230125725071851411418858523441305542960197934970282201978169899128421358221136498561735378441078904842496000031288381427214662674876164488503477712353985285783038861704346173846417987800700372136855065513839371230505066979655945991689080601315943347208545322825551901523372801711788730312786616209024413727099938085068143933277702040068400770002392724273077692527618673710507253235031640666038293670466879460643091688898398212470614257430485567355559709027230476557173161015264993692101931219217542706455704780898322106052146131050344223354172417918892290877128904693397579183166821072452886577300287605913021934410696419333315789150404571415571510150759313347067018155614134697544366299631877615175466702747992499691378170723240805893694504266094416954653431726471214487875228995333970243486471724362065194634035562833627383830413785525590101462126279287790122982746853775543354375554949279704170225740172092202881398339481"

4. Get Exponent from Public Key

$ openssl rsa -pubin -in $FILE -text | grep 'Exponent'

Gets the decimal exponent from the public key (example output below). Most people use 65537 as the exponent.

Exponent: 65537 (0x10001)
1
  • In step 4, $FILE should be $OUTPUT_PUBLIC_KEY_PATH to match the .pem generated earlier. Otherwise a fantastic answer. Thanks! Commented Jan 18 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.