DataFlex
DataFlex
JWE using RSAES-PKCS1-v1_5 and AES_128_CBC_HMAC_SHA_256
See more JSON Web Encryption (JWE) Examples
This example duplicates the example A.2 in RFC 7516 for JSON Web Encryption (JWE).Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
String sPlaintext
ProtHdr Handle hoJweProtHdr
Handle hoSbJwk
Variant vRsaPrivKey
Handle hoRsaPrivKey
Variant vRsaPubKey
Handle hoRsaPubKey
Handle hoJwe
String sStrJwe
2 Handle hoJwe2
String sOriginalPlaintext
Variant vSbJwe
Handle hoSbJwe
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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.
Move "Live long and prosper." To sPlaintext
// First build the JWE Protected Header.
// We want to build this: {"alg":"RSA1_5","enc":"A128CBC-HS256"}
Get Create (RefClass(cComChilkatJsonObject)) To hoJweProtHdr
If (Not(IsComObjectCreated(hoJweProtHdr))) Begin
Send CreateComObject of hoJweProtHdr
End
Get ComAppendString Of hoJweProtHdr "alg" "RSA1_5" To iSuccess
Get ComAppendString Of hoJweProtHdr "enc" "A128CBC-HS256" To iSuccess
Get ComEmit Of hoJweProtHdr To sTemp1
Showln "JWE Protected Header: " sTemp1
Showln "--"
// The specific RSA key used in the A.2 example is the following JWK:
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJwk
If (Not(IsComObjectCreated(hoSbJwk))) Begin
Send CreateComObject of hoSbJwk
End
Get ComAppend Of hoSbJwk '{"kty":"RSA",' To iSuccess
Get ComAppend Of hoSbJwk '"n":"sXchDaQebHnPiGvyDOAT4saGEUetSyo9MKLOoWFsueri23bOdgWp4Dy1Wl' To iSuccess
Get ComAppend Of hoSbJwk "UzewbgBHod5pcM9H95GQRV3JDXboIRROSBigeC5yjU1hGzHHyXss8UDpre" To iSuccess
Get ComAppend Of hoSbJwk "cbAYxknTcQkhslANGRUZmdTOQ5qTRsLAt6BTYuyvVRdhS8exSZEy_c4gs_" To iSuccess
Get ComAppend Of hoSbJwk "7svlJJQ4H9_NxsiIoLwAEk7-Q3UXERGYw_75IDrGA84-lA_-Ct4eTlXHBI" To iSuccess
Get ComAppend Of hoSbJwk "Y2EaV7t7LjJaynVJCpkv4LKjTTAumiGUIuQhrNhZLuF_RJLqHpM2kgWFLU" To iSuccess
Get ComAppend Of hoSbJwk '7-VTdL1VbC2tejvcI2BlMkEpk1BzBZI0KQB0GaDWFLN-aEAw3vRw",' To iSuccess
Get ComAppend Of hoSbJwk '"e":"AQAB",' To iSuccess
Get ComAppend Of hoSbJwk '"d":"VFCWOqXr8nvZNyaaJLXdnNPXZKRaWCjkU5Q2egQQpTBMwhprMzWzpR8Sxq' To iSuccess
Get ComAppend Of hoSbJwk "1OPThh_J6MUD8Z35wky9b8eEO0pwNS8xlh1lOFRRBoNqDIKVOku0aZb-ry" To iSuccess
Get ComAppend Of hoSbJwk "nq8cxjDTLZQ6Fz7jSjR1Klop-YKaUHc9GsEofQqYruPhzSA-QgajZGPbE_" To iSuccess
Get ComAppend Of hoSbJwk "0ZaVDJHfyd7UUBUKunFMScbflYAAOYJqVIVwaYR5zWEEceUjNnTNo_CVSj" To iSuccess
Get ComAppend Of hoSbJwk "-VvXLO5VZfCUAVLgW4dpf1SrtZjSt34YLsRarSb127reG_DUwg9Ch-Kyvj" To iSuccess
Get ComAppend Of hoSbJwk 'T1SkHgUWRVGcyly7uvVGRSDwsXypdrNinPA4jlhoNdizK2zF2CWQ",' To iSuccess
Get ComAppend Of hoSbJwk '"p":"9gY2w6I6S6L0juEKsbeDAwpd9WMfgqFoeA9vEyEUuk4kLwBKcoe1x4HG68' To iSuccess
Get ComAppend Of hoSbJwk "ik918hdDSE9vDQSccA3xXHOAFOPJ8R9EeIAbTi1VwBYnbTp87X-xcPWlEP" To iSuccess
Get ComAppend Of hoSbJwk 'krdoUKW60tgs1aNd_Nnc9LEVVPMS390zbFxt8TN_biaBgelNgbC95sM",' To iSuccess
Get ComAppend Of hoSbJwk '"q":"uKlCKvKv_ZJMVcdIs5vVSU_6cPtYI1ljWytExV_skstvRSNi9r66jdd9-y' To iSuccess
Get ComAppend Of hoSbJwk "BhVfuG4shsp2j7rGnIio901RBeHo6TPKWVVykPu1iYhQXw1jIABfw-MVsN" To iSuccess
Get ComAppend Of hoSbJwk '-3bQ76WLdt2SDxsHs7q7zPyUyHXmps7ycZ5c72wGkUwNOjYelmkiNS0",' To iSuccess
Get ComAppend Of hoSbJwk '"dp":"w0kZbV63cVRvVX6yk3C8cMxo2qCM4Y8nsq1lmMSYhG4EcL6FWbX5h9yuv' To iSuccess
Get ComAppend Of hoSbJwk "ngs4iLEFk6eALoUS4vIWEwcL4txw9LsWH_zKI-hwoReoP77cOdSL4AVcra" To iSuccess
Get ComAppend Of hoSbJwk 'Hawlkpyd2TWjE5evgbhWtOxnZee3cXJBkAi64Ik6jZxbvk-RR3pEhnCs",' To iSuccess
Get ComAppend Of hoSbJwk '"dq":"o_8V14SezckO6CNLKs_btPdFiO9_kC1DsuUTd2LAfIIVeMZ7jn1Gus_Ff' To iSuccess
Get ComAppend Of hoSbJwk "7B7IVx3p5KuBGOVF8L-qifLb6nQnLysgHDh132NDioZkhH7mI7hPG-PYE_" To iSuccess
Get ComAppend Of hoSbJwk 'odApKdnqECHWw0J-F0JWnUd6D2B_1TvF9mXA2Qx-iGYn8OVV1Bsmp6qU",' To iSuccess
Get ComAppend Of hoSbJwk '"qi":"eNho5yRBEBxhGBtQRww9QirZsB66TrfFReG_CcteI1aCneT0ELGhYlRlC' To iSuccess
Get ComAppend Of hoSbJwk "tUkTRclIfuEPmNsNDPbLoLqqCVznFbvdB7x-Tl-m0l_eFTj2KiqwGqE9PZ" To iSuccess
Get ComAppend Of hoSbJwk 'B9nNTwMVvH3VRRSLWACvPnSiwP8N5Usy-WRXS-V7TbpxIhvepTfE0NNo"' To iSuccess
Get ComAppend Of hoSbJwk "}" To iSuccess
// Load this JWK into a Chilkat private key object.
Get Create (RefClass(cComChilkatPrivateKey)) To hoRsaPrivKey
If (Not(IsComObjectCreated(hoRsaPrivKey))) Begin
Send CreateComObject of hoRsaPrivKey
End
Get ComGetAsString Of hoSbJwk To sTemp1
Get ComLoadJwk Of hoRsaPrivKey sTemp1 To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoRsaPrivKey To sTemp1
Showln sTemp1
Procedure_Return
End
// The public key is used to encrypt (i.e. create the JWE),
// and the private key is used to decrypt.
// The RSA public key is simply a subset of the private key. The RSA public key
// is composed of the "n" and "e" members shown above. These are also known as the
// modulus and exponent.
// We can simply get the public key object from the private key object
Get Create (RefClass(cComChilkatPublicKey)) To hoRsaPubKey
If (Not(IsComObjectCreated(hoRsaPubKey))) Begin
Send CreateComObject of hoRsaPubKey
End
Get pvComObject of hoRsaPubKey to vRsaPubKey
Get ComToPublicKey Of hoRsaPrivKey vRsaPubKey To iSuccess
// Create the JWE...
Get Create (RefClass(cComChilkatJwe)) To hoJwe
If (Not(IsComObjectCreated(hoJwe))) Begin
Send CreateComObject of hoJwe
End
Get pvComObject of hoJweProtHdr to vJweProtHdr
Get ComSetProtectedHeader Of hoJwe vJweProtHdr To iSuccess
Get pvComObject of hoRsaPubKey to vRsaPubKey
Get ComSetPublicKey Of hoJwe 0 vRsaPubKey To iSuccess
Get ComEncrypt Of hoJwe sPlaintext "utf-8" To sStrJwe
Get ComLastMethodSuccess Of hoJwe To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoJwe To sTemp1
Showln sTemp1
Procedure_Return
End
// Show the JWE we just created:
Showln sStrJwe
// Note: The RSA PKCS1_V1_5 padding uses random value, and the results
// will be different each time. However, each result should be successfully
// decrypting if using the correct RSA private key.
// Let's decrypt the JWE that was just produced.
// Do the following to decrypt a JWE:
// 1) Load the JWE.
// 2) Set the private key for decryption.
// 3) Decrypt.
Get Create (RefClass(cComChilkatJwe)) To hoJwe2
If (Not(IsComObjectCreated(hoJwe2))) Begin
Send CreateComObject of hoJwe2
End
Get ComLoadJwe Of hoJwe2 sStrJwe To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoJwe2 To sTemp1
Showln sTemp1
Procedure_Return
End
// Provide the RSA private key for decryption.
// (The JWE was encrypted for a single recipient at index 0.)
Get pvComObject of hoRsaPrivKey to vRsaPrivKey
Get ComSetPrivateKey Of hoJwe2 0 vRsaPrivKey To iSuccess
// Decrypt.
Get ComDecrypt Of hoJwe2 0 "utf-8" To sOriginalPlaintext
Get ComLastMethodSuccess Of hoJwe2 To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoJwe2 To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "original text: "
Showln sOriginalPlaintext
// ---------------------------------------------------------------------------------
// It should also be possible to decrypt the JWE as shown in RFC 7516, Appendix A.2.7
// because it was produced using the same RSA key.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJwe
If (Not(IsComObjectCreated(hoSbJwe))) Begin
Send CreateComObject of hoSbJwe
End
Get ComAppend Of hoSbJwe "eyJhbGciOiJSU0ExXzUiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0." To iSuccess
Get ComAppend Of hoSbJwe "UGhIOguC7IuEvf_NPVaXsGMoLOmwvc1GyqlIKOK1nN94nHPoltGRhWhw7Zx0-kFm" To iSuccess
Get ComAppend Of hoSbJwe "1NJn8LE9XShH59_i8J0PH5ZZyNfGy2xGdULU7sHNF6Gp2vPLgNZ__deLKxGHZ7Pc" To iSuccess
Get ComAppend Of hoSbJwe "HALUzoOegEI-8E66jX2E4zyJKx-YxzZIItRzC5hlRirb6Y5Cl_p-ko3YvkkysZIF" To iSuccess
Get ComAppend Of hoSbJwe "NPccxRU7qve1WYPxqbb2Yw8kZqa2rMWI5ng8OtvzlV7elprCbuPhcCdZ6XDP0_F8" To iSuccess
Get ComAppend Of hoSbJwe "rkXds2vE4X-ncOIM8hAYHHi29NX0mcKiRaD0-D-ljQTP-cFPgwCp6X-nZZd9OHBv" To iSuccess
Get ComAppend Of hoSbJwe "-B3oWh2TbqmScqXMR4gp_A." To iSuccess
Get ComAppend Of hoSbJwe "AxY8DCtDaGlsbGljb3RoZQ." To iSuccess
Get ComAppend Of hoSbJwe "KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY." To iSuccess
Get ComAppend Of hoSbJwe "9hH0vgRfYgPnAHOd8stkvw" To iSuccess
Get pvComObject of hoSbJwe to vSbJwe
Get ComLoadJweSb Of hoJwe2 vSbJwe To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoJwe2 To sTemp1
Showln sTemp1
Procedure_Return
End
// Provide the RSA private key for decryption.
Get pvComObject of hoRsaPrivKey to vRsaPrivKey
Get ComSetPrivateKey Of hoJwe2 0 vRsaPrivKey To iSuccess
// Decrypt.
Get ComDecrypt Of hoJwe2 0 "utf-8" To sOriginalPlaintext
Get ComLastMethodSuccess Of hoJwe2 To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoJwe2 To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sOriginalPlaintext
End_Procedure