Sample code for 30+ languages & platforms
Xbase++

Decrypt a JWE with a Private Key

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetPrivateKey, which sets the private key used to decrypt a recipient of a loaded JWE.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The private key unwraps the content-encryption key that was wrapped with the corresponding public key during encryption.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJwe
LOCAL cJweCompact
LOCAL oPrivKey
LOCAL cContent

nSuccess := 0

oJwe := CreateObject("Chilkat.Jwe")

//  Load the JWE compact serialization to be decrypted.
cJweCompact := "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>"
nSuccess := oJwe:LoadJwe(cJweCompact)
IF (nSuccess == 0)
    ? oJwe:LastErrorText
    oJwe:destroy()
    RETURN
ENDIF

//  The file path is relative to the application's current working directory.  An absolute path
//  may also be used.  Supply the path appropriate to your own environment.
//  Load the recipient's private key.
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oPrivKey:LoadPemFile("qa_data/recipient_privkey.pem")
IF (nSuccess == 0)
    ? oPrivKey:LastErrorText
    oJwe:destroy()
    oPrivKey:destroy()
    RETURN
ENDIF

//  Set the private key used to decrypt recipient 0.
nSuccess := oJwe:SetPrivateKey(0, oPrivKey)
IF (nSuccess == 0)
    ? oJwe:LastErrorText
    oJwe:destroy()
    oPrivKey:destroy()
    RETURN
ENDIF

cContent := oJwe:Decrypt(0, "utf-8")
IF (oJwe:LastMethodSuccess == 0)
    ? oJwe:LastErrorText
    oJwe:destroy()
    oPrivKey:destroy()
    RETURN
ENDIF

? cContent

oJwe:destroy()
oPrivKey:destroy()