如何将证书转换为十进制模数和指数

时间:2018-07-06 22:49:44

标签: ssl-certificate public-key-encryption modulus exponent

某些库要求公用密钥用整数模数和指数表示。很难将证书或公钥快速转换为该格式。如何使用bash进行转换?

1 个答案:

答案 0 :(得分:3)

将证书转换为指数模量

乍一看,获取证书或公共密钥并将其转换为整数的过程是一项令人惊讶的艰巨任务。此页面将描述一长串的各种格式的解码和编码。

1。公钥证书

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

创建一个公共密钥(下面的示例输出,不用担心这是示例证书,不是真实的)。

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

2。从公共Ke获取十六进制模量

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

创建模数的十六进制表示形式(下面的示例输出)。

DD6DBDF880FAD7DE1B1FA7A32EB202E216F6520A3CBFA642F8CADC93674D60C34F8DC38A001BF1C44B416A69D269E53F218EC50BF82237ADB62C4B55FF7A0372BB9AD3EC96B9569FCB1999C932946F8FC652069F4503DFFDE897F6EAD6BABB482BB5E034614D52360FAB87522503CF87008713F2CA0329169D905746B5F40EAE17C80A4D92ED08A632231171FEF22C44D76C07F30B7B0C4BDD3BB4F737709F51B6884E5D6A057F8D9B667AAB8020FEEE6B97C3497D783BD599970375CE8FBCC5BE9C9AA5121970F9A4BD9627ED2302A7C757C971CF7694A22162F6B81DCA88EE09AD462FB761B32C1513869FA535265A67F437C8E68001490EC7ED61D3CDBCE4F8BE3FC94EF87D9789CE12BCCAB5C6D2E0D9B3683C2E4A9DB45FB853EE503DBFDDD4A28AB6A027AB980CB3B25890E2BCA1ADFFBD8E55310F00BF68E93DA9199AF06D0BA2146AC64CC64EBD6312A50B4D97EB42097953E265AA243470B8C1AB2380E79C6CEDDC82AA3704B8432A3D2AA8CC20FC275D902658F9B714E29EE2C1707397E96B028ED352597B00EC6130F1563F9CC17C05C5B136C81885CF61401F07E8A70687DF9A770BA9647203F693FCE00259C196ECC009423E30A27F1B482FFEE0218F5387250DCBEA49F54A9BD0E35FEE7818E5BA7131A904980FB1AD6752A0F2E39CAB6AFE588484DD073D3294051645159659A0586C180EE37766C7B3F799

3。将十六进制模量转换为十进制

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

将模数的十六进制表示转换为十进制(以下示例输出)。

"903350200741846433103490275702318092987711221795080780487705515793447642462352293930005029082816456324966429635984183642976753224939100182934586764830322547353276613511778555156314685902120045785748740652102149499802458636594115145098376676532039811405280029484180531080897744797671946602535748106482573745640230125725071851411418858523441305542960197934970282201978169899128421358221136498561735378441078904842496000031288381427214662674876164488503477712353985285783038861704346173846417987800700372136855065513839371230505066979655945991689080601315943347208545322825551901523372801711788730312786616209024413727099938085068143933277702040068400770002392724273077692527618673710507253235031640666038293670466879460643091688898398212470614257430485567355559709027230476557173161015264993692101931219217542706455704780898322106052146131050344223354172417918892290877128904693397579183166821072452886577300287605913021934410696419333315789150404571415571510150759313347067018155614134697544366299631877615175466702747992499691378170723240805893694504266094416954653431726471214487875228995333970243486471724362065194634035562833627383830413785525590101462126279287790122982746853775543354375554949279704170225740172092202881398339481"

4。从公钥获取指数

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

从公钥获取小数指数(下面的示例输出)。大多数人使用65537作为指数。

Exponent: 65537 (0x10001)