From ExpKey + Modkey to PEM

谁都会走 提交于 2019-12-24 12:07:39

问题


I have for example

EXPKEY : 010001
MODKEY : 00A5AC1136D1CE663E37E9F40F36C84F208FF0BEDE08BEE53B83A7BF79A39EA86E13F82893ACE0FB877BAC2FCAFB931D8C6CAB1D7A343072EB0D3499D2538E5B8DDA346BE333293BD6BDC9949752BCFCDE94FE7F74E92EBA4EB22016CEB27D0B538F843DF76F31D3BB9ADD4CEE48F80BF8B6284B25AA53ED13B004EF2F110CBB3E5B3FA8DA77C89397E1E9AC7E0072793759C2145761517FCB4CD1878B7DB94D03764487DF4484FB154253161207CFABCD45321B4528B276FBD87847B12F5B27BD8EB1FE4C1C05C814D7E4E8DAD87FCF95B56A67C2EB045D05E758E268A29F18C8DCD9BBC43778F18544B42E5F73FB6C256EA8A2E9EE3F798A1873DFDF84403623

What is the openssl or any linux command to retrieve a PEM encoded public key

Thank you


回答1:


As far as I know, none of the openssl apps or Linux commands supports what you are looking for. One option would be to leverage the python cryptography module. Install with pip install cryptography and then use something along these lines:

import sys
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends.openssl import backend

pubnums = RSAPublicNumbers(
    int(sys.argv[2], 16),
    int(sys.argv[1], 16)
)
pubkey = pubnums.public_key(backend)
pubkey_PEM = pubkey.public_bytes(
    encoding = serialization.Encoding.PEM,
    format = serialization.PublicFormat.PKCS1).decode('utf-8')
print(pubkey_PEM) # Remove the parenthesis for old versions of python

Testing the script with your input, assuming it is called makePubPEM.py:

$ MODKEY=00A5AC1136D1CE663E37E9F40F36C84F208FF0BEDE08BEE53B83A7BF79A39EA86E13F82893ACE0FB877BAC2FCAFB931D8C6CAB1D7A343072EB0D3499D2538E5B8DDA346BE333293BD6BDC9949752BCFCDE94FE7F74E92EBA4EB22016CEB27D0B538F843DF76F31D3BB9ADD4CEE48F80BF8B6284B25AA53ED13B004EF2F110CBB3E5B3FA8DA77C89397E1E9AC7E0072793759C2145761517FCB4CD1878B7DB94D03764487DF4484FB154253161207CFABCD45321B4528B276FBD87847B12F5B27BD8EB1FE4C1C05C814D7E4E8DAD87FCF95B56A67C2EB045D05E758E268A29F18C8DCD9BBC43778F18544B42E5F73FB6C256EA8A2E9EE3F798A1873DFDF84403623
$ EXPKEY=010001
$ python makePubPEM.py ${MODKEY} ${EXPKEY}
-----BEGIN RSA PUBLIC KEY-----
MIIBCAKCAQEApawRNtHOZj436fQPNshPII/wvt4IvuU7g6e/eaOeqG4T+CiTrOD7
h3usL8r7kx2MbKsdejQwcusNNJnSU45bjdo0a+MzKTvWvcmUl1K8/N6U/n906S66
TrIgFs6yfQtTj4Q9928x07ua3UzuSPgL+LYoSyWqU+0TsATvLxEMuz5bP6jad8iT
l+HprH4Acnk3WcIUV2FRf8tM0YeLfblNA3ZEh99EhPsVQlMWEgfPq81FMhtFKLJ2
+9h4R7EvWye9jrH+TBwFyBTX5Oja2H/PlbVqZ8LrBF0F51jiaKKfGMjc2bvEN3jx
hUS0Ll9z+2wlbqii6e4/eYoYc9/fhEA2IwIBEQ==
-----END RSA PUBLIC KEY-----

Verifying the result:

$ openssl rsa -RSAPublicKey_in -text -noout -in <(./makePubPEM.py ${MODKEY} ${EXPKEY})
Public-Key: (2048 bit)

Modulus:
    00:a5:ac:11:36:d1:ce:66:3e:37:e9:f4:0f:36:c8:
    4f:20:8f:f0:be:de:08:be:e5:3b:83:a7:bf:79:a3:
    9e:a8:6e:13:f8:28:93:ac:e0:fb:87:7b:ac:2f:ca:
    fb:93:1d:8c:6c:ab:1d:7a:34:30:72:eb:0d:34:99:
    d2:53:8e:5b:8d:da:34:6b:e3:33:29:3b:d6:bd:c9:
    94:97:52:bc:fc:de:94:fe:7f:74:e9:2e:ba:4e:b2:
    20:16:ce:b2:7d:0b:53:8f:84:3d:f7:6f:31:d3:bb:
    9a:dd:4c:ee:48:f8:0b:f8:b6:28:4b:25:aa:53:ed:
    13:b0:04:ef:2f:11:0c:bb:3e:5b:3f:a8:da:77:c8:
    93:97:e1:e9:ac:7e:00:72:79:37:59:c2:14:57:61:
    51:7f:cb:4c:d1:87:8b:7d:b9:4d:03:76:44:87:df:
    44:84:fb:15:42:53:16:12:07:cf:ab:cd:45:32:1b:
    45:28:b2:76:fb:d8:78:47:b1:2f:5b:27:bd:8e:b1:
    fe:4c:1c:05:c8:14:d7:e4:e8:da:d8:7f:cf:95:b5:
    6a:67:c2:eb:04:5d:05:e7:58:e2:68:a2:9f:18:c8:
    dc:d9:bb:c4:37:78:f1:85:44:b4:2e:5f:73:fb:6c:
    25:6e:a8:a2:e9:ee:3f:79:8a:18:73:df:df:84:40:
    36:23
Exponent: 65537 (0x10001)


来源:https://stackoverflow.com/questions/52448535/from-expkey-modkey-to-pem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!