Unicode C++
Unicode C++
JWE using ECDH-ES, BP-256, A256GCM
See more JSON Web Encryption (JWE) Examples
Create a JWE with the following header:
{
"alg": "ECDH-ES",
"enc": "A256GCM",
"exp": 1621957030,
"cty": "NJWT",
"epk": {
"kty": "EC",
"x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"
"y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
"crv": "BP-256"
}
}
Note: This example requires Chilkat v9.5.0.87 or greater.
Chilkat Unicode C++ Downloads
#include <CkJsonObjectW.h>
#include <CkPublicKeyW.h>
#include <CkJwtW.h>
#include <CkJweW.h>
void ChilkatSample(void)
{
bool success = false;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load our brainpool BP-256 public key.
// {
// "use": "enc",
// "kid": "puk_idp_enc",
// "kty": "EC",
// "crv": "BP-256",
// "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w",
// "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
// }
CkJsonObjectW json;
json.UpdateString(L"use",L"enc");
json.UpdateString(L"kid",L"puk_idp_enc");
json.UpdateString(L"kty",L"EC");
json.UpdateString(L"crv",L"BP-256");
json.UpdateString(L"x",L"QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w");
json.UpdateString(L"y",L"AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu");
CkPublicKeyW pubkey;
success = pubkey.LoadFromString(json.emit());
if (success == false) {
wprintf(L"%s\n",pubkey.lastErrorText());
return;
}
// Build our protected header:
// {
// "alg": "ECDH-ES",
// "enc": "A256GCM",
// "exp": 1621957030,
// "cty": "NJWT",
// "epk": {
// "kty": "EC",
// "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"
// "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
// "crv": "BP-256"
// }
// }
// Use jwt only for getting the current date/time + 3600 seconds.
CkJwtW jwt;
CkJsonObjectW jweProtHdr;
jweProtHdr.UpdateString(L"alg",L"ECDH-ES");
jweProtHdr.UpdateString(L"enc",L"A256GCM");
jweProtHdr.UpdateInt(L"exp",jwt.GenNumericDate(3600));
jweProtHdr.UpdateString(L"cty",L"NJWT");
jweProtHdr.UpdateString(L"epk.kty",L"EC");
jweProtHdr.UpdateString(L"epk.x",L"QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w");
jweProtHdr.UpdateString(L"epk.y",L"AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu");
jweProtHdr.UpdateString(L"epk.crv",L"BP-256");
CkJweW jwe;
jwe.SetProtectedHeader(jweProtHdr);
jwe.SetPublicKey(0,pubkey);
const wchar_t *plainText = L"This is the text to be encrypted.";
const wchar_t *strJwe = jwe.encrypt(plainText,L"utf-8");
if (jwe.get_LastMethodSuccess() != true) {
wprintf(L"%s\n",jwe.lastErrorText());
return;
}
wprintf(L"%s\n",strJwe);
wprintf(L"Success.\n");
}