Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loJwe
LOCAL loHeader
LOCAL loPubKey
LOCAL lcJweCompact

lnSuccess = 0

loJwe = CreateObject('Chilkat.Jwe')

*  Protected header: RSA-OAEP-256 key management with AES-256-GCM content encryption.
loHeader = CreateObject('Chilkat.JsonObject')
loHeader.UpdateString("alg","RSA-OAEP-256")
loHeader.UpdateString("enc","A256GCM")
lnSuccess = loJwe.SetProtectedHeader(loHeader)
IF (lnSuccess = 0) THEN
    ? loJwe.LastErrorText
    RELEASE loJwe
    RELEASE loHeader
    CANCEL
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.
loPubKey = CreateObject('Chilkat.PublicKey')
lnSuccess = loPubKey.LoadFromFile("qa_data/recipient_pubkey.pem")
IF (lnSuccess = 0) THEN
    ? loPubKey.LastErrorText
    RELEASE loJwe
    RELEASE loHeader
    RELEASE loPubKey
    CANCEL
ENDIF

*  Set the public key used to encrypt for recipient 0.
lnSuccess = loJwe.SetPublicKey(0,loPubKey)
IF (lnSuccess = 0) THEN
    ? loJwe.LastErrorText
    RELEASE loJwe
    RELEASE loHeader
    RELEASE loPubKey
    CANCEL
ENDIF

lcJweCompact = loJwe.Encrypt("This is the secret content.","utf-8")
IF (loJwe.LastMethodSuccess = 0) THEN
    ? loJwe.LastErrorText
    RELEASE loJwe
    RELEASE loHeader
    RELEASE loPubKey
    CANCEL
ENDIF

? lcJweCompact

RELEASE loJwe
RELEASE loHeader
RELEASE loPubKey