PowerBuilder
PowerBuilder
JWE using RSAES-OAEP and AES GCM
See more JSON Web Encryption (JWE) Examples
This example duplicates the example A.1 in RFC 7516 for JSON Web Encryption (JWE).Chilkat PowerBuilder Downloads
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
li_Success = 0
// 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 = "The true sign of intelligence is not knowledge but imagination."
// ---------------------------------
// A.1.1 JOSE Header
// First build the JWE Protected Header.
// We want to build this: {"alg":"RSA-OAEP","enc":"A256GCM"}
loo_JweProtHdr = create oleobject
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","RSA-OAEP")
loo_JweProtHdr.AppendString("enc","A256GCM")
Write-Debug "JWE Protected Header: " + loo_JweProtHdr.Emit()
Write-Debug "--"
// ---------------------------------
// A.1.2 Content Encryption Key
// Note: Chilkat automatically generates the random CEK internally.
// The application does not need to explicitly take this step.
// ---------------------------------
// A.1.3. Key Encryption
// The application should load an RSA private key from any format.
// However, the application does not need to explicitly construct the JWE Encrypted Key.
// Chilkat automatically does it internally.
// The design of the Chilkat JWE API is to allow the application to create the JWE
// after specifying the inputs. (This is in contrast to forcing the application developer
// to painstakingly go through each step of the JWE construction process.)
// The specific RSA key used in the A.1 example is the following JWK:
loo_SbJwk = create oleobject
li_rc = loo_SbJwk.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbJwk.Append("{~"kty~": ~"RSA~",")
loo_SbJwk.Append("~"n~": ~"oahUIoWw0K0usKNuOR6H4wkf4oBUXHTxRvgb48E-BVvxkeDNjbC4he8rUW")
loo_SbJwk.Append("cJoZmds2h7M70imEVhRU5djINXtqllXI4DFqcI1DgjT9LewND8MW2Krf3S")
loo_SbJwk.Append("psk_ZkoFnilakGygTwpZ3uesH-PFABNIUYpOiN15dsQRkgr0vEhxN92i2a")
loo_SbJwk.Append("sbOenSZeyaxziK72UwxrrKoExv6kc5twXTq4h-QChLOln0_mtUZwfsRaMS")
loo_SbJwk.Append("tPs6mS6XrgxnxbWhojf663tuEQueGC-FCMfra36C9knDFGzKsNa7LZK2dj")
loo_SbJwk.Append("YgyD3JR_MB_4NUJW_TqOQtwHYbxevoJArm-L5StowjzGy-_bq6Gw~",")
loo_SbJwk.Append("~"e~": ~"AQAB~",")
loo_SbJwk.Append("~"d~": ~"kLdtIj6GbDks_ApCSTYQtelcNttlKiOyPzMrXHeI-yk1F7-kpDxY4-WY5N")
loo_SbJwk.Append("WV5KntaEeXS1j82E375xxhWMHXyvjYecPT9fpwR_M9gV8n9Hrh2anTpTD9")
loo_SbJwk.Append("3Dt62ypW3yDsJzBnTnrYu1iwWRgBKrEYY46qAZIrA2xAwnm2X7uGR1hghk")
loo_SbJwk.Append("qDp0Vqj3kbSCz1XyfCs6_LehBwtxHIyh8Ripy40p24moOAbgxVw3rxT_vl")
loo_SbJwk.Append("t3UVe4WO3JkJOzlpUf-KTVI2Ptgm-dARxTEtE-id-4OJr0h-K-VFs3VSnd")
loo_SbJwk.Append("VTIznSxfyrj8ILL6MG_Uv8YAu7VILSB3lOW085-4qE3DzgrTjgyQ~",")
loo_SbJwk.Append("~"p~": ~"1r52Xk46c-LsfB5P442p7atdPUrxQSy4mti_tZI3Mgf2EuFVbUoDBvaRQ-")
loo_SbJwk.Append("SWxkbkmoEzL7JXroSBjSrK3YIQgYdMgyAEPTPjXv_hI2_1eTSPVZfzL0lf")
loo_SbJwk.Append("fNn03IXqWF5MDFuoUYE0hzb2vhrlN_rKrbfDIwUbTrjjgieRbwC6Cl0~",")
loo_SbJwk.Append("~"q~": ~"wLb35x7hmQWZsWJmB_vle87ihgZ19S8lBEROLIsZG4ayZVe9Hi9gDVCOBm")
loo_SbJwk.Append("UDdaDYVTSNx_8Fyw1YYa9XGrGnDew00J28cRUoeBB_jKI1oma0Orv1T9aX")
loo_SbJwk.Append("IWxKwd4gvxFImOWr3QRL9KEBRzk2RatUBnmDZJTIAfwTs0g68UZHvtc~",")
loo_SbJwk.Append("~"dp~": ~"ZK-YwE7diUh0qR1tR7w8WHtolDx3MZ_OTowiFvgfeQ3SiresXjm9gZ5KL")
loo_SbJwk.Append("hMXvo-uz-KUJWDxS5pFQ_M0evdo1dKiRTjVw_x4NyqyXPM5nULPkcpU827")
loo_SbJwk.Append("rnpZzAJKpdhWAgqrXGKAECQH0Xt4taznjnd_zVpAmZZq60WPMBMfKcuE~",")
loo_SbJwk.Append("~"dq~": ~"Dq0gfgJ1DdFGXiLvQEZnuKEN0UUmsJBxkjydc3j4ZYdBiMRAy86x0vHCj")
loo_SbJwk.Append("ywcMlYYg4yoC4YZa9hNVcsjqA3FeiL19rk8g6Qn29Tt0cj8qqyFpz9vNDB")
loo_SbJwk.Append("UfCAiJVeESOjJDZPYHdHY8v1b-o-Z2X5tvLx-TCekf7oxyeKDUqKWjis~",")
loo_SbJwk.Append("~"qi~": ~"VIMpMYbPf47dT1w_zDUXfPimsSegnMOA1zTaX7aGk_8urY6R8-ZW1FxU7")
loo_SbJwk.Append("AlWAyLWybqq6t16VFd7hQd0y6flUK4SlOydB61gwanOsXGOAOv82cHq0E3")
loo_SbJwk.Append("eL4HrtZkUuKvnPrMnsUUFlfUdybVzxyjz9JF_XyaY14ardLSjf4L_FNY~"}")
// Load this JWK into a Chilkat private key object.
loo_RsaPrivKey = create oleobject
li_rc = loo_RsaPrivKey.ConnectToNewObject("Chilkat.PrivateKey")
li_Success = loo_RsaPrivKey.LoadJwk(loo_SbJwk.GetAsString())
if li_Success = 0 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 = create oleobject
li_rc = loo_RsaPubKey.ConnectToNewObject("Chilkat.PublicKey")
loo_RsaPrivKey.ToPublicKey(loo_RsaPubKey)
// ---------------------------------
// A.1.4. Initialization Vector
// Chilkat automatically generates the necessary random IV internally.
// The application does not need to do this explicitly.
// ---------------------------------
// A.1.5. Additional Authenticated Data
// The Additional Authenticated Data encryption parameter is
// ASCII(BASE64URL(UTF8(JWE Protected Header))).
// Again, Chilkat automatically takes care of this internally.
// The application does not need to explicitly take this step.
// ---------------------------------
// A.1.6. Content Encryption
// Again... this step is handled by Chilkat internally.
// ---------------------------------
// A.1.7. Complete Representation
// The application need only call the Encrypt, EncryptSb, or EncryptBd method
// return the fully assembled JWE.
// The final representation in the Compact Serialization
// is the string BASE64URL(UTF8(JWE Protected Header)) || '.' ||
// BASE64URL(JWE Encrypted Key) || '.' || BASE64URL(JWE Initialization
// Vector) || '.' || BASE64URL(JWE Ciphertext) || '.' || BASE64URL(JWE
// Authentication Tag).
loo_Jwe = create oleobject
li_rc = loo_Jwe.ConnectToNewObject("Chilkat.Jwe")
loo_Jwe.SetProtectedHeader(loo_JweProtHdr)
loo_Jwe.SetPublicKey(0,loo_RsaPubKey)
ls_StrJwe = loo_Jwe.Encrypt(ls_Plaintext,"utf-8")
if loo_Jwe.LastMethodSuccess = 0 then
Write-Debug loo_Jwe.LastErrorText
destroy loo_JweProtHdr
destroy loo_SbJwk
destroy loo_RsaPrivKey
destroy loo_RsaPubKey
destroy loo_Jwe
return
end if
// Note: The RSA OAEP algorithm uses random padding bytes internally.
// Therefore, the results will appear different each time -- even if the
// identical plaintext is encrypted with the identical RSA key.
// (Do not expect the appearance of the results to be the same as what
// is published in the RFC. However, what is published in the RFC *should*
// be decryptable using the code that follows.)
Write-Debug ls_StrJwe
// 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
li_rc = loo_Jwe2.ConnectToNewObject("Chilkat.Jwe")
li_Success = loo_Jwe2.LoadJwe(ls_StrJwe)
if li_Success = 0 then
Write-Debug loo_Jwe2.LastErrorText
destroy loo_JweProtHdr
destroy loo_SbJwk
destroy loo_RsaPrivKey
destroy loo_RsaPubKey
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 = 0 then
Write-Debug loo_Jwe2.LastErrorText
destroy loo_JweProtHdr
destroy loo_SbJwk
destroy loo_RsaPrivKey
destroy loo_RsaPubKey
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.1.7
// because it was produced using the same RSA key.
loo_SbJwe = create oleobject
li_rc = loo_SbJwe.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbJwe.Append("eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkEyNTZHQ00ifQ.")
loo_SbJwe.Append("OKOawDo13gRp2ojaHV7LFpZcgV7T6DVZKTyKOMTYUmKoTCVJRgckCL9kiMT03JGe")
loo_SbJwe.Append("ipsEdY3mx_etLbbWSrFr05kLzcSr4qKAq7YN7e9jwQRb23nfa6c9d-StnImGyFDb")
loo_SbJwe.Append("Sv04uVuxIp5Zms1gNxKKK2Da14B8S4rzVRltdYwam_lDp5XnZAYpQdb76FdIKLaV")
loo_SbJwe.Append("mqgfwX7XWRxv2322i-vDxRfqNzo_tETKzpVLzfiwQyeyPGLBIO56YJ7eObdv0je8")
loo_SbJwe.Append("1860ppamavo35UgoRdbYaBcoh9QcfylQr66oc6vFWXRcZ_ZT2LawVCWTIy3brGPi")
loo_SbJwe.Append("6UklfCpIMfIjf7iGdXKHzg.")
loo_SbJwe.Append("48V1_ALb6US04U3b.")
loo_SbJwe.Append("5eym8TW_c8SuK0ltJ3rpYIzOeDQz7TALvtu6UG9oMo4vpzs9tX_EFShS8iB7j6ji")
loo_SbJwe.Append("SdiwkIr3ajwQzaBtQD_A.")
loo_SbJwe.Append("XFBoMYUZodetZdvTiFvSkQ")
li_Success = loo_Jwe2.LoadJweSb(loo_SbJwe)
if li_Success = 0 then
Write-Debug loo_Jwe2.LastErrorText
destroy loo_JweProtHdr
destroy loo_SbJwk
destroy loo_RsaPrivKey
destroy loo_RsaPubKey
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 = 0 then
Write-Debug loo_Jwe2.LastErrorText
destroy loo_JweProtHdr
destroy loo_SbJwk
destroy loo_RsaPrivKey
destroy loo_RsaPubKey
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_RsaPubKey
destroy loo_Jwe
destroy loo_Jwe2
destroy loo_SbJwe