Sample code for 30+ languages & platforms
VBScript

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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

set jwe = 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.
set sbJwe = CreateObject("Chilkat.StringBuilder")
bLoaded = sbJwe.LoadFile("qa_data/message.jwe","utf-8")
If (bLoaded <> 1) Then
    outFile.WriteLine("Failed to load the JWE file.")
    WScript.Quit
End If

success = jwe.LoadJweSb(sbJwe)
If (success = 0) Then
    outFile.WriteLine(jwe.LastErrorText)
    WScript.Quit
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.
bCaseSensitive = 1
index = jwe.FindRecipient("kid","recipient-key-1",bCaseSensitive)
If (index < 0) Then
    outFile.WriteLine("Recipient not found.")
    WScript.Quit
End If

outFile.WriteLine("Found recipient at index " & index)

outFile.Close