Sample code for 30+ languages & platforms
PowerBuilder

Encrypt a JWE with a Recipient Public Key

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetPublicKey, which sets the public key used to encrypt for a recipient. The example uses RSA-OAEP-256 key management with AES-256-GCM content encryption.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. In public-key JWE, the content-encryption key is wrapped for the recipient using their public key, so only the holder of the matching private key can decrypt.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jwe
oleobject loo_Header
oleobject loo_PubKey
string ls_JweCompact

li_Success = 0

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

//  Protected header: RSA-OAEP-256 key management with AES-256-GCM content encryption.
loo_Header = create oleobject
li_rc = loo_Header.ConnectToNewObject("Chilkat.JsonObject")

loo_Header.UpdateString("alg","RSA-OAEP-256")
loo_Header.UpdateString("enc","A256GCM")
li_Success = loo_Jwe.SetProtectedHeader(loo_Header)
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_Header
    return
end if

//  The file path is relative to the application's current working directory.  An absolute path
//  may also be used.  Supply the path appropriate to your own environment.
//  Load the recipient's public key.
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")

li_Success = loo_PubKey.LoadFromFile("qa_data/recipient_pubkey.pem")
if li_Success = 0 then
    Write-Debug loo_PubKey.LastErrorText
    destroy loo_Jwe
    destroy loo_Header
    destroy loo_PubKey
    return
end if

//  Set the public key used to encrypt for recipient 0.
li_Success = loo_Jwe.SetPublicKey(0,loo_PubKey)
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_Header
    destroy loo_PubKey
    return
end if

ls_JweCompact = loo_Jwe.Encrypt("This is the secret content.","utf-8")
if loo_Jwe.LastMethodSuccess = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_Header
    destroy loo_PubKey
    return
end if

Write-Debug ls_JweCompact


destroy loo_Jwe
destroy loo_Header
destroy loo_PubKey