Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) JWE using ECDH-ES, BP-256, A256GCMSee more JSON Web Encryption (JWE) ExamplesCreate 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.
IncludeFile "CkJwt.pb" IncludeFile "CkJwe.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkPublicKey.pb" Procedure ChilkatExample() ; This requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. success.i ; Load our brainpool BP-256 public key. ; { ; "use": "enc", ; "kid": "puk_idp_enc", ; "kty": "EC", ; "crv": "BP-256", ; "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w", ; "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu" ; } json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(json,"use","enc") CkJsonObject::ckUpdateString(json,"kid","puk_idp_enc") CkJsonObject::ckUpdateString(json,"kty","EC") CkJsonObject::ckUpdateString(json,"crv","BP-256") CkJsonObject::ckUpdateString(json,"x","QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w") CkJsonObject::ckUpdateString(json,"y","AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu") pubkey.i = CkPublicKey::ckCreate() If pubkey.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkPublicKey::ckLoadFromString(pubkey,CkJsonObject::ckEmit(json)) If success = 0 Debug CkPublicKey::ckLastErrorText(pubkey) CkJsonObject::ckDispose(json) CkPublicKey::ckDispose(pubkey) ProcedureReturn 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. jwt.i = CkJwt::ckCreate() If jwt.i = 0 Debug "Failed to create object." ProcedureReturn EndIf jweProtHdr.i = CkJsonObject::ckCreate() If jweProtHdr.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(jweProtHdr,"alg","ECDH-ES") CkJsonObject::ckUpdateString(jweProtHdr,"enc","A256GCM") CkJsonObject::ckUpdateInt(jweProtHdr,"exp",CkJwt::ckGenNumericDate(jwt,3600)) CkJsonObject::ckUpdateString(jweProtHdr,"cty","NJWT") CkJsonObject::ckUpdateString(jweProtHdr,"epk.kty","EC") CkJsonObject::ckUpdateString(jweProtHdr,"epk.x","QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w") CkJsonObject::ckUpdateString(jweProtHdr,"epk.y","AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu") CkJsonObject::ckUpdateString(jweProtHdr,"epk.crv","BP-256") jwe.i = CkJwe::ckCreate() If jwe.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJwe::ckSetProtectedHeader(jwe,jweProtHdr) CkJwe::ckSetPublicKey(jwe,0,pubkey) plainText.s = "This is the text to be encrypted." strJwe.s = CkJwe::ckEncrypt(jwe,plainText,"utf-8") If CkJwe::ckLastMethodSuccess(jwe) <> 1 Debug CkJwe::ckLastErrorText(jwe) CkJsonObject::ckDispose(json) CkPublicKey::ckDispose(pubkey) CkJwt::ckDispose(jwt) CkJsonObject::ckDispose(jweProtHdr) CkJwe::ckDispose(jwe) ProcedureReturn EndIf Debug strJwe Debug "Success." CkJsonObject::ckDispose(json) CkPublicKey::ckDispose(pubkey) CkJwt::ckDispose(jwt) CkJsonObject::ckDispose(jweProtHdr) CkJwe::ckDispose(jwe) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.