DataFlex
DataFlex
Encrypt Content to a JWE
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.Encrypt, which encrypts a string and returns the resulting JWE. The charset argument converts the text to bytes before encryption.
Background. JWE (JSON Web Encryption) protects content with a content-encryption algorithm (the header
enc value) while the content-encryption key is managed by the key-management algorithm (the header alg value). This example uses AES key wrapping with a symmetric wrapping key.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoJwe
Variant vHeader
Handle hoHeader
String sBase64Key
String sJweCompact
String sTemp1
Boolean bTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatJwe)) To hoJwe
If (Not(IsComObjectCreated(hoJwe))) Begin
Send CreateComObject of hoJwe
End
// Set the JWE protected header, specifying the key-management algorithm (alg) and the content-
// encryption algorithm (enc).
Get Create (RefClass(cComChilkatJsonObject)) To hoHeader
If (Not(IsComObjectCreated(hoHeader))) Begin
Send CreateComObject of hoHeader
End
Get ComUpdateString Of hoHeader "alg" "A256KW" To iSuccess
Get ComUpdateString Of hoHeader "enc" "A256GCM" To iSuccess
Get pvComObject of hoHeader to vHeader
Get ComSetProtectedHeader Of hoJwe vHeader To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoJwe To sTemp1
Showln sTemp1
Procedure_Return
End
// The 256-bit key-wrapping key (base64) for recipient index 0. In production, obtain the key
// from a secure source rather than hard-coding it.
Move "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=" To sBase64Key
Get ComSetWrappingKey Of hoJwe 0 sBase64Key "base64" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoJwe To sTemp1
Showln sTemp1
Procedure_Return
End
// Encrypt the content. The 2nd argument is the charset used to convert the text to bytes. The
// returned string is the JWE (compact serialization by default).
Get ComEncrypt Of hoJwe "This is the secret content." "utf-8" To sJweCompact
Get ComLastMethodSuccess Of hoJwe To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoJwe To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sJweCompact
End_Procedure