Sample code for 30+ languages & platforms
Visual FoxPro

Encrypt a JWE with a Password (PBES2)

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetPassword, which sets the PBES2 password for a recipient. The example uses PBES2-HS256+A128KW key management with AES-128-GCM content encryption.

Background. PBES2 derives a key-wrapping key from a password using a salt and iteration count, allowing password-based JWE encryption. The password should come from a secure source.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loJwe
LOCAL loHeader
LOCAL lcPassword
LOCAL lcJweCompact

lnSuccess = 0

loJwe = CreateObject('Chilkat.Jwe')

*  Protected header: PBES2 password-based key management with AES-128-GCM content encryption.
loHeader = CreateObject('Chilkat.JsonObject')
loHeader.UpdateString("alg","PBES2-HS256+A128KW")
loHeader.UpdateString("enc","A128GCM")
lnSuccess = loJwe.SetProtectedHeader(loHeader)
IF (lnSuccess = 0) THEN
    ? loJwe.LastErrorText
    RELEASE loJwe
    RELEASE loHeader
    CANCEL
ENDIF

*  Set the PBES2 password for recipient 0.  The password should come from a secure source rather than
*  being hard-coded.
lcPassword = "myPassword"
lnSuccess = loJwe.SetPassword(0,lcPassword)
IF (lnSuccess = 0) THEN
    ? loJwe.LastErrorText
    RELEASE loJwe
    RELEASE loHeader
    CANCEL
ENDIF

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

? lcJweCompact

RELEASE loJwe
RELEASE loHeader