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
(Objective-C) 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.
#import <CkoJsonObject.h> #import <CkoPublicKey.h> #import <CkoJwt.h> #import <CkoJwe.h> #import <NSString.h> // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. BOOL success; // Load our brainpool BP-256 public key. // { // "use": "enc", // "kid": "puk_idp_enc", // "kty": "EC", // "crv": "BP-256", // "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w", // "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu" // } CkoJsonObject *json = [[CkoJsonObject alloc] init]; [json UpdateString: @"use" value: @"enc"]; [json UpdateString: @"kid" value: @"puk_idp_enc"]; [json UpdateString: @"kty" value: @"EC"]; [json UpdateString: @"crv" value: @"BP-256"]; [json UpdateString: @"x" value: @"QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"]; [json UpdateString: @"y" value: @"AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"]; CkoPublicKey *pubkey = [[CkoPublicKey alloc] init]; success = [pubkey LoadFromString: [json Emit]]; if (success == NO) { NSLog(@"%@",pubkey.LastErrorText); return; } // 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. CkoJwt *jwt = [[CkoJwt alloc] init]; CkoJsonObject *jweProtHdr = [[CkoJsonObject alloc] init]; [jweProtHdr UpdateString: @"alg" value: @"ECDH-ES"]; [jweProtHdr UpdateString: @"enc" value: @"A256GCM"]; [jweProtHdr UpdateInt: @"exp" value: [jwt GenNumericDate: [NSNumber numberWithInt: 3600]]]; [jweProtHdr UpdateString: @"cty" value: @"NJWT"]; [jweProtHdr UpdateString: @"epk.kty" value: @"EC"]; [jweProtHdr UpdateString: @"epk.x" value: @"QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"]; [jweProtHdr UpdateString: @"epk.y" value: @"AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"]; [jweProtHdr UpdateString: @"epk.crv" value: @"BP-256"]; CkoJwe *jwe = [[CkoJwe alloc] init]; [jwe SetProtectedHeader: jweProtHdr]; [jwe SetPublicKey: [NSNumber numberWithInt: 0] pubKey: pubkey]; NSString *plainText = @"This is the text to be encrypted."; NSString *strJwe = [jwe Encrypt: plainText charset: @"utf-8"]; if (jwe.LastMethodSuccess != YES) { NSLog(@"%@",jwe.LastErrorText); return; } NSLog(@"%@",strJwe); NSLog(@"%@",@"Success."); |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.