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
(Visual FoxPro) JWE Using General JWE JSON SerializationThis 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.
LOCAL lnSuccess LOCAL lcPlaintext LOCAL loJwe LOCAL loJweProtHdr LOCAL loJweRecipientHdr1 LOCAL lnRecipientIndex LOCAL loJweRecipientHdr2 LOCAL loJweUnprotHdr LOCAL loSbJwk LOCAL loRsaPrivKey LOCAL loRsaPubKey LOCAL lcAesWrappingKey LOCAL lcStrJwe LOCAL loJsonTemp LOCAL loJwe2 LOCAL lnCaseSensitive LOCAL lcOriginalPlaintext LOCAL loSbJwe * 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. lcPlaintext = "Live long and prosper." * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Jwe') loJwe = CreateObject('Chilkat.Jwe') * First build the JWE Protected Header: {"enc":"A128CBC-HS256"} * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject') loJweProtHdr = CreateObject('Chilkat.JsonObject') loJweProtHdr.AppendString("enc","A128CBC-HS256") loJwe.SetProtectedHeader(loJweProtHdr) * 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"} * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject') loJweRecipientHdr1 = CreateObject('Chilkat.JsonObject') loJweRecipientHdr1.AppendString("alg","RSA1_5") loJweRecipientHdr1.AppendString("kid","2011-04-29") lnRecipientIndex = 0 loJwe.SetRecipientHeader(lnRecipientIndex,loJweRecipientHdr1) * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject') loJweRecipientHdr2 = CreateObject('Chilkat.JsonObject') loJweRecipientHdr2.AppendString("alg","A128KW") loJweRecipientHdr2.AppendString("kid","7") lnRecipientIndex = 1 loJwe.SetRecipientHeader(lnRecipientIndex,loJweRecipientHdr2) * Set the Shared Unprotected Header: {"jku":"https://server.example.com/keys.jwks"} * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject') loJweUnprotHdr = CreateObject('Chilkat.JsonObject') loJweUnprotHdr.AppendString("jku","https://server.example.com/keys.jwks") loJwe.SetUnprotectedHeader(loJweUnprotHdr) * 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: * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.StringBuilder') loSbJwk = CreateObject('Chilkat.StringBuilder') loSbJwk.Append('{"kty":"RSA",') loSbJwk.Append('"n":"sXchDaQebHnPiGvyDOAT4saGEUetSyo9MKLOoWFsueri23bOdgWp4Dy1Wl') loSbJwk.Append("UzewbgBHod5pcM9H95GQRV3JDXboIRROSBigeC5yjU1hGzHHyXss8UDpre") loSbJwk.Append("cbAYxknTcQkhslANGRUZmdTOQ5qTRsLAt6BTYuyvVRdhS8exSZEy_c4gs_") loSbJwk.Append("7svlJJQ4H9_NxsiIoLwAEk7-Q3UXERGYw_75IDrGA84-lA_-Ct4eTlXHBI") loSbJwk.Append("Y2EaV7t7LjJaynVJCpkv4LKjTTAumiGUIuQhrNhZLuF_RJLqHpM2kgWFLU") loSbJwk.Append('7-VTdL1VbC2tejvcI2BlMkEpk1BzBZI0KQB0GaDWFLN-aEAw3vRw",') loSbJwk.Append('"e":"AQAB",') loSbJwk.Append('"d":"VFCWOqXr8nvZNyaaJLXdnNPXZKRaWCjkU5Q2egQQpTBMwhprMzWzpR8Sxq') loSbJwk.Append("1OPThh_J6MUD8Z35wky9b8eEO0pwNS8xlh1lOFRRBoNqDIKVOku0aZb-ry") loSbJwk.Append("nq8cxjDTLZQ6Fz7jSjR1Klop-YKaUHc9GsEofQqYruPhzSA-QgajZGPbE_") loSbJwk.Append("0ZaVDJHfyd7UUBUKunFMScbflYAAOYJqVIVwaYR5zWEEceUjNnTNo_CVSj") loSbJwk.Append("-VvXLO5VZfCUAVLgW4dpf1SrtZjSt34YLsRarSb127reG_DUwg9Ch-Kyvj") loSbJwk.Append('T1SkHgUWRVGcyly7uvVGRSDwsXypdrNinPA4jlhoNdizK2zF2CWQ",') loSbJwk.Append('"p":"9gY2w6I6S6L0juEKsbeDAwpd9WMfgqFoeA9vEyEUuk4kLwBKcoe1x4HG68') loSbJwk.Append("ik918hdDSE9vDQSccA3xXHOAFOPJ8R9EeIAbTi1VwBYnbTp87X-xcPWlEP") loSbJwk.Append('krdoUKW60tgs1aNd_Nnc9LEVVPMS390zbFxt8TN_biaBgelNgbC95sM",') loSbJwk.Append('"q":"uKlCKvKv_ZJMVcdIs5vVSU_6cPtYI1ljWytExV_skstvRSNi9r66jdd9-y') loSbJwk.Append("BhVfuG4shsp2j7rGnIio901RBeHo6TPKWVVykPu1iYhQXw1jIABfw-MVsN") loSbJwk.Append('-3bQ76WLdt2SDxsHs7q7zPyUyHXmps7ycZ5c72wGkUwNOjYelmkiNS0",') loSbJwk.Append('"dp":"w0kZbV63cVRvVX6yk3C8cMxo2qCM4Y8nsq1lmMSYhG4EcL6FWbX5h9yuv') loSbJwk.Append("ngs4iLEFk6eALoUS4vIWEwcL4txw9LsWH_zKI-hwoReoP77cOdSL4AVcra") loSbJwk.Append('Hawlkpyd2TWjE5evgbhWtOxnZee3cXJBkAi64Ik6jZxbvk-RR3pEhnCs",') loSbJwk.Append('"dq":"o_8V14SezckO6CNLKs_btPdFiO9_kC1DsuUTd2LAfIIVeMZ7jn1Gus_Ff') loSbJwk.Append("7B7IVx3p5KuBGOVF8L-qifLb6nQnLysgHDh132NDioZkhH7mI7hPG-PYE_") loSbJwk.Append('odApKdnqECHWw0J-F0JWnUd6D2B_1TvF9mXA2Qx-iGYn8OVV1Bsmp6qU",') loSbJwk.Append('"qi":"eNho5yRBEBxhGBtQRww9QirZsB66TrfFReG_CcteI1aCneT0ELGhYlRlC') loSbJwk.Append("tUkTRclIfuEPmNsNDPbLoLqqCVznFbvdB7x-Tl-m0l_eFTj2KiqwGqE9PZ") loSbJwk.Append('B9nNTwMVvH3VRRSLWACvPnSiwP8N5Usy-WRXS-V7TbpxIhvepTfE0NNo"') loSbJwk.Append("}") * Load this JWK into a Chilkat private key object. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.PrivateKey') loRsaPrivKey = CreateObject('Chilkat.PrivateKey') lnSuccess = loRsaPrivKey.LoadJwk(loSbJwk.GetAsString()) IF (lnSuccess <> 1) THEN ? loRsaPrivKey.LastErrorText RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey CANCEL ENDIF * Get the public key for encryption. (The RSA private key is used for decryption.) loRsaPubKey = loRsaPrivKey.GetPublicKey() lnRecipientIndex = 0 loJwe.SetPublicKey(lnRecipientIndex,loRsaPubKey) RELEASE loRsaPubKey * Recipient 1 uses AES Key Wrap lcAesWrappingKey = "GawgguFyGrWKav7AX4VKUg" lnRecipientIndex = 1 loJwe.SetWrappingKey(lnRecipientIndex,lcAesWrappingKey,"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. lcStrJwe = loJwe.Encrypt(lcPlaintext,"utf-8") IF (loJwe.LastMethodSuccess <> 1) THEN ? loJwe.LastErrorText RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey CANCEL ENDIF * 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: * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject') loJsonTemp = CreateObject('Chilkat.JsonObject') loJsonTemp.Load(lcStrJwe) loJsonTemp.EmitCompact = 0 ? loJsonTemp.Emit() * 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.. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Jwe') loJwe2 = CreateObject('Chilkat.Jwe') lnSuccess = loJwe2.LoadJwe(lcStrJwe) IF (lnSuccess <> 1) THEN ? loJwe2.LastErrorText RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey RELEASE loJsonTemp RELEASE loJwe2 CANCEL ENDIF * Let's say we have the AES key wrap key, and we know the "kid" equals "7". lnCaseSensitive = 0 lnRecipientIndex = loJwe2.FindRecipient("kid","7",lnCaseSensitive) IF (lnRecipientIndex < 0) THEN ? "Unable to find recipient with kid=7" RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey RELEASE loJsonTemp RELEASE loJwe2 CANCEL ENDIF * Set the AES wrap key for the recipient index. loJwe2.SetWrappingKey(lnRecipientIndex,lcAesWrappingKey,"base64url") * Decrypt lcOriginalPlaintext = loJwe2.Decrypt(lnRecipientIndex,"utf-8") IF (loJwe2.LastMethodSuccess <> 1) THEN ? loJwe2.LastErrorText RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey RELEASE loJsonTemp RELEASE loJwe2 CANCEL ENDIF ? "original text decrypted with AES key wrap key: " ? lcOriginalPlaintext * Now, let's do the same with the RSA private key: lnRecipientIndex = loJwe2.FindRecipient("kid","2011-04-29",lnCaseSensitive) IF (lnRecipientIndex < 0) THEN ? "Unable to find recipient with kid=2011-04-29" RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey RELEASE loJsonTemp RELEASE loJwe2 CANCEL ENDIF * Set the RSA private key for the recipient index. loJwe2.SetPrivateKey(lnRecipientIndex,loRsaPrivKey) * Decrypt lcOriginalPlaintext = loJwe2.Decrypt(lnRecipientIndex,"utf-8") IF (loJwe2.LastMethodSuccess <> 1) THEN ? loJwe2.LastErrorText RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey RELEASE loJsonTemp RELEASE loJwe2 CANCEL ENDIF ? "original text decrypted with RSA private key: " ? lcOriginalPlaintext * --------------------------------------------------------------------------------- * 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. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.StringBuilder') loSbJwe = CreateObject('Chilkat.StringBuilder') loSbJwe.Append("{") loSbJwe.Append('"protected":') loSbJwe.Append('"eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0",') loSbJwe.Append('"unprotected":') loSbJwe.Append('{"jku":"https://server.example.com/keys.jwks"},') loSbJwe.Append('"recipients":[') loSbJwe.Append('{"header":') loSbJwe.Append('{"alg":"RSA1_5","kid":"2011-04-29"},') loSbJwe.Append('"encrypted_key":') loSbJwe.Append('"UGhIOguC7IuEvf_NPVaXsGMoLOmwvc1GyqlIKOK1nN94nHPoltGRhWhw7Zx0-') loSbJwe.Append("kFm1NJn8LE9XShH59_i8J0PH5ZZyNfGy2xGdULU7sHNF6Gp2vPLgNZ__deLKx") loSbJwe.Append("GHZ7PcHALUzoOegEI-8E66jX2E4zyJKx-YxzZIItRzC5hlRirb6Y5Cl_p-ko3") loSbJwe.Append("YvkkysZIFNPccxRU7qve1WYPxqbb2Yw8kZqa2rMWI5ng8OtvzlV7elprCbuPh") loSbJwe.Append("cCdZ6XDP0_F8rkXds2vE4X-ncOIM8hAYHHi29NX0mcKiRaD0-D-ljQTP-cFPg") loSbJwe.Append('wCp6X-nZZd9OHBv-B3oWh2TbqmScqXMR4gp_A"},') loSbJwe.Append('{"header":') loSbJwe.Append('{"alg":"A128KW","kid":"7"},') loSbJwe.Append('"encrypted_key":') loSbJwe.Append('"6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ"}],') loSbJwe.Append('"iv":') loSbJwe.Append('"AxY8DCtDaGlsbGljb3RoZQ",') loSbJwe.Append('"ciphertext":') loSbJwe.Append('"KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY",') loSbJwe.Append('"tag":') loSbJwe.Append('"Mz-VPPyU4RlcuYv1IwIvzw"') loSbJwe.Append("}") lnSuccess = loJwe2.LoadJweSb(loSbJwe) IF (lnSuccess <> 1) THEN ? loJwe2.LastErrorText RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey RELEASE loJsonTemp RELEASE loJwe2 RELEASE loSbJwe CANCEL ENDIF * We can decrypt with either key. Let's use the AES key wrap key... lnRecipientIndex = loJwe2.FindRecipient("kid","7",lnCaseSensitive) IF (lnRecipientIndex < 0) THEN ? "Unable to find recipient with kid=7" RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey RELEASE loJsonTemp RELEASE loJwe2 RELEASE loSbJwe CANCEL ENDIF * Set the AES wrap key for the recipient index. loJwe2.SetWrappingKey(lnRecipientIndex,lcAesWrappingKey,"base64url") * Decrypt lcOriginalPlaintext = loJwe2.Decrypt(lnRecipientIndex,"utf-8") IF (loJwe2.LastMethodSuccess <> 1) THEN ? loJwe2.LastErrorText RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey RELEASE loJsonTemp RELEASE loJwe2 RELEASE loSbJwe CANCEL ENDIF ? "original text decrypted from published JWE, with AES key wrap key: " ? lcOriginalPlaintext * 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. RELEASE loJwe RELEASE loJweProtHdr RELEASE loJweRecipientHdr1 RELEASE loJweRecipientHdr2 RELEASE loJweUnprotHdr RELEASE loSbJwk RELEASE loRsaPrivKey RELEASE loJsonTemp RELEASE loJwe2 RELEASE loSbJwe |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.