Sample code for 30+ languages & platforms
C

Load EC Public Key from X,Y Values

See more ECC Examples

Demonstrates how to load an EC public key from X and Y values.

Chilkat C Downloads

C
#include <C_CkJsonObject.h>
#include <C_CkPublicKey.h>

void ChilkatSample(void)
    {
    BOOL success;
    const char *x;
    const char *y;
    HCkJsonObject json;
    HCkPublicKey pubkey;

    success = FALSE;

    // We have the following x and y values in base64 (for an EC point on the P-256 curve).
    x = "Dn7uB1O7kgk74G6qfQwFJESeDnxO6lLjGZFWZJE16tw";
    y = "iOWA5DInzK6nuUGvHJbMVq1Dpj248FqSV2teN3HzmhU";

    // Build a JWK that looks like this:

    // {
    //   "kty": "EC",
    //   "crv": "P-256",
    //   "x": "Dn7uB1O7kgk74G6qfQwFJESeDnxO6lLjGZFWZJE16tw",
    //   "y": "iOWA5DInzK6nuUGvHJbMVq1Dpj248FqSV2teN3HzmhU"
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"kty","EC");
    CkJsonObject_UpdateString(json,"crv","P-256");
    CkJsonObject_UpdateString(json,"x",x);
    CkJsonObject_UpdateString(json,"y",y);

    // Load from the JWK.
    pubkey = CkPublicKey_Create();
    success = CkPublicKey_LoadFromString(pubkey,CkJsonObject_emit(json));
    if (success == FALSE) {
        printf("%s\n",CkPublicKey_lastErrorText(pubkey));
        CkJsonObject_Dispose(json);
        CkPublicKey_Dispose(pubkey);
        return;
    }

    printf("Success.\n");


    CkJsonObject_Dispose(json);
    CkPublicKey_Dispose(pubkey);

    }