Unicode C++
Unicode C++
Encrypt a JWE with a Password (PBES2)
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetPassword, which sets the PBES2 password for a recipient. The example uses PBES2-HS256+A128KW key management with AES-128-GCM content encryption.
Background. PBES2 derives a key-wrapping key from a password using a salt and iteration count, allowing password-based JWE encryption. The password should come from a secure source.
Chilkat Unicode C++ Downloads
#include <CkJweW.h>
#include <CkJsonObjectW.h>
void ChilkatSample(void)
{
bool success = false;
CkJweW jwe;
// Protected header: PBES2 password-based key management with AES-128-GCM content encryption.
CkJsonObjectW header;
header.UpdateString(L"alg",L"PBES2-HS256+A128KW");
header.UpdateString(L"enc",L"A128GCM");
success = jwe.SetProtectedHeader(header);
if (success == false) {
wprintf(L"%s\n",jwe.lastErrorText());
return;
}
// Set the PBES2 password for recipient 0. The password should come from a secure source rather than
// being hard-coded.
const wchar_t *password = L"myPassword";
success = jwe.SetPassword(0,password);
if (success == false) {
wprintf(L"%s\n",jwe.lastErrorText());
return;
}
const wchar_t *jweCompact = jwe.encrypt(L"This is the secret content.",L"utf-8");
if (jwe.get_LastMethodSuccess() == false) {
wprintf(L"%s\n",jwe.lastErrorText());
return;
}
wprintf(L"%s\n",jweCompact);
}