Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) JWE using ECDH-ES, BP-256, A256GCMSee more JSON Web Encryption (JWE) ExamplesCreate 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.
#include <C_CkJsonObjectW.h> #include <C_CkPublicKeyW.h> #include <C_CkJwtW.h> #include <C_CkJweW.h> void ChilkatSample(void) { BOOL success; HCkJsonObjectW json; HCkPublicKeyW pubkey; HCkJwtW jwt; HCkJsonObjectW jweProtHdr; HCkJweW jwe; const wchar_t *plainText; const wchar_t *strJwe; // 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" // } json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"use",L"enc"); CkJsonObjectW_UpdateString(json,L"kid",L"puk_idp_enc"); CkJsonObjectW_UpdateString(json,L"kty",L"EC"); CkJsonObjectW_UpdateString(json,L"crv",L"BP-256"); CkJsonObjectW_UpdateString(json,L"x",L"QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"); CkJsonObjectW_UpdateString(json,L"y",L"AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"); pubkey = CkPublicKeyW_Create(); success = CkPublicKeyW_LoadFromString(pubkey,CkJsonObjectW_emit(json)); if (success == FALSE) { wprintf(L"%s\n",CkPublicKeyW_lastErrorText(pubkey)); CkJsonObjectW_Dispose(json); CkPublicKeyW_Dispose(pubkey); 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. jwt = CkJwtW_Create(); jweProtHdr = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(jweProtHdr,L"alg",L"ECDH-ES"); CkJsonObjectW_UpdateString(jweProtHdr,L"enc",L"A256GCM"); CkJsonObjectW_UpdateInt(jweProtHdr,L"exp",CkJwtW_GenNumericDate(jwt,3600)); CkJsonObjectW_UpdateString(jweProtHdr,L"cty",L"NJWT"); CkJsonObjectW_UpdateString(jweProtHdr,L"epk.kty",L"EC"); CkJsonObjectW_UpdateString(jweProtHdr,L"epk.x",L"QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"); CkJsonObjectW_UpdateString(jweProtHdr,L"epk.y",L"AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"); CkJsonObjectW_UpdateString(jweProtHdr,L"epk.crv",L"BP-256"); jwe = CkJweW_Create(); CkJweW_SetProtectedHeader(jwe,jweProtHdr); CkJweW_SetPublicKey(jwe,0,pubkey); plainText = L"This is the text to be encrypted."; strJwe = CkJweW_encrypt(jwe,plainText,L"utf-8"); if (CkJweW_getLastMethodSuccess(jwe) != TRUE) { wprintf(L"%s\n",CkJweW_lastErrorText(jwe)); CkJsonObjectW_Dispose(json); CkPublicKeyW_Dispose(pubkey); CkJwtW_Dispose(jwt); CkJsonObjectW_Dispose(jweProtHdr); CkJweW_Dispose(jwe); return; } wprintf(L"%s\n",strJwe); wprintf(L"Success.\n"); CkJsonObjectW_Dispose(json); CkPublicKeyW_Dispose(pubkey); CkJwtW_Dispose(jwt); CkJsonObjectW_Dispose(jweProtHdr); CkJweW_Dispose(jwe); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.