Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJwe
    Variant vSbJwe
    Handle hoSbJwe
    Boolean iBLoaded
    Boolean iBCaseSensitive
    Integer iIndex
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatJwe)) To hoJwe
    If (Not(IsComObjectCreated(hoJwe))) Begin
        Send CreateComObject of hoJwe
    End

    //  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.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJwe
    If (Not(IsComObjectCreated(hoSbJwe))) Begin
        Send CreateComObject of hoSbJwe
    End
    Get ComLoadFile Of hoSbJwe "qa_data/message.jwe" "utf-8" To iBLoaded
    If (iBLoaded <> True) Begin
        Showln "Failed to load the JWE file."
        Procedure_Return
    End

    Get pvComObject of hoSbJwe to vSbJwe
    Get ComLoadJweSb Of hoJwe vSbJwe To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJwe To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Move True To iBCaseSensitive
    Get ComFindRecipient Of hoJwe "kid" "recipient-key-1" iBCaseSensitive To iIndex
    If (iIndex < 0) Begin
        Showln "Recipient not found."
        Procedure_Return
    End

    Showln "Found recipient at index " iIndex


End_Procedure