Delphi DLL
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
jwe: HCkJwe;
sbJwe: HCkStringBuilder;
bLoaded: Boolean;
bCaseSensitive: Boolean;
index: Integer;
begin
success := False;
jwe := CkJwe_Create();
// 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.
sbJwe := CkStringBuilder_Create();
bLoaded := CkStringBuilder_LoadFile(sbJwe,'qa_data/message.jwe','utf-8');
if (bLoaded <> True) then
begin
Memo1.Lines.Add('Failed to load the JWE file.');
Exit;
end;
success := CkJwe_LoadJweSb(jwe,sbJwe);
if (success = False) then
begin
Memo1.Lines.Add(CkJwe__lastErrorText(jwe));
Exit;
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.
bCaseSensitive := True;
index := CkJwe_FindRecipient(jwe,'kid','recipient-key-1',bCaseSensitive);
if (index < 0) then
begin
Memo1.Lines.Add('Recipient not found.');
Exit;
end;
Memo1.Lines.Add('Found recipient at index ' + IntToStr(index));
CkJwe_Dispose(jwe);
CkStringBuilder_Dispose(sbJwe);