David-mu
David-mu

Reputation: 1772

Parse EC Public Key with pyasn1

How to parse ec public with the use of pyasn1?

    from pyasn1.codec.der import decoder
    import base64
    raw2='''
    MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE/FU6/Om2m5EnxDwGSVO+YNXorpDtzutLtCAMTZR5
    NIs6pfKx9oyjpS5aURx4BinuW8dr8K7N2oafY1TNvc41oQ==
    '''
    der = decoder.decode(base64.b64decode(raw2))
    print der
    '''
     der:
    (Sequence().setComponentByPosition(0, Sequence().setComponentByPosition(0, ObjectIdentifier(1.2.840.10045.2.1)).setComponentByPosition(1, ObjectIdentifier(1.2.840.10045.3.1.7))).setComponentByPosition(1, BitString("'0000010011111100010101010011101011111100111010011011011010011011100100010010011111000100001111000000011001001001010100111011111001100000110101011110100010101110100100001110110111001110111010110100101110110100001000000000110001001101100101000111100100110100100010110011101010100101111100101011000111110110100011001010001110100101001011100101101001010001000111000111100000000110001010011110111001011011110001110110101111110000101011101100110111011010100001101001111101100011010101001100110110111101110011100011010110100001'B")), '')
'''

Is it possible, to get something like this with pyasn1.

Edit: I base myself on this to obtain the following:

class curve(univ.Sequence):

    componentType = namedtype.NamedTypes(
     namedtype.NamedType('public KeyType',univ.ObjectIdentifier()),
     namedtype.NamedType('curveName',univ.ObjectIdentifier())
    )

class EcPublicKey(univ.Sequence):
  componentType = namedtype.NamedTypes(
    namedtype.NamedType('curve', curve()),
    namedtype.NamedType('publicKeyValue', univ.BitString())
    )

pubKey,rest = decoder.decode(base64.b64decode(raw2), asn1Spec = EcPublicKey())

print(pubKey.prettyPrint())
'''
EcPublicKey:
 curve=curve:
  public KeyType=1.2.840.10045.2.1
  curveName=1.2.840.10045.3.1.7

 publicKeyValue="'0000010011111100010101010011101011111100111010011011011010011011100100010010011111000100001111000000011001001001010100111011111001100000110101011110100010101110100100001110110111001110111010110100101110110100001000000000110001001101100101000111100100110100100010110011101010100101111100101011000111110110100011001010001110100101001011100101101001010001000111000111100000000110001010011110111001011011110001110110101111110000101011101100110111011010100001101001111101100011010101001100110110111101110011100011010110100001'B"

'''

Upvotes: 1

Views: 1996

Answers (3)

sylvain
sylvain

Reputation: 771

If you want to have access to the value of the public key itself, you can use the pycryptodome library. Just format your data in the file public.pem:

-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE/FU6/Om2m5EnxDwGSVO+YNXorpDtzutLtCAMTZR5
NIs6pfKx9oyjpS5aURx4BinuW8dr8K7N2oafY1TNvc41oQ==
-----END PUBLIC KEY-----

And then in Python:

from Crypto.PublicKey import ECC
print(ECC.import_key(open('public.pem').read()))

EccKey(curve='P-256', x=114133426963296850915436792023241197556938624347236768937730916445384676553867, y=26527350004009046719864726291814153938182054558131723928772218772465776473505)

Upvotes: 0

Pooh
Pooh

Reputation: 276

repr() gives you Python code which would produce the same object(s) once evaluated. For a human-oriented representation use .prettyPrint() method (of any pyasn1 object):

publicKey, restOfInput = decoder.decode(base64.b64decode(raw2))
print publicKey.prettyPrint()

Sequence:
<no-name>=Sequence:
<no-name>=1.2.840.10045.2.1
<no-name>=1.2.840.10045.3.1.7

<no-name>="'0000010011111100010101010011101011111100111010011011011010011011100100010010011111000100001111000000011001001001010100111011111001100000110101011110100010101110100100001110110111001110111010110100101110110100001000000000110001001101100101000111100100110100100010110011101010100101111100101011000111110110100011001010001110100101001011100101101001010001000111000111100000000110001010011110111001011011110001110110101111110000101011101100110111011010100001101001111101100011010101001100110110111101110011100011010110100001'B"

Upvotes: 2

Andrei Bozantan
Andrei Bozantan

Reputation: 3921

It seems that pyasn1 doen't provide better formatting, so you will have to implement it yourself by following the pattern from __repr__ method (see AbstractSimpleAsn1Item and AbstractConstructedAsn1Item from http://pyasn1.cvs.sourceforge.net/viewvc/pyasn1/pyasn1/pyasn1/type/base.py?revision=1.35&view=markup)

Upvotes: 0

Related Questions