PowerBuilder
PowerBuilder
JWE using AES Key Wrap and AES_128_CBC_HMAC_SHA_256
See more JSON Web Encryption (JWE) Examples
This example duplicates the example A.3 in RFC 7516 for JSON Web Encryption (JWE).Note: This example requires Chilkat v9.5.0.66 or greater.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
string ls_Plaintext
oleobject loo_Jwe
oleobject loo_JweProtHdr
string ls_AesWrappingKey
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 = "Live long and prosper."
loo_Jwe = create oleobject
li_rc = loo_Jwe.ConnectToNewObject("Chilkat.Jwe")
if li_rc < 0 then
destroy loo_Jwe
MessageBox("Error","Connecting to COM object failed")
return
end if
// First build the JWE Protected Header: {"alg":"A128KW","enc":"A128CBC-HS256"}
loo_JweProtHdr = create oleobject
li_rc = loo_JweProtHdr.ConnectToNewObject("Chilkat.JsonObject")
loo_JweProtHdr.AppendString("alg","A128KW")
loo_JweProtHdr.AppendString("enc","A128CBC-HS256")
loo_Jwe.SetProtectedHeader(loo_JweProtHdr)
Write-Debug "JWE Protected Header: " + loo_JweProtHdr.Emit()
Write-Debug "--"
// The example A.3 in RFC 7516 uses the following 128-bit AES key,
// specified in JWK (JSON Web Key) format:
// {"kty":"oct",
// "k":"GawgguFyGrWKav7AX4VKUg"
// }
// This is just a way of saying: The key type ("kty") is
// a bunch of octets ("k") in base64url encoding.
// We can simply set the AES wrapping key like this:
ls_AesWrappingKey = "GawgguFyGrWKav7AX4VKUg"
loo_Jwe.SetWrappingKey(0,ls_AesWrappingKey,"base64url")
// Encrypt and return the JWE:
ls_StrJwe = loo_Jwe.Encrypt(ls_Plaintext,"utf-8")
if loo_Jwe.LastMethodSuccess <> 1 then
Write-Debug loo_Jwe.LastErrorText
destroy loo_Jwe
destroy loo_JweProtHdr
return
end if
// Show the JWE we just created:
Write-Debug ls_StrJwe
// Decrypt the JWE that was just produced.
// 1) Load the JWE.
// 2) Set the AES wrapping key.
// 3) Decrypt.
loo_Jwe2 = create oleobject
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_Jwe
destroy loo_JweProtHdr
destroy loo_Jwe2
return
end if
// Set the AES wrap key.
loo_Jwe2.SetWrappingKey(0,ls_AesWrappingKey,"base64url")
// Decrypt.
ls_OriginalPlaintext = loo_Jwe2.Decrypt(0,"utf-8")
if loo_Jwe2.LastMethodSuccess <> 1 then
Write-Debug loo_Jwe2.LastErrorText
destroy loo_Jwe
destroy loo_JweProtHdr
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.3.7
// because it was produced using the same AES Wrap key.
loo_SbJwe = create oleobject
li_rc = loo_SbJwe.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbJwe.Append("eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.")
loo_SbJwe.Append("6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ.")
loo_SbJwe.Append("AxY8DCtDaGlsbGljb3RoZQ.")
loo_SbJwe.Append("KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY.")
loo_SbJwe.Append("U0m_YmjN04DJvceFICbCVQ")
li_Success = loo_Jwe2.LoadJweSb(loo_SbJwe)
if li_Success <> 1 then
Write-Debug loo_Jwe2.LastErrorText
destroy loo_Jwe
destroy loo_JweProtHdr
destroy loo_Jwe2
destroy loo_SbJwe
return
end if
loo_Jwe2.SetWrappingKey(0,ls_AesWrappingKey,"base64url")
// Decrypt.
ls_OriginalPlaintext = loo_Jwe2.Decrypt(0,"utf-8")
if loo_Jwe2.LastMethodSuccess <> 1 then
Write-Debug loo_Jwe2.LastErrorText
destroy loo_Jwe
destroy loo_JweProtHdr
destroy loo_Jwe2
destroy loo_SbJwe
return
end if
Write-Debug ls_OriginalPlaintext
destroy loo_Jwe
destroy loo_JweProtHdr
destroy loo_Jwe2
destroy loo_SbJwe