Search code examples
ecdsa

What is the format of this private key?


I got a pem key like this:

 -----BEGIN PRIVATE KEY----- 

 MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgDZUgDvKixfLi8cK8
 /TFLY97TDmQV3J2ygPpvuI8jSdihRANCAARRN3xgbPIR83dr27UuDaf2OJezpEJx
 UC3v06+FD8MUNcRAboqt4akehaNNSh7MMZI+HdnsM4RXN2y8NePUQsPL

 -----END PRIVATE KEY-----

I only know that this key is used for ecdsa signature.

Now I want to know the real value of the key, because I need it to calculate something, and I believe there is some kind of header in front of the real value.

I tried to use python-ecdsa to parse the key, but obviously didn't work.

How can I get the real value?


Solution

  • It looks like you have an ASN1 structure representing an ECDSA private key for the NIST P-256 curve, and it was encoded as a PEM file.

    I was able to get your private and public key and the curve information using the OpenSSL command-line utility:

    $ openssl ec -in test.pem -text -noout
    read EC key
    Private-Key: (256 bit)
    priv:
        0d:95:20:0e:f2:a2:c5:f2:e2:f1:c2:bc:fd:31:4b:
        63:de:d3:0e:64:15:dc:9d:b2:80:fa:6f:b8:8f:23:
        49:d8
    pub: 
        04:51:37:7c:60:6c:f2:11:f3:77:6b:db:b5:2e:0d:
        a7:f6:38:97:b3:a4:42:71:50:2d:ef:d3:af:85:0f:
        c3:14:35:c4:40:6e:8a:ad:e1:a9:1e:85:a3:4d:4a:
        1e:cc:31:92:3e:1d:d9:ec:33:84:57:37:6c:bc:35:
        e3:d4:42:c3:cb
    ASN1 OID: prime256v1
    NIST CURVE: P-256
    

    You can also look at the ASN1 structure itself:

    $ openssl asn1parse -in test.pem 
        0:d=0  hl=3 l= 135 cons: SEQUENCE          
        3:d=1  hl=2 l=   1 prim: INTEGER           :00
        6:d=1  hl=2 l=  19 cons: SEQUENCE          
        8:d=2  hl=2 l=   7 prim: OBJECT            :id-ecPublicKey
       17:d=2  hl=2 l=   8 prim: OBJECT            :prime256v1
       27:d=1  hl=2 l= 109 prim: OCTET STRING      [HEX DUMP]:306B02010104200D95200EF2A2C5F2E2F1C2BCFD314B63DED30E6415DC9DB280FA6FB88F2349D8A1440342000451377C606CF211F3776BDBB52E0DA7F63897B3A44271502DEFD3AF850FC31435C4406E8AADE1A91E85A34D4A1ECC31923E1DD9EC338457376CBC35E3D442C3CB