Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oJwe
LOCAL oHeader
LOCAL oPubKey
LOCAL cJweCompact
nSuccess := 0
oJwe := CreateObject("Chilkat.Jwe")
// Protected header: RSA-OAEP-256 key management with AES-256-GCM content encryption.
oHeader := CreateObject("Chilkat.JsonObject")
oHeader:UpdateString("alg", "RSA-OAEP-256")
oHeader:UpdateString("enc", "A256GCM")
nSuccess := oJwe:SetProtectedHeader(oHeader)
IF (nSuccess == 0)
? oJwe:LastErrorText
oJwe:destroy()
oHeader:destroy()
RETURN
ENDIF
// 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.
oPubKey := CreateObject("Chilkat.PublicKey")
nSuccess := oPubKey:LoadFromFile("qa_data/recipient_pubkey.pem")
IF (nSuccess == 0)
? oPubKey:LastErrorText
oJwe:destroy()
oHeader:destroy()
oPubKey:destroy()
RETURN
ENDIF
// Set the public key used to encrypt for recipient 0.
nSuccess := oJwe:SetPublicKey(0, oPubKey)
IF (nSuccess == 0)
? oJwe:LastErrorText
oJwe:destroy()
oHeader:destroy()
oPubKey:destroy()
RETURN
ENDIF
cJweCompact := oJwe:Encrypt("This is the secret content.", "utf-8")
IF (oJwe:LastMethodSuccess == 0)
? oJwe:LastErrorText
oJwe:destroy()
oHeader:destroy()
oPubKey:destroy()
RETURN
ENDIF
? cJweCompact
oJwe:destroy()
oHeader:destroy()
oPubKey:destroy()