DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoJson
Variant vPubkey
Handle hoPubkey
Handle hoJwt
Variant vJweProtHdr
Handle hoJweProtHdr
Handle hoJwe
String sPlainText
String sStrJwe
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// 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"
// }
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComUpdateString Of hoJson "use" "enc" To iSuccess
Get ComUpdateString Of hoJson "kid" "puk_idp_enc" To iSuccess
Get ComUpdateString Of hoJson "kty" "EC" To iSuccess
Get ComUpdateString Of hoJson "crv" "BP-256" To iSuccess
Get ComUpdateString Of hoJson "x" "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w" To iSuccess
Get ComUpdateString Of hoJson "y" "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu" To iSuccess
Get Create (RefClass(cComChilkatPublicKey)) To hoPubkey
If (Not(IsComObjectCreated(hoPubkey))) Begin
Send CreateComObject of hoPubkey
End
Get ComEmit Of hoJson To sTemp1
Get ComLoadFromString Of hoPubkey sTemp1 To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPubkey To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Get Create (RefClass(cComChilkatJwt)) To hoJwt
If (Not(IsComObjectCreated(hoJwt))) Begin
Send CreateComObject of hoJwt
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJweProtHdr
If (Not(IsComObjectCreated(hoJweProtHdr))) Begin
Send CreateComObject of hoJweProtHdr
End
Get ComUpdateString Of hoJweProtHdr "alg" "ECDH-ES" To iSuccess
Get ComUpdateString Of hoJweProtHdr "enc" "A256GCM" To iSuccess
Get ComGenNumericDate Of hoJwt 3600 To iTemp1
Get ComUpdateInt Of hoJweProtHdr "exp" iTemp1 To iSuccess
Get ComUpdateString Of hoJweProtHdr "cty" "NJWT" To iSuccess
Get ComUpdateString Of hoJweProtHdr "epk.kty" "EC" To iSuccess
Get ComUpdateString Of hoJweProtHdr "epk.x" "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w" To iSuccess
Get ComUpdateString Of hoJweProtHdr "epk.y" "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu" To iSuccess
Get ComUpdateString Of hoJweProtHdr "epk.crv" "BP-256" To iSuccess
Get Create (RefClass(cComChilkatJwe)) To hoJwe
If (Not(IsComObjectCreated(hoJwe))) Begin
Send CreateComObject of hoJwe
End
Get pvComObject of hoJweProtHdr to vJweProtHdr
Get ComSetProtectedHeader Of hoJwe vJweProtHdr To iSuccess
Get pvComObject of hoPubkey to vPubkey
Get ComSetPublicKey Of hoJwe 0 vPubkey To iSuccess
Move "This is the text to be encrypted." To sPlainText
Get ComEncrypt Of hoJwe sPlainText "utf-8" To sStrJwe
Get ComLastMethodSuccess Of hoJwe To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoJwe To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sStrJwe
Showln "Success."
End_Procedure