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
(PowerBuilder) JWE using RSAES-PKCS1-v1_5 and AES_128_CBC_HMAC_SHA_256This example duplicates the example A.2 in RFC 7516 for JSON Web Encryption (JWE). Note: This example requires Chilkat v9.5.0.66 or greater.
integer li_rc integer li_Success string ls_Plaintext oleobject loo_JweProtHdr oleobject loo_SbJwk oleobject loo_RsaPrivKey oleobject loo_RsaPubKey oleobject loo_Jwe string ls_StrJwe oleobject loo_Jwe2 string ls_OriginalPlaintext oleobject loo_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. ls_Plaintext = "Live long and prosper." // First build the JWE Protected Header. // We want to build this: {"alg":"RSA1_5","enc":"A128CBC-HS256"} loo_JweProtHdr = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JweProtHdr.ConnectToNewObject("Chilkat.JsonObject") if li_rc < 0 then destroy loo_JweProtHdr MessageBox("Error","Connecting to COM object failed") return end if loo_JweProtHdr.AppendString("alg","RSA1_5") loo_JweProtHdr.AppendString("enc","A128CBC-HS256") Write-Debug "JWE Protected Header: " + loo_JweProtHdr.Emit() Write-Debug "--" // The specific RSA key used in the A.2 example is the following JWK: loo_SbJwk = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbJwk.ConnectToNewObject("Chilkat.StringBuilder") loo_SbJwk.Append("{~"kty~":~"RSA~",") loo_SbJwk.Append("~"n~":~"sXchDaQebHnPiGvyDOAT4saGEUetSyo9MKLOoWFsueri23bOdgWp4Dy1Wl") loo_SbJwk.Append("UzewbgBHod5pcM9H95GQRV3JDXboIRROSBigeC5yjU1hGzHHyXss8UDpre") loo_SbJwk.Append("cbAYxknTcQkhslANGRUZmdTOQ5qTRsLAt6BTYuyvVRdhS8exSZEy_c4gs_") loo_SbJwk.Append("7svlJJQ4H9_NxsiIoLwAEk7-Q3UXERGYw_75IDrGA84-lA_-Ct4eTlXHBI") loo_SbJwk.Append("Y2EaV7t7LjJaynVJCpkv4LKjTTAumiGUIuQhrNhZLuF_RJLqHpM2kgWFLU") loo_SbJwk.Append("7-VTdL1VbC2tejvcI2BlMkEpk1BzBZI0KQB0GaDWFLN-aEAw3vRw~",") loo_SbJwk.Append("~"e~":~"AQAB~",") loo_SbJwk.Append("~"d~":~"VFCWOqXr8nvZNyaaJLXdnNPXZKRaWCjkU5Q2egQQpTBMwhprMzWzpR8Sxq") loo_SbJwk.Append("1OPThh_J6MUD8Z35wky9b8eEO0pwNS8xlh1lOFRRBoNqDIKVOku0aZb-ry") loo_SbJwk.Append("nq8cxjDTLZQ6Fz7jSjR1Klop-YKaUHc9GsEofQqYruPhzSA-QgajZGPbE_") loo_SbJwk.Append("0ZaVDJHfyd7UUBUKunFMScbflYAAOYJqVIVwaYR5zWEEceUjNnTNo_CVSj") loo_SbJwk.Append("-VvXLO5VZfCUAVLgW4dpf1SrtZjSt34YLsRarSb127reG_DUwg9Ch-Kyvj") loo_SbJwk.Append("T1SkHgUWRVGcyly7uvVGRSDwsXypdrNinPA4jlhoNdizK2zF2CWQ~",") loo_SbJwk.Append("~"p~":~"9gY2w6I6S6L0juEKsbeDAwpd9WMfgqFoeA9vEyEUuk4kLwBKcoe1x4HG68") loo_SbJwk.Append("ik918hdDSE9vDQSccA3xXHOAFOPJ8R9EeIAbTi1VwBYnbTp87X-xcPWlEP") loo_SbJwk.Append("krdoUKW60tgs1aNd_Nnc9LEVVPMS390zbFxt8TN_biaBgelNgbC95sM~",") loo_SbJwk.Append("~"q~":~"uKlCKvKv_ZJMVcdIs5vVSU_6cPtYI1ljWytExV_skstvRSNi9r66jdd9-y") loo_SbJwk.Append("BhVfuG4shsp2j7rGnIio901RBeHo6TPKWVVykPu1iYhQXw1jIABfw-MVsN") loo_SbJwk.Append("-3bQ76WLdt2SDxsHs7q7zPyUyHXmps7ycZ5c72wGkUwNOjYelmkiNS0~",") loo_SbJwk.Append("~"dp~":~"w0kZbV63cVRvVX6yk3C8cMxo2qCM4Y8nsq1lmMSYhG4EcL6FWbX5h9yuv") loo_SbJwk.Append("ngs4iLEFk6eALoUS4vIWEwcL4txw9LsWH_zKI-hwoReoP77cOdSL4AVcra") loo_SbJwk.Append("Hawlkpyd2TWjE5evgbhWtOxnZee3cXJBkAi64Ik6jZxbvk-RR3pEhnCs~",") loo_SbJwk.Append("~"dq~":~"o_8V14SezckO6CNLKs_btPdFiO9_kC1DsuUTd2LAfIIVeMZ7jn1Gus_Ff") loo_SbJwk.Append("7B7IVx3p5KuBGOVF8L-qifLb6nQnLysgHDh132NDioZkhH7mI7hPG-PYE_") loo_SbJwk.Append("odApKdnqECHWw0J-F0JWnUd6D2B_1TvF9mXA2Qx-iGYn8OVV1Bsmp6qU~",") loo_SbJwk.Append("~"qi~":~"eNho5yRBEBxhGBtQRww9QirZsB66TrfFReG_CcteI1aCneT0ELGhYlRlC") loo_SbJwk.Append("tUkTRclIfuEPmNsNDPbLoLqqCVznFbvdB7x-Tl-m0l_eFTj2KiqwGqE9PZ") loo_SbJwk.Append("B9nNTwMVvH3VRRSLWACvPnSiwP8N5Usy-WRXS-V7TbpxIhvepTfE0NNo~"") loo_SbJwk.Append("}") // Load this JWK into a Chilkat private key object. loo_RsaPrivKey = create oleobject // Use "Chilkat_9_5_0.PrivateKey" for versions of Chilkat < 10.0.0 li_rc = loo_RsaPrivKey.ConnectToNewObject("Chilkat.PrivateKey") li_Success = loo_RsaPrivKey.LoadJwk(loo_SbJwk.GetAsString()) if li_Success <> 1 then Write-Debug loo_RsaPrivKey.LastErrorText destroy loo_JweProtHdr destroy loo_SbJwk destroy loo_RsaPrivKey return end if // 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 loo_RsaPubKey = loo_RsaPrivKey.GetPublicKey() // Create the JWE... loo_Jwe = create oleobject // Use "Chilkat_9_5_0.Jwe" for versions of Chilkat < 10.0.0 li_rc = loo_Jwe.ConnectToNewObject("Chilkat.Jwe") loo_Jwe.SetProtectedHeader(loo_JweProtHdr) loo_Jwe.SetPublicKey(0,loo_RsaPubKey) destroy loo_RsaPubKey ls_StrJwe = loo_Jwe.Encrypt(ls_Plaintext,"utf-8") if loo_Jwe.LastMethodSuccess <> 1 then Write-Debug loo_Jwe.LastErrorText destroy loo_JweProtHdr destroy loo_SbJwk destroy loo_RsaPrivKey destroy loo_Jwe return end if // Show the JWE we just created: Write-Debug ls_StrJwe // 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. loo_Jwe2 = create oleobject // Use "Chilkat_9_5_0.Jwe" for versions of Chilkat < 10.0.0 li_rc = loo_Jwe2.ConnectToNewObject("Chilkat.Jwe") li_Success = loo_Jwe2.LoadJwe(ls_StrJwe) if li_Success <> 1 then Write-Debug loo_Jwe2.LastErrorText destroy loo_JweProtHdr destroy loo_SbJwk destroy loo_RsaPrivKey destroy loo_Jwe destroy loo_Jwe2 return end if // Provide the RSA private key for decryption. // (The JWE was encrypted for a single recipient at index 0.) loo_Jwe2.SetPrivateKey(0,loo_RsaPrivKey) // Decrypt. ls_OriginalPlaintext = loo_Jwe2.Decrypt(0,"utf-8") if loo_Jwe2.LastMethodSuccess <> 1 then Write-Debug loo_Jwe2.LastErrorText destroy loo_JweProtHdr destroy loo_SbJwk destroy loo_RsaPrivKey destroy loo_Jwe destroy loo_Jwe2 return end if Write-Debug "original text: " Write-Debug ls_OriginalPlaintext // --------------------------------------------------------------------------------- // 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. loo_SbJwe = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbJwe.ConnectToNewObject("Chilkat.StringBuilder") loo_SbJwe.Append("eyJhbGciOiJSU0ExXzUiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.") loo_SbJwe.Append("UGhIOguC7IuEvf_NPVaXsGMoLOmwvc1GyqlIKOK1nN94nHPoltGRhWhw7Zx0-kFm") loo_SbJwe.Append("1NJn8LE9XShH59_i8J0PH5ZZyNfGy2xGdULU7sHNF6Gp2vPLgNZ__deLKxGHZ7Pc") loo_SbJwe.Append("HALUzoOegEI-8E66jX2E4zyJKx-YxzZIItRzC5hlRirb6Y5Cl_p-ko3YvkkysZIF") loo_SbJwe.Append("NPccxRU7qve1WYPxqbb2Yw8kZqa2rMWI5ng8OtvzlV7elprCbuPhcCdZ6XDP0_F8") loo_SbJwe.Append("rkXds2vE4X-ncOIM8hAYHHi29NX0mcKiRaD0-D-ljQTP-cFPgwCp6X-nZZd9OHBv") loo_SbJwe.Append("-B3oWh2TbqmScqXMR4gp_A.") loo_SbJwe.Append("AxY8DCtDaGlsbGljb3RoZQ.") loo_SbJwe.Append("KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY.") loo_SbJwe.Append("9hH0vgRfYgPnAHOd8stkvw") li_Success = loo_Jwe2.LoadJweSb(loo_SbJwe) if li_Success <> 1 then Write-Debug loo_Jwe2.LastErrorText destroy loo_JweProtHdr destroy loo_SbJwk destroy loo_RsaPrivKey destroy loo_Jwe destroy loo_Jwe2 destroy loo_SbJwe return end if // Provide the RSA private key for decryption. loo_Jwe2.SetPrivateKey(0,loo_RsaPrivKey) // Decrypt. ls_OriginalPlaintext = loo_Jwe2.Decrypt(0,"utf-8") if loo_Jwe2.LastMethodSuccess <> 1 then Write-Debug loo_Jwe2.LastErrorText destroy loo_JweProtHdr destroy loo_SbJwk destroy loo_RsaPrivKey destroy loo_Jwe destroy loo_Jwe2 destroy loo_SbJwe return end if Write-Debug ls_OriginalPlaintext destroy loo_JweProtHdr destroy loo_SbJwk destroy loo_RsaPrivKey destroy loo_Jwe destroy loo_Jwe2 destroy loo_SbJwe |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.