Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loJwe
LOCAL lcJweCompact
LOCAL loPrivKey
LOCAL lcContent

lnSuccess = 0

loJwe = CreateObject('Chilkat.Jwe')

*  Load the JWE compact serialization to be decrypted.
lcJweCompact = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>"
lnSuccess = loJwe.LoadJwe(lcJweCompact)
IF (lnSuccess = 0) THEN
    ? loJwe.LastErrorText
    RELEASE loJwe
    CANCEL
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.
loPrivKey = CreateObject('Chilkat.PrivateKey')
lnSuccess = loPrivKey.LoadPemFile("qa_data/recipient_privkey.pem")
IF (lnSuccess = 0) THEN
    ? loPrivKey.LastErrorText
    RELEASE loJwe
    RELEASE loPrivKey
    CANCEL
ENDIF

*  Set the private key used to decrypt recipient 0.
lnSuccess = loJwe.SetPrivateKey(0,loPrivKey)
IF (lnSuccess = 0) THEN
    ? loJwe.LastErrorText
    RELEASE loJwe
    RELEASE loPrivKey
    CANCEL
ENDIF

lcContent = loJwe.Decrypt(0,"utf-8")
IF (loJwe.LastMethodSuccess = 0) THEN
    ? loJwe.LastErrorText
    RELEASE loJwe
    RELEASE loPrivKey
    CANCEL
ENDIF

? lcContent

RELEASE loJwe
RELEASE loPrivKey