使用pyasn1解析EC公钥

时间:2014-02-22 12:44:32

标签: python-2.7 asn.1 ecdsa

如何使用pyasn1解析ec public?

    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")), '')
'''

是否有可能使用pyasn1获得类似this的内容。

修改 我基于this获得以下内容:

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"

'''

3 个答案:

答案 0 :(得分:2)

repr()为您提供Python代码,一旦评估,它将生成相同的对象。对于面向人类的表示,使用.prettyPrint()方法(任何pyasn1对象):

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

序列:
 <无名称> =序列:
 <无名称> = 1.2.840.10045.2.1
 <无名称> = 1.2.840.10045.3.1.7

<无名称> = “'0000010011111100010101010011101011111100111010011011011010011011100100010010011111000100001111000000011001001001010100111011111001100000110101011110100010101110100100001110110111001110111010110100101110110100001000000000110001001101100101000111100100110100100010110011101010100101111100101011000111110110100011001010001110100101001011100101101001010001000111000111100000000110001010011110111001011011110001110110101111110000101011101100110111011010100001101001111101100011010101001100110110111101110011100011010110100001'B”

答案 1 :(得分:0)

似乎pyasn1不提供更好的格式,因此您必须自己按照__repr__方法的模式实现它(请参阅AbstractSimpleAsn1ItemAbstractConstructedAsn1Item {{3 }})

答案 2 :(得分:0)

如果您想要访问公钥本身的值,可以使用pycryptodome库。只需在public.pem文件中格式化数据:

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

然后在Python中:

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

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