Sample code for 30+ languages & platforms
Visual FoxPro

Find a JWE Recipient by Header Value

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.FindRecipient, which searches the per-recipient unprotected headers for a member with a given name and string value, and returns the matching recipient index.

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. This locates the recipient that a particular key belongs to (for example by kid) before decrypting. The method returns -1 when no match is found.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loJwe
LOCAL loSbJwe
LOCAL lnBLoaded
LOCAL lnBCaseSensitive
LOCAL lnIndex

lnSuccess = 0

loJwe = CreateObject('Chilkat.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.
*  Load a multi-recipient JWE (JSON serialization) from a file.
loSbJwe = CreateObject('Chilkat.StringBuilder')
lnBLoaded = loSbJwe.LoadFile("qa_data/message.jwe","utf-8")
IF (lnBLoaded <> 1) THEN
    ? "Failed to load the JWE file."
    RELEASE loJwe
    RELEASE loSbJwe
    CANCEL
ENDIF

lnSuccess = loJwe.LoadJweSb(loSbJwe)
IF (lnSuccess = 0) THEN
    ? loJwe.LastErrorText
    RELEASE loJwe
    RELEASE loSbJwe
    CANCEL
ENDIF

*  Search the per-recipient unprotected headers for a member named "kid" whose value equals the
*  given string.  Returns the recipient index, or -1 if not found.
lnBCaseSensitive = 1
lnIndex = loJwe.FindRecipient("kid","recipient-key-1",lnBCaseSensitive)
IF (lnIndex < 0) THEN
    ? "Recipient not found."
    RELEASE loJwe
    RELEASE loSbJwe
    CANCEL
ENDIF

? "Found recipient at index " + STR(lnIndex)

RELEASE loJwe
RELEASE loSbJwe