Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jwe
oleobject loo_SbJwe
integer li_BLoaded
integer li_BCaseSensitive
integer li_Index

li_Success = 0

loo_Jwe = create oleobject
li_rc = loo_Jwe.ConnectToNewObject("Chilkat.Jwe")
if li_rc < 0 then
    destroy loo_Jwe
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  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.
loo_SbJwe = create oleobject
li_rc = loo_SbJwe.ConnectToNewObject("Chilkat.StringBuilder")

li_BLoaded = loo_SbJwe.LoadFile("qa_data/message.jwe","utf-8")
if li_BLoaded <> 1 then
    Write-Debug "Failed to load the JWE file."
    destroy loo_Jwe
    destroy loo_SbJwe
    return
end if

li_Success = loo_Jwe.LoadJweSb(loo_SbJwe)
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_SbJwe
    return
end if

//  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.
li_BCaseSensitive = 1
li_Index = loo_Jwe.FindRecipient("kid","recipient-key-1",li_BCaseSensitive)
if li_Index < 0 then
    Write-Debug "Recipient not found."
    destroy loo_Jwe
    destroy loo_SbJwe
    return
end if

Write-Debug "Found recipient at index " + string(li_Index)


destroy loo_Jwe
destroy loo_SbJwe