Xbase++
Xbase++
JWE using ECDH-ES, BP-256, A256GCM
See more JSON Web Encryption (JWE) Examples
Create a JWE with the following header:
{
"alg": "ECDH-ES",
"enc": "A256GCM",
"exp": 1621957030,
"cty": "NJWT",
"epk": {
"kty": "EC",
"x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"
"y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
"crv": "BP-256"
}
}
Note: This example requires Chilkat v9.5.0.87 or greater.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oJson
LOCAL oPubkey
LOCAL oJwt
LOCAL oJweProtHdr
LOCAL oJwe
LOCAL cPlainText
LOCAL cStrJwe
nSuccess := 0
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load our brainpool BP-256 public key.
// {
// "use": "enc",
// "kid": "puk_idp_enc",
// "kty": "EC",
// "crv": "BP-256",
// "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w",
// "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
// }
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("use", "enc")
oJson:UpdateString("kid", "puk_idp_enc")
oJson:UpdateString("kty", "EC")
oJson:UpdateString("crv", "BP-256")
oJson:UpdateString("x", "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w")
oJson:UpdateString("y", "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu")
oPubkey := CreateObject("Chilkat.PublicKey")
nSuccess := oPubkey:LoadFromString(oJson:Emit())
IF (nSuccess == 0)
? oPubkey:LastErrorText
oJson:destroy()
oPubkey:destroy()
RETURN
ENDIF
// Build our protected header:
// {
// "alg": "ECDH-ES",
// "enc": "A256GCM",
// "exp": 1621957030,
// "cty": "NJWT",
// "epk": {
// "kty": "EC",
// "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"
// "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
// "crv": "BP-256"
// }
// }
// Use jwt only for getting the current date/time + 3600 seconds.
oJwt := CreateObject("Chilkat.Jwt")
oJweProtHdr := CreateObject("Chilkat.JsonObject")
oJweProtHdr:UpdateString("alg", "ECDH-ES")
oJweProtHdr:UpdateString("enc", "A256GCM")
oJweProtHdr:UpdateInt("exp", oJwt:GenNumericDate(3600))
oJweProtHdr:UpdateString("cty", "NJWT")
oJweProtHdr:UpdateString("epk.kty", "EC")
oJweProtHdr:UpdateString("epk.x", "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w")
oJweProtHdr:UpdateString("epk.y", "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu")
oJweProtHdr:UpdateString("epk.crv", "BP-256")
oJwe := CreateObject("Chilkat.Jwe")
oJwe:SetProtectedHeader(oJweProtHdr)
oJwe:SetPublicKey(0, oPubkey)
cPlainText := "This is the text to be encrypted."
cStrJwe := oJwe:Encrypt(cPlainText, "utf-8")
IF (oJwe:LastMethodSuccess != 1)
? oJwe:LastErrorText
oJson:destroy()
oPubkey:destroy()
oJwt:destroy()
oJweProtHdr:destroy()
oJwe:destroy()
RETURN
ENDIF
? cStrJwe
? "Success."
oJson:destroy()
oPubkey:destroy()
oJwt:destroy()
oJweProtHdr:destroy()
oJwe:destroy()