Sample code for 30+ languages & platforms
Delphi DLL

Decrypt a JWE with a Private Key

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetPrivateKey, which sets the private key used to decrypt a recipient of a loaded 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.

Background. The private key unwraps the content-encryption key that was wrapped with the corresponding public key during encryption.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
jwe: HCkJwe;
jweCompact: PWideChar;
privKey: HCkPrivateKey;
content: PWideChar;

begin
success := False;

jwe := CkJwe_Create();

//  Load the JWE compact serialization to be decrypted.
jweCompact := 'eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>';
success := CkJwe_LoadJwe(jwe,jweCompact);
if (success = False) then
  begin
    Memo1.Lines.Add(CkJwe__lastErrorText(jwe));
    Exit;
  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 the recipient's private key.
privKey := CkPrivateKey_Create();
success := CkPrivateKey_LoadPemFile(privKey,'qa_data/recipient_privkey.pem');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPrivateKey__lastErrorText(privKey));
    Exit;
  end;

//  Set the private key used to decrypt recipient 0.
success := CkJwe_SetPrivateKey(jwe,0,privKey);
if (success = False) then
  begin
    Memo1.Lines.Add(CkJwe__lastErrorText(jwe));
    Exit;
  end;

content := CkJwe__decrypt(jwe,0,'utf-8');
if (CkJwe_getLastMethodSuccess(jwe) = False) then
  begin
    Memo1.Lines.Add(CkJwe__lastErrorText(jwe));
    Exit;
  end;
Memo1.Lines.Add(content);

CkJwe_Dispose(jwe);
CkPrivateKey_Dispose(privKey);