Sample code for 30+ languages & platforms
Swift

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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // 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"
    // 	}

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "use", value: "enc")
    json.updateString(jsonPath: "kid", value: "puk_idp_enc")
    json.updateString(jsonPath: "kty", value: "EC")
    json.updateString(jsonPath: "crv", value: "BP-256")
    json.updateString(jsonPath: "x", value: "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w")
    json.updateString(jsonPath: "y", value: "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu")

    let pubkey = CkoPublicKey()!

    success = pubkey.load(fromString: json.emit())
    if success == false {
        print("\(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.
    let jwt = CkoJwt()!

    let jweProtHdr = CkoJsonObject()!
    jweProtHdr.updateString(jsonPath: "alg", value: "ECDH-ES")
    jweProtHdr.updateString(jsonPath: "enc", value: "A256GCM")
    jweProtHdr.updateInt(jsonPath: "exp", value: jwt.genNumericDate(numSecOffset: 3600).intValue)
    jweProtHdr.updateString(jsonPath: "cty", value: "NJWT")
    jweProtHdr.updateString(jsonPath: "epk.kty", value: "EC")
    jweProtHdr.updateString(jsonPath: "epk.x", value: "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w")
    jweProtHdr.updateString(jsonPath: "epk.y", value: "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu")
    jweProtHdr.updateString(jsonPath: "epk.crv", value: "BP-256")

    let jwe = CkoJwe()!
    jwe.setProtectedHeader(json: jweProtHdr)
    jwe.setPublicKey(index: 0, pubKey: pubkey)

    var plainText: String? = "This is the text to be encrypted."
    var strJwe: String? = jwe.encrypt(content: plainText, charset: "utf-8")
    if jwe.lastMethodSuccess != true {
        print("\(jwe.lastErrorText!)")
        return
    }

    print("\(strJwe!)")

    print("Success.")

}