Unicode C++
Unicode C++
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 Unicode C++ Downloads
#include <CkJweW.h>
#include <CkPrivateKeyW.h>
void ChilkatSample(void)
{
bool success = false;
CkJweW jwe;
// Load the JWE compact serialization to be decrypted.
const wchar_t *jweCompact = L"eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>";
success = jwe.LoadJwe(jweCompact);
if (success == false) {
wprintf(L"%s\n",jwe.lastErrorText());
return;
}
// 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.
CkPrivateKeyW privKey;
success = privKey.LoadPemFile(L"qa_data/recipient_privkey.pem");
if (success == false) {
wprintf(L"%s\n",privKey.lastErrorText());
return;
}
// Set the private key used to decrypt recipient 0.
success = jwe.SetPrivateKey(0,privKey);
if (success == false) {
wprintf(L"%s\n",jwe.lastErrorText());
return;
}
const wchar_t *content = jwe.decrypt(0,L"utf-8");
if (jwe.get_LastMethodSuccess() == false) {
wprintf(L"%s\n",jwe.lastErrorText());
return;
}
wprintf(L"%s\n",content);
}