简体   繁体   中英

Import RSA key into Python

I am trying to import an RSA (Public) key into Python using RSA.importkey but I am getting RSA Key is not supported .

Here is the code I have written:

f = open('pub.key','rb')
data = f.read()
imported = RSA.importkey(data)
f.close()

Here is a screen shot of the key I am trying to import: RSA_Public_Key

Can someone please help me out?

在此处输入图片说明

[UPDATE]: I've done some more research and the key is in MS PUBLICKEYBLOB format which I guess is not compatible with PyCrypto? IS there anything else I can use to import this

I don't think Python's Crypto.PublicKey module supports PUBLICKEYBLOB keys, but it's not that hard to extract something usable.

Since you provided your data in the form of an image ( please don't ), I'll show you how the process works with this key instead:

0000000: 0602 0000 00a4 0000 5253 4131 0008 0000  ........RSA1....
0000010: 0100 0100 7597 4c3b 8446 de2c 2af4 95a8  ....u.L;.F.,*...
0000020: 5dc0 cd6d dad7 d492 1e13 8234 6a70 8d8f  ]..m.......4jp..
0000030: 7cf7 0492 557f f1a2 27b2 9e41 ac90 8091  |...U...'..A....
0000040: 1893 c2b1 7bad 2bf3 ffaf db2b 51be 1da3  ....{.+....+Q...
0000050: 27e3 a757 085a bec1 1df6 04f8 1cbe 5bb1  '..W.Z........[.
0000060: 67fb e4c8 da75 0070 b117 7024 6c09 6374  g....u.p..p$l.ct
0000070: ac4b 0a1d 71ae 7fae 65b8 c586 79c5 7e9f  .K..q...e...y.~.
0000080: 9860 4c52 b929 62cb 2329 ed31 9174 7b7b  .`LR.)b.#).1.t{{
0000090: 0b26 1bf2 7d67 bfda 7a40 daf2 614d 94a5  .&..}g..z@..aM..
00000a0: 7dad 596b ad9e a33a 39c6 5b6e 9fd2 bb36  }.Yk...:9.[n...6
00000b0: b5f5 d265 f52c 30d8 c117 bdaf 2800 9620  ...e.,0.....(.. 
00000c0: 46a7 2d62 030c d7d0 75a0 0b07 ead4 1fca  F.-b....u.......
00000d0: e8d9 4edb 38f2 2675 cb12 a688 709b e1ea  ..N.8.&u....p...
00000e0: 32dc f871 7250 41e6 1781 6827 428e dfe5  2..qrPA...h'B...
00000f0: dea1 72d9 3bfb e59d 3011 6992 cd60 2be2  ..r.;...0.i..`+.
0000100: d546 3c28 cf9d 304a f7ad b9fb 0f91 fe2e  .F<(..0J........
0000110: be18 f1ce                                ....

The only two pieces of information you actually need are the public exponent e and the modulus n . You can find e as a 32-bit little-endian number eight bytes after the RSA1 token. In this case, 01000100 is 0x10001 , or 65537. The remainder of the key is the modulus, which is also in little-endian order.

Here's a bit of Python code that will convert this information into a regular key:

from Crypto.PublicKey import RSA
e = 65537L
n = int('75974c3b8446de2c2af495a85dc0cd6ddad7d4921e1382346a708d8f7cf70492557ff1a2'\
        '27b29e41ac9080911893c2b17bad2bf3ffafdb2b51be1da327e3a757085abec11df604f8'\
        '1cbe5bb167fbe4c8da750070b11770246c096374ac4b0a1d71ae7fae65b8c58679c57e9f'\
        '98604c52b92962cb2329ed3191747b7b0b261bf27d67bfda7a40daf2614d94a57dad596b'\
        'ad9ea33a39c65b6e9fd2bb36b5f5d265f52c30d8c117bdaf2800962046a72d62030cd7d0'\
        '75a00b07ead41fcae8d94edb38f22675cb12a688709be1ea32dcf871725041e617816827'\
        '428edfe5dea172d93bfbe59d30116992cd602be2d5463c28cf9d304af7adb9fb0f91fe2e'\
        'be18f1ce'.decode('hex')[::-1].encode('hex'), 16)
key_params = (n, e)
pub_key = RSA.construct(key_params)
print pub_key.exportKey()

And here's the output:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzvEYvi7+kQ/7ua33SjCd
zyg8RtXiK2DNkmkRMJ3l+zvZcqHe5d+OQidogRfmQVBycfjcMurhm3CIphLLdSby
ONtO2ejKH9TqBwugddDXDANiLadGIJYAKK+9F8HYMCz1ZdL1tTa70p9uW8Y5OqOe
rWtZrX2llE1h8tpAetq/Z33yGyYLe3t0kTHtKSPLYim5UkxgmJ9+xXmGxbhlrn+u
cR0KS6x0YwlsJHAXsXAAddrI5PtnsVu+HPgE9h3BvloIV6fjJ6MdvlEr26//8yut
e7HCkxiRgJCsQZ6yJ6Lxf1WSBPd8j41wajSCEx6S1Nfabc3AXaiV9Cos3kaEO0yX
dQIDAQAB
-----END PUBLIC KEY-----

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM