PowerBuilder
PowerBuilder
JWE using PBES2 Key Wrapping
See more JSON Web Encryption (JWE) Examples
Demonstrates how to create and decrypt a JWE that using PBES2 key wrapping.This example demonstrates PBES2 with HMAC SHA-256 and A128KW wrapping. It is also possible to do the following by simply changing the "alg" parameter:
- PBES2 with HMAC SHA-384 and A192KW wrapping
- PBES2 with HMAC SHA-512 and A256KW wrapping
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
oleobject loo_Prng
integer li_RecipientIndex
string ls_StrJwe
oleobject loo_Jwe2
string ls_OriginalPlaintext
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..
loo_JweProtHdr = create oleobject
li_rc = loo_JweProtHdr.ConnectToNewObject("Chilkat.JsonObject")
loo_JweProtHdr.AppendString("alg","PBES2-HS256+A128KW")
loo_JweProtHdr.AppendString("enc","A128GCM")
// PBES2 requires two additional parameters:
// 1) A random salt parameter ("p2s") containing 8 or more bytes in base64url format.
// 2) An iteration count parameter ("p2c"). A minimum count of 1000 is recommended.
// The iteration count is intended to make the PBES2 computation more expensive (time consuming)
// to prevent brute-force attacks.
loo_Prng = create oleobject
li_rc = loo_Prng.ConnectToNewObject("Chilkat.Prng")
loo_JweProtHdr.AppendString("p2s",loo_Prng.GenRandom(16,"base64url"))
loo_JweProtHdr.AppendString("p2c","1000")
Write-Debug "JWE Protected Header: " + loo_JweProtHdr.Emit()
Write-Debug "--"
// Don't forget to actually provide the protected header to the JWE object:
loo_Jwe.SetProtectedHeader(loo_JweProtHdr)
// Set the PBES2 password
li_RecipientIndex = 0
loo_Jwe.SetPassword(li_RecipientIndex,"top secret")
// 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
destroy loo_Prng
return
end if
// Show the JWE we just created:
Write-Debug ls_StrJwe
// Decrypt the JWE.
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_Prng
destroy loo_Jwe2
return
end if
// Set the PBES2 password
loo_Jwe2.SetPassword(li_RecipientIndex,"top secret")
// 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_Prng
destroy loo_Jwe2
return
end if
Write-Debug "original text: "
Write-Debug ls_OriginalPlaintext
// Sample output:
// JWE Protected Header: {"alg":"PBES2-HS256+A128KW","enc":"A128GCM","p2s":"z39rTEfRy1T1Yn_D1mZRlg","p2c":"1000"}
// --
// eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJlbmMiOiJBMTI4R0NNIiwicDJzIjoiejM5clRFZlJ5MVQxWW5fRDFtWlJsZyIsInAyYyI6IjEwMDAifQ.koYt6PrFmYwcwdcT7ZcvXHA1d-Xez5h4.luGlbvEnZp-7IsBOj42Yhw.YMTcfLf8Qe4zazozGV2OAu3cUdQ8Kg.rWub47ESWkc6IqZJTvSTmg
// original text:
// Live long and prosper.
destroy loo_Jwe
destroy loo_JweProtHdr
destroy loo_Prng
destroy loo_Jwe2