Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Unicode C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Unicode C) JWE Using General JWE JSON Serialization

This example duplicates the example A.4 in RFC 7516 for JSON Web Encryption (JWE).

This example demonstrates the capability for encrypting the same plaintext to multiple recipients. Two recipients are present in this example.

Note: This example requires Chilkat v9.5.0.66 or greater.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <C_CkJweW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkPrivateKeyW.h>
#include <C_CkPublicKeyW.h>

void ChilkatSample(void)
    {
    BOOL success;
    const wchar_t *plaintext;
    HCkJweW jwe;
    HCkJsonObjectW jweProtHdr;
    HCkJsonObjectW jweRecipientHdr1;
    int recipientIndex;
    HCkJsonObjectW jweRecipientHdr2;
    HCkJsonObjectW jweUnprotHdr;
    HCkStringBuilderW sbJwk;
    HCkPrivateKeyW rsaPrivKey;
    HCkPublicKeyW rsaPubKey;
    const wchar_t *aesWrappingKey;
    const wchar_t *strJwe;
    HCkJsonObjectW jsonTemp;
    HCkJweW jwe2;
    BOOL caseSensitive;
    const wchar_t *originalPlaintext;
    HCkStringBuilderW sbJwe;

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Note: This example requires Chilkat v9.5.0.66 or greater.

    plaintext = L"Live long and prosper.";

    jwe = CkJweW_Create();

    // First build the JWE Protected Header: {"enc":"A128CBC-HS256"}
    jweProtHdr = CkJsonObjectW_Create();
    CkJsonObjectW_AppendString(jweProtHdr,L"enc",L"A128CBC-HS256");
    CkJweW_SetProtectedHeader(jwe,jweProtHdr);

    // The first recipient uses the RSAES-PKCS1-v1_5 algorithm to encrypt
    // the CEK.  The second uses AES Key Wrap to encrypt the CEK.  Key ID
    // values are supplied for both keys.  The two JWE Per-Recipient
    // Unprotected Header values used to represent these algorithms and key
    // IDs are:
    // 
    //      {"alg":"RSA1_5","kid":"2011-04-29"}
    // 
    // and
    // 
    //      {"alg":"A128KW","kid":"7"}

    jweRecipientHdr1 = CkJsonObjectW_Create();
    CkJsonObjectW_AppendString(jweRecipientHdr1,L"alg",L"RSA1_5");
    CkJsonObjectW_AppendString(jweRecipientHdr1,L"kid",L"2011-04-29");
    recipientIndex = 0;
    CkJweW_SetRecipientHeader(jwe,recipientIndex,jweRecipientHdr1);

    jweRecipientHdr2 = CkJsonObjectW_Create();
    CkJsonObjectW_AppendString(jweRecipientHdr2,L"alg",L"A128KW");
    CkJsonObjectW_AppendString(jweRecipientHdr2,L"kid",L"7");
    recipientIndex = 1;
    CkJweW_SetRecipientHeader(jwe,recipientIndex,jweRecipientHdr2);

    // Set the Shared Unprotected Header:  {"jku":"https://server.example.com/keys.jwks"}
    jweUnprotHdr = CkJsonObjectW_Create();
    CkJsonObjectW_AppendString(jweUnprotHdr,L"jku",L"https://server.example.com/keys.jwks");
    CkJweW_SetUnprotectedHeader(jwe,jweUnprotHdr);

    // Note: The intent of specifying a "kid" (an acronym for "Key ID") is that
    // the software would somehow download the keys.jwks from https://server.example.com/keys.jwks,
    // and would select the key (whatever format it may be, such as RSA, or an AES key wrap key, etc.),
    // and then automatically use it. 

    // This example keeps the "kid" and "jku" in their respective headers, but does not actually fetch
    // the keys from some URL.  We'll just provide the keys directly..

    // Recipient 0 uses this RSA key:
    sbJwk = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbJwk,L"{\"kty\":\"RSA\",");
    CkStringBuilderW_Append(sbJwk,L"\"n\":\"sXchDaQebHnPiGvyDOAT4saGEUetSyo9MKLOoWFsueri23bOdgWp4Dy1Wl");
    CkStringBuilderW_Append(sbJwk,L"UzewbgBHod5pcM9H95GQRV3JDXboIRROSBigeC5yjU1hGzHHyXss8UDpre");
    CkStringBuilderW_Append(sbJwk,L"cbAYxknTcQkhslANGRUZmdTOQ5qTRsLAt6BTYuyvVRdhS8exSZEy_c4gs_");
    CkStringBuilderW_Append(sbJwk,L"7svlJJQ4H9_NxsiIoLwAEk7-Q3UXERGYw_75IDrGA84-lA_-Ct4eTlXHBI");
    CkStringBuilderW_Append(sbJwk,L"Y2EaV7t7LjJaynVJCpkv4LKjTTAumiGUIuQhrNhZLuF_RJLqHpM2kgWFLU");
    CkStringBuilderW_Append(sbJwk,L"7-VTdL1VbC2tejvcI2BlMkEpk1BzBZI0KQB0GaDWFLN-aEAw3vRw\",");
    CkStringBuilderW_Append(sbJwk,L"\"e\":\"AQAB\",");
    CkStringBuilderW_Append(sbJwk,L"\"d\":\"VFCWOqXr8nvZNyaaJLXdnNPXZKRaWCjkU5Q2egQQpTBMwhprMzWzpR8Sxq");
    CkStringBuilderW_Append(sbJwk,L"1OPThh_J6MUD8Z35wky9b8eEO0pwNS8xlh1lOFRRBoNqDIKVOku0aZb-ry");
    CkStringBuilderW_Append(sbJwk,L"nq8cxjDTLZQ6Fz7jSjR1Klop-YKaUHc9GsEofQqYruPhzSA-QgajZGPbE_");
    CkStringBuilderW_Append(sbJwk,L"0ZaVDJHfyd7UUBUKunFMScbflYAAOYJqVIVwaYR5zWEEceUjNnTNo_CVSj");
    CkStringBuilderW_Append(sbJwk,L"-VvXLO5VZfCUAVLgW4dpf1SrtZjSt34YLsRarSb127reG_DUwg9Ch-Kyvj");
    CkStringBuilderW_Append(sbJwk,L"T1SkHgUWRVGcyly7uvVGRSDwsXypdrNinPA4jlhoNdizK2zF2CWQ\",");
    CkStringBuilderW_Append(sbJwk,L"\"p\":\"9gY2w6I6S6L0juEKsbeDAwpd9WMfgqFoeA9vEyEUuk4kLwBKcoe1x4HG68");
    CkStringBuilderW_Append(sbJwk,L"ik918hdDSE9vDQSccA3xXHOAFOPJ8R9EeIAbTi1VwBYnbTp87X-xcPWlEP");
    CkStringBuilderW_Append(sbJwk,L"krdoUKW60tgs1aNd_Nnc9LEVVPMS390zbFxt8TN_biaBgelNgbC95sM\",");
    CkStringBuilderW_Append(sbJwk,L"\"q\":\"uKlCKvKv_ZJMVcdIs5vVSU_6cPtYI1ljWytExV_skstvRSNi9r66jdd9-y");
    CkStringBuilderW_Append(sbJwk,L"BhVfuG4shsp2j7rGnIio901RBeHo6TPKWVVykPu1iYhQXw1jIABfw-MVsN");
    CkStringBuilderW_Append(sbJwk,L"-3bQ76WLdt2SDxsHs7q7zPyUyHXmps7ycZ5c72wGkUwNOjYelmkiNS0\",");
    CkStringBuilderW_Append(sbJwk,L"\"dp\":\"w0kZbV63cVRvVX6yk3C8cMxo2qCM4Y8nsq1lmMSYhG4EcL6FWbX5h9yuv");
    CkStringBuilderW_Append(sbJwk,L"ngs4iLEFk6eALoUS4vIWEwcL4txw9LsWH_zKI-hwoReoP77cOdSL4AVcra");
    CkStringBuilderW_Append(sbJwk,L"Hawlkpyd2TWjE5evgbhWtOxnZee3cXJBkAi64Ik6jZxbvk-RR3pEhnCs\",");
    CkStringBuilderW_Append(sbJwk,L"\"dq\":\"o_8V14SezckO6CNLKs_btPdFiO9_kC1DsuUTd2LAfIIVeMZ7jn1Gus_Ff");
    CkStringBuilderW_Append(sbJwk,L"7B7IVx3p5KuBGOVF8L-qifLb6nQnLysgHDh132NDioZkhH7mI7hPG-PYE_");
    CkStringBuilderW_Append(sbJwk,L"odApKdnqECHWw0J-F0JWnUd6D2B_1TvF9mXA2Qx-iGYn8OVV1Bsmp6qU\",");
    CkStringBuilderW_Append(sbJwk,L"\"qi\":\"eNho5yRBEBxhGBtQRww9QirZsB66TrfFReG_CcteI1aCneT0ELGhYlRlC");
    CkStringBuilderW_Append(sbJwk,L"tUkTRclIfuEPmNsNDPbLoLqqCVznFbvdB7x-Tl-m0l_eFTj2KiqwGqE9PZ");
    CkStringBuilderW_Append(sbJwk,L"B9nNTwMVvH3VRRSLWACvPnSiwP8N5Usy-WRXS-V7TbpxIhvepTfE0NNo\"");
    CkStringBuilderW_Append(sbJwk,L"}");

    // Load this JWK into a Chilkat private key object.
    rsaPrivKey = CkPrivateKeyW_Create();
    success = CkPrivateKeyW_LoadJwk(rsaPrivKey,CkStringBuilderW_getAsString(sbJwk));
    if (success != TRUE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(rsaPrivKey));
        CkJweW_Dispose(jwe);
        CkJsonObjectW_Dispose(jweProtHdr);
        CkJsonObjectW_Dispose(jweRecipientHdr1);
        CkJsonObjectW_Dispose(jweRecipientHdr2);
        CkJsonObjectW_Dispose(jweUnprotHdr);
        CkStringBuilderW_Dispose(sbJwk);
        CkPrivateKeyW_Dispose(rsaPrivKey);
        return;
    }

    // Get the public key for encryption.  (The RSA private key is used for decryption.)
    rsaPubKey = CkPrivateKeyW_GetPublicKey(rsaPrivKey);
    recipientIndex = 0;
    CkJweW_SetPublicKey(jwe,recipientIndex,rsaPubKey);
    CkPublicKeyW_Dispose(rsaPubKey);

    // Recipient 1 uses AES Key Wrap
    aesWrappingKey = L"GawgguFyGrWKav7AX4VKUg";
    recipientIndex = 1;
    CkJweW_SetWrappingKey(jwe,recipientIndex,aesWrappingKey,L"base64url");

    // OK.. everything has been specified.
    // Now encrypt.  Chilkat will use the general JSON serialization because it is only
    // possible serializationto use given there are multiple recipients. 
    strJwe = CkJweW_encrypt(jwe,plaintext,L"utf-8");
    if (CkJweW_getLastMethodSuccess(jwe) != TRUE) {
        wprintf(L"%s\n",CkJweW_lastErrorText(jwe));
        CkJweW_Dispose(jwe);
        CkJsonObjectW_Dispose(jweProtHdr);
        CkJsonObjectW_Dispose(jweRecipientHdr1);
        CkJsonObjectW_Dispose(jweRecipientHdr2);
        CkJsonObjectW_Dispose(jweUnprotHdr);
        CkStringBuilderW_Dispose(sbJwk);
        CkPrivateKeyW_Dispose(rsaPrivKey);
        return;
    }

    // The strJwe is in the most compact form possible (a single line).
    // Let's load it into a JSON object and examine in a non-compact pretty-printed format:
    jsonTemp = CkJsonObjectW_Create();
    CkJsonObjectW_Load(jsonTemp,strJwe);
    CkJsonObjectW_putEmitCompact(jsonTemp,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(jsonTemp));

    // The JWE looks like this:
    // (Note: Because of random values used in the encryption process, your encrypted results will be different.)

    // 	{ 
    // 	  "protected": "eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0",
    // 	  "unprotected": { 
    // 	    "jku": "https://server.example.com/keys.jwks"
    // 	  },
    // 	  "recipients": [
    // 	    { 
    // 	      "header": { 
    // 	        "alg": "RSA1_5",
    // 	        "kid": "2011-04-29"
    // 	      },
    // 	      "encrypted_key": "WlUggejR-vStJVNKgjG2mQrVv74aga1FFutT8E-n-yJEOTsOVnGjhj1NW_Snd9DqHAhFrc7NEvKCFplKWGnusBZxxjm1JpdUa0MIpkGmncJHQQdfm21vcbEUbDmfqVY79SnEis3tih1D3qmyp_Bxti4byDAHJIOJv_cj0Sx8oHZzgGtOLjtHsydyo1MtBIqI0w86i_uhUuraihZ3ngj67YZ6uqpR6lulkPIVohfF3oJ0D3Ay_XOCHWlHYkTg6VZsa0FDrPtfB2pG3TxTBPwI4IMj5uF_D3zrg51dCYKU1Gah71ujLaXRE5q9XF7oOknxiWQuWc7Ox8JP03lSx-DiVA"
    // 	    },
    // 	    { 
    // 	      "header": { 
    // 	        "alg": "A128KW",
    // 	        "kid": "7"
    // 	      },
    // 	      "encrypted_key": "8diBP2aUUB_Jl5WxCuMJLN6HsppE3rhSjcecee0fBcwB31zbPAVejQ"
    // 	    } 
    // 	  ],
    // 	  "iv": "JqahFKx5Z8SFT_LnkfOq0Q",
    // 	  "ciphertext": "YcSdjlszsaY1ADcs4Vw85H_WoAqnkDIJaJsTkmCj05s",
    // 	  "tag": "G3KF2CZ-DMhm0cqUbhJKMA"
    // 	} 

    // To decrypt, we don't need both recipient keys.  We can decrypt with one key or the other.
    // The FindRecipient method can be used to find a particular recipient index.

    // First, load the JWE..
    jwe2 = CkJweW_Create();
    success = CkJweW_LoadJwe(jwe2,strJwe);
    if (success != TRUE) {
        wprintf(L"%s\n",CkJweW_lastErrorText(jwe2));
        CkJweW_Dispose(jwe);
        CkJsonObjectW_Dispose(jweProtHdr);
        CkJsonObjectW_Dispose(jweRecipientHdr1);
        CkJsonObjectW_Dispose(jweRecipientHdr2);
        CkJsonObjectW_Dispose(jweUnprotHdr);
        CkStringBuilderW_Dispose(sbJwk);
        CkPrivateKeyW_Dispose(rsaPrivKey);
        CkJsonObjectW_Dispose(jsonTemp);
        CkJweW_Dispose(jwe2);
        return;
    }

    // Let's say we have the AES key wrap key, and we know the "kid" equals "7".
    caseSensitive = FALSE;
    recipientIndex = CkJweW_FindRecipient(jwe2,L"kid",L"7",caseSensitive);
    if (recipientIndex < 0) {
        wprintf(L"Unable to find recipient with kid=7\n");
        CkJweW_Dispose(jwe);
        CkJsonObjectW_Dispose(jweProtHdr);
        CkJsonObjectW_Dispose(jweRecipientHdr1);
        CkJsonObjectW_Dispose(jweRecipientHdr2);
        CkJsonObjectW_Dispose(jweUnprotHdr);
        CkStringBuilderW_Dispose(sbJwk);
        CkPrivateKeyW_Dispose(rsaPrivKey);
        CkJsonObjectW_Dispose(jsonTemp);
        CkJweW_Dispose(jwe2);
        return;
    }

    // Set the AES wrap key for the recipient index.
    CkJweW_SetWrappingKey(jwe2,recipientIndex,aesWrappingKey,L"base64url");

    // Decrypt
    originalPlaintext = CkJweW_decrypt(jwe2,recipientIndex,L"utf-8");
    if (CkJweW_getLastMethodSuccess(jwe2) != TRUE) {
        wprintf(L"%s\n",CkJweW_lastErrorText(jwe2));
        CkJweW_Dispose(jwe);
        CkJsonObjectW_Dispose(jweProtHdr);
        CkJsonObjectW_Dispose(jweRecipientHdr1);
        CkJsonObjectW_Dispose(jweRecipientHdr2);
        CkJsonObjectW_Dispose(jweUnprotHdr);
        CkStringBuilderW_Dispose(sbJwk);
        CkPrivateKeyW_Dispose(rsaPrivKey);
        CkJsonObjectW_Dispose(jsonTemp);
        CkJweW_Dispose(jwe2);
        return;
    }

    wprintf(L"original text decrypted with AES key wrap key: \n");
    wprintf(L"%s\n",originalPlaintext);

    // Now, let's do the same with the RSA private key:
    recipientIndex = CkJweW_FindRecipient(jwe2,L"kid",L"2011-04-29",caseSensitive);
    if (recipientIndex < 0) {
        wprintf(L"Unable to find recipient with kid=2011-04-29\n");
        CkJweW_Dispose(jwe);
        CkJsonObjectW_Dispose(jweProtHdr);
        CkJsonObjectW_Dispose(jweRecipientHdr1);
        CkJsonObjectW_Dispose(jweRecipientHdr2);
        CkJsonObjectW_Dispose(jweUnprotHdr);
        CkStringBuilderW_Dispose(sbJwk);
        CkPrivateKeyW_Dispose(rsaPrivKey);
        CkJsonObjectW_Dispose(jsonTemp);
        CkJweW_Dispose(jwe2);
        return;
    }

    // Set the RSA private key for the recipient index.
    CkJweW_SetPrivateKey(jwe2,recipientIndex,rsaPrivKey);

    // Decrypt
    originalPlaintext = CkJweW_decrypt(jwe2,recipientIndex,L"utf-8");
    if (CkJweW_getLastMethodSuccess(jwe2) != TRUE) {
        wprintf(L"%s\n",CkJweW_lastErrorText(jwe2));
        CkJweW_Dispose(jwe);
        CkJsonObjectW_Dispose(jweProtHdr);
        CkJsonObjectW_Dispose(jweRecipientHdr1);
        CkJsonObjectW_Dispose(jweRecipientHdr2);
        CkJsonObjectW_Dispose(jweUnprotHdr);
        CkStringBuilderW_Dispose(sbJwk);
        CkPrivateKeyW_Dispose(rsaPrivKey);
        CkJsonObjectW_Dispose(jsonTemp);
        CkJweW_Dispose(jwe2);
        return;
    }

    wprintf(L"original text decrypted with RSA private key: \n");
    wprintf(L"%s\n",originalPlaintext);

    // ---------------------------------------------------------------------------------
    // It should also be possible to decrypt the JWE as shown in RFC 7516, Appendix A.4.7
    // because it was produced using the same keys.

    sbJwe = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbJwe,L"{");
    CkStringBuilderW_Append(sbJwe,L"\"protected\":");
    CkStringBuilderW_Append(sbJwe,L"\"eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0\",");
    CkStringBuilderW_Append(sbJwe,L"\"unprotected\":");
    CkStringBuilderW_Append(sbJwe,L"{\"jku\":\"https://server.example.com/keys.jwks\"},");
    CkStringBuilderW_Append(sbJwe,L"\"recipients\":[");
    CkStringBuilderW_Append(sbJwe,L"{\"header\":");
    CkStringBuilderW_Append(sbJwe,L"{\"alg\":\"RSA1_5\",\"kid\":\"2011-04-29\"},");
    CkStringBuilderW_Append(sbJwe,L"\"encrypted_key\":");
    CkStringBuilderW_Append(sbJwe,L"\"UGhIOguC7IuEvf_NPVaXsGMoLOmwvc1GyqlIKOK1nN94nHPoltGRhWhw7Zx0-");
    CkStringBuilderW_Append(sbJwe,L"kFm1NJn8LE9XShH59_i8J0PH5ZZyNfGy2xGdULU7sHNF6Gp2vPLgNZ__deLKx");
    CkStringBuilderW_Append(sbJwe,L"GHZ7PcHALUzoOegEI-8E66jX2E4zyJKx-YxzZIItRzC5hlRirb6Y5Cl_p-ko3");
    CkStringBuilderW_Append(sbJwe,L"YvkkysZIFNPccxRU7qve1WYPxqbb2Yw8kZqa2rMWI5ng8OtvzlV7elprCbuPh");
    CkStringBuilderW_Append(sbJwe,L"cCdZ6XDP0_F8rkXds2vE4X-ncOIM8hAYHHi29NX0mcKiRaD0-D-ljQTP-cFPg");
    CkStringBuilderW_Append(sbJwe,L"wCp6X-nZZd9OHBv-B3oWh2TbqmScqXMR4gp_A\"},");
    CkStringBuilderW_Append(sbJwe,L"{\"header\":");
    CkStringBuilderW_Append(sbJwe,L"{\"alg\":\"A128KW\",\"kid\":\"7\"},");
    CkStringBuilderW_Append(sbJwe,L"\"encrypted_key\":");
    CkStringBuilderW_Append(sbJwe,L"\"6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ\"}],");
    CkStringBuilderW_Append(sbJwe,L"\"iv\":");
    CkStringBuilderW_Append(sbJwe,L"\"AxY8DCtDaGlsbGljb3RoZQ\",");
    CkStringBuilderW_Append(sbJwe,L"\"ciphertext\":");
    CkStringBuilderW_Append(sbJwe,L"\"KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY\",");
    CkStringBuilderW_Append(sbJwe,L"\"tag\":");
    CkStringBuilderW_Append(sbJwe,L"\"Mz-VPPyU4RlcuYv1IwIvzw\"");
    CkStringBuilderW_Append(sbJwe,L"}");

    success = CkJweW_LoadJweSb(jwe2,sbJwe);
    if (success != TRUE) {
        wprintf(L"%s\n",CkJweW_lastErrorText(jwe2));
        CkJweW_Dispose(jwe);
        CkJsonObjectW_Dispose(jweProtHdr);
        CkJsonObjectW_Dispose(jweRecipientHdr1);
        CkJsonObjectW_Dispose(jweRecipientHdr2);
        CkJsonObjectW_Dispose(jweUnprotHdr);
        CkStringBuilderW_Dispose(sbJwk);
        CkPrivateKeyW_Dispose(rsaPrivKey);
        CkJsonObjectW_Dispose(jsonTemp);
        CkJweW_Dispose(jwe2);
        CkStringBuilderW_Dispose(sbJwe);
        return;
    }

    // We can decrypt with either key.  Let's use the AES key wrap key...
    recipientIndex = CkJweW_FindRecipient(jwe2,L"kid",L"7",caseSensitive);
    if (recipientIndex < 0) {
        wprintf(L"Unable to find recipient with kid=7\n");
        CkJweW_Dispose(jwe);
        CkJsonObjectW_Dispose(jweProtHdr);
        CkJsonObjectW_Dispose(jweRecipientHdr1);
        CkJsonObjectW_Dispose(jweRecipientHdr2);
        CkJsonObjectW_Dispose(jweUnprotHdr);
        CkStringBuilderW_Dispose(sbJwk);
        CkPrivateKeyW_Dispose(rsaPrivKey);
        CkJsonObjectW_Dispose(jsonTemp);
        CkJweW_Dispose(jwe2);
        CkStringBuilderW_Dispose(sbJwe);
        return;
    }

    // Set the AES wrap key for the recipient index.
    CkJweW_SetWrappingKey(jwe2,recipientIndex,aesWrappingKey,L"base64url");

    // Decrypt
    originalPlaintext = CkJweW_decrypt(jwe2,recipientIndex,L"utf-8");
    if (CkJweW_getLastMethodSuccess(jwe2) != TRUE) {
        wprintf(L"%s\n",CkJweW_lastErrorText(jwe2));
        CkJweW_Dispose(jwe);
        CkJsonObjectW_Dispose(jweProtHdr);
        CkJsonObjectW_Dispose(jweRecipientHdr1);
        CkJsonObjectW_Dispose(jweRecipientHdr2);
        CkJsonObjectW_Dispose(jweUnprotHdr);
        CkStringBuilderW_Dispose(sbJwk);
        CkPrivateKeyW_Dispose(rsaPrivKey);
        CkJsonObjectW_Dispose(jsonTemp);
        CkJweW_Dispose(jwe2);
        CkStringBuilderW_Dispose(sbJwe);
        return;
    }

    wprintf(L"original text decrypted from published JWE, with AES key wrap key: \n");
    wprintf(L"%s\n",originalPlaintext);

    // The output:

    // original text decrypted with AES key wrap key: 
    // Live long and prosper.
    // original text decrypted with RSA private key: 
    // Live long and prosper.
    // original text decrypted from published JWE, with AES key wrap key: 
    // Live long and prosper.


    CkJweW_Dispose(jwe);
    CkJsonObjectW_Dispose(jweProtHdr);
    CkJsonObjectW_Dispose(jweRecipientHdr1);
    CkJsonObjectW_Dispose(jweRecipientHdr2);
    CkJsonObjectW_Dispose(jweUnprotHdr);
    CkStringBuilderW_Dispose(sbJwk);
    CkPrivateKeyW_Dispose(rsaPrivKey);
    CkJsonObjectW_Dispose(jsonTemp);
    CkJweW_Dispose(jwe2);
    CkStringBuilderW_Dispose(sbJwe);

    }

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.