Sample code for 30+ languages & platforms
DataFlex

Understanding a few ECDSA Public Key Formats

See more ECC Examples

Describes a few ECDSA public key formats.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSbPem
    Handle hoPubKey1
    Handle hoSbHex
    Integer iNumReplaced
    Handle hoBdKey
    Handle hoPubKey2
    String sTemp1

    Move False To iSuccess

    // Here we have the output of the following openssl command:  openssl ec -in key.pem -pubout -text

    // Private-Key: (256 bit)
    // priv:
    //     0e:63:25:8a:73:3c:71:b6:c0:e7:a3:0f:94:b9:74:
    //     e0:be:bd:46:18:be:40:7e:66:9e:21:99:85:0e:ed:
    //     87:2d
    // pub:
    //     04:5d:1a:4f:d9:bd:49:9e:e4:fd:55:2c:0d:ea:6d:
    //     b1:66:64:7a:71:91:13:63:86:fe:ca:94:d4:47:51:
    //     39:66:ff:43:d5:62:de:f2:f2:41:3c:2e:3f:95:18:
    //     2d:23:f7:e7:8e:75:19:3b:c6:50:fb:d9:90:f5:e8:
    //     12:b7:b8:6a:43
    // ASN1 OID: prime256v1
    // NIST CURVE: P-256
    // writing EC key
    // -----BEGIN PUBLIC KEY-----
    // MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXRpP2b1JnuT9VSwN6m2xZmR6cZET
    // Y4b+ypTUR1E5Zv9D1WLe8vJBPC4/lRgtI/fnjnUZO8ZQ+9mQ9egSt7hqQw==
    // -----END PUBLIC KEY-----

    // The public key is shown in two different formats.
    // The first is this:

    // pub:
    //     04:5d:1a:4f:d9:bd:49:9e:e4:fd:55:2c:0d:ea:6d:
    //     b1:66:64:7a:71:91:13:63:86:fe:ca:94:d4:47:51:
    //     39:66:ff:43:d5:62:de:f2:f2:41:3c:2e:3f:95:18:
    //     2d:23:f7:e7:8e:75:19:3b:c6:50:fb:d9:90:f5:e8:
    //     12:b7:b8:6a:43

    // It is the ANSI X9.63 format.
    // 65-bytes are the uncompressed public key (04 || X || Y) 

    // This is the same public key, but in PEM format

    // -----BEGIN PUBLIC KEY-----
    // MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXRpP2b1JnuT9VSwN6m2xZmR6cZET
    // Y4b+ypTUR1E5Zv9D1WLe8vJBPC4/lRgtI/fnjnUZO8ZQ+9mQ9egSt7hqQw==
    // -----END PUBLIC KEY-----

    // It contains ASN.1 that more explicitly identifies the key type.
    // 
    // SEQUENCE (2 elem)
    //   SEQUENCE (2 elem)
    //     OBJECT IDENTIFIER 1.2.840.10045.2.1 ecPublicKey (ANSI X9.62 public key type)
    //     OBJECT IDENTIFIER 1.2.840.10045.3.1.7 prime256v1 (ANSI X9.62 named elliptic curve)
    //   BIT STRING (520 bit) 000001000101110100011010010011111101100110111101010010011001111011100...

    // PEM format can be loaded into a Chilkat public key object like this:

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPem
    If (Not(IsComObjectCreated(hoSbPem))) Begin
        Send CreateComObject of hoSbPem
    End
    Get ComAppend Of hoSbPem "-----BEGIN PUBLIC KEY-----" + (character(13)) + (character(10)) To iSuccess
    Get ComAppend Of hoSbPem "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXRpP2b1JnuT9VSwN6m2xZmR6cZET" + (character(13)) + (character(10)) To iSuccess
    Get ComAppend Of hoSbPem "Y4b+ypTUR1E5Zv9D1WLe8vJBPC4/lRgtI/fnjnUZO8ZQ+9mQ9egSt7hqQw==" + (character(13)) + (character(10)) To iSuccess
    Get ComAppend Of hoSbPem "-----END PUBLIC KEY-----" + (character(13)) + (character(10)) To iSuccess

    Get Create (RefClass(cComChilkatPublicKey)) To hoPubKey1
    If (Not(IsComObjectCreated(hoPubKey1))) Begin
        Send CreateComObject of hoPubKey1
    End
    Get ComGetAsString Of hoSbPem To sTemp1
    Get ComLoadFromString Of hoPubKey1 sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPubKey1 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The X9.63 format can be loaded like this:
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbHex
    If (Not(IsComObjectCreated(hoSbHex))) Begin
        Send CreateComObject of hoSbHex
    End
    Get ComAppend Of hoSbHex "04:5d:1a:4f:d9:bd:49:9e:e4:fd:55:2c:0d:ea:6d:" To iSuccess
    Get ComAppend Of hoSbHex "b1:66:64:7a:71:91:13:63:86:fe:ca:94:d4:47:51:" To iSuccess
    Get ComAppend Of hoSbHex "39:66:ff:43:d5:62:de:f2:f2:41:3c:2e:3f:95:18:" To iSuccess
    Get ComAppend Of hoSbHex "2d:23:f7:e7:8e:75:19:3b:c6:50:fb:d9:90:f5:e8:" To iSuccess
    Get ComAppend Of hoSbHex "12:b7:b8:6a:43" To iSuccess

    // Get rid of the ":" chars.
    Get ComReplace Of hoSbHex ":" "" To iNumReplaced

    // We'll need to convert hex to base64..
    Get Create (RefClass(cComChilkatBinData)) To hoBdKey
    If (Not(IsComObjectCreated(hoBdKey))) Begin
        Send CreateComObject of hoBdKey
    End
    Get ComGetAsString Of hoSbHex To sTemp1
    Get ComAppendEncoded Of hoBdKey sTemp1 "hex" To iSuccess

    Get Create (RefClass(cComChilkatPublicKey)) To hoPubKey2
    If (Not(IsComObjectCreated(hoPubKey2))) Begin
        Send CreateComObject of hoPubKey2
    End
    Get ComGetEncoded Of hoBdKey "base64" To sTemp1
    Get ComLoadFromString Of hoPubKey2 sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPubKey2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Let's get the key in pubKey2 as PEM.
    // It should be idential to the PEM above.
    Get ComGetPem Of hoPubKey2 True To sTemp1
    Showln sTemp1

    // Here's the output.  You can see it's the same as the PEM above..

    // -----BEGIN PUBLIC KEY-----
    // MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXRpP2b1JnuT9VSwN6m2xZmR6cZET
    // Y4b+ypTUR1E5Zv9D1WLe8vJBPC4/lRgtI/fnjnUZO8ZQ+9mQ9egSt7hqQw==
    // -----END PUBLIC KEY-----


End_Procedure