Sample code for 30+ languages & platforms
PureBasic

Load a Private Key from a JWK

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadJwk method, which loads a private key from JWK (JSON Web Key) JSON. The only argument is the JWK JSON string. Supported key types are RSA, EC, and Ed25519 (kty=OKP).

Background: JWK is the JSON key format used throughout modern web security — JWT signing, OAuth, OpenID Connect — so this loader is what you use to consume a key from a JWK Set or an identity provider. A private JWK includes the private parameters (d and, for RSA, the CRT values); a public-only JWK omits them. As JSON in the clear, a private JWK must be handled as sensitive material.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkPrivateKey.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the PrivateKey.LoadJwk method, which loads a private key from JWK (JSON Web Key)
    ;  JSON.  The only argument is the JWK JSON string.  Supported key types are RSA, EC, and
    ;  Ed25519 (kty=OKP).

    privKey.i = CkPrivateKey::ckCreate()
    If privKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  A private JWK.  For a real key, "d" (and the other private parameters) would be present.
    jwk.s = "{" + Chr(34) + "kty" + Chr(34) + ":" + Chr(34) + "EC" + Chr(34) + "," + Chr(34) + "crv" + Chr(34) + ":" + Chr(34) + "P-256" + Chr(34) + "," + Chr(34) + "x" + Chr(34) + ":" + Chr(34) + "..." + Chr(34) + "," + Chr(34) + "y" + Chr(34) + ":" + Chr(34) + "..." + Chr(34) + "," + Chr(34) + "d" + Chr(34) + ":" + Chr(34) + "..." + Chr(34) + "}"

    success = CkPrivateKey::ckLoadJwk(privKey,jwk)
    If success = 0
        Debug CkPrivateKey::ckLastErrorText(privKey)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    Debug "Loaded a " + CkPrivateKey::ckKeyType(privKey) + " private key."


    CkPrivateKey::ckDispose(privKey)


    ProcedureReturn
EndProcedure