Sample code for 30+ languages & platforms
Android™

Load ECC Public Key from JWK Format (JSON Web Key)

See more ECC Examples

Demonstrates how to load an ECC public key from JWK (JSON Web Key) format.

Note: This example requires Chilkat v9.5.0.66 or later.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    // Note: This example requires Chilkat v9.5.0.66 or later.

    // First build a JWK sample to load..
    CkJsonObject json = new CkJsonObject();
    json.UpdateString("kty","EC");
    json.UpdateString("crv","P-256");
    json.UpdateString("x","oBUyo8CQAFPeYPvv78ylh5MwFZjTCLQeb042TjiMJxE");
    json.UpdateString("y","vvQyxZkUjJQUPU_0bCy3Pj5qQdfu8jwEfqEeYGZ95CU");

    // The JSON contains the following:
    // { 
    //   "kty": "EC",
    //   "crv": "P-256",
    //   "x": "oBUyo8CQAFPeYPvv78ylh5MwFZjTCLQeb042TjiMJxE",
    //   "y": "vvQyxZkUjJQUPU_0bCy3Pj5qQdfu8jwEfqEeYGZ95CU",
    // }

    // Note: The JSON can contain other members, such as "use", "kid", or anything else.  These will be ignored.
    json.put_EmitCompact(false);

    // Show the JWK string to be loaded:
    String jwkStr = json.emit();

    CkPublicKey pubKey = new CkPublicKey();
    // The LoadFromString method will automatically detect the format.
    success = pubKey.LoadFromString(jwkStr);
    if (success != true) {
        Log.i(TAG, pubKey.lastErrorText());
        return;
        }

    // OK.. the JWK is loaded.  It can be used in whatever way desired...

    // The key can be retrieved in any other format, such as XML or PEM..
    Log.i(TAG, pubKey.getXml());

    // XML output:
    // <ECCPublicKey curve="secp256r1" 
    //    x="A01532A3C0900053DE60FBEFEFCCA58793301598D308B41E6F4E364E388C2711" 
    //    y="BEF432C599148C94143D4FF46C2CB73E3E6A41D7EEF23C047EA11E60667DE425">
    //    MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABKAVMqPAkABT3mD77+/MpYeTMBWY0wi0Hm9ONk44jCcRvvQyxZkUjJQUPU/0bCy3Pj5qQdfu8jwEfqEeYGZ95CU=
    // </ECCPublicKey>

    // Choose PCKS1 or PCKS8 PEM format..
    boolean bPreferPkcs1 = false;
    Log.i(TAG, pubKey.getPem(bPreferPkcs1));

    // PEM output
    // -----BEGIN PUBLIC KEY-----
    // MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
    // AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
    // ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
    // NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
    // RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
    // //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABKAVMqPAkABT3mD77+/MpYeT
    // MBWY0wi0Hm9ONk44jCcRvvQyxZkUjJQUPU/0bCy3Pj5qQdfu8jwEfqEeYGZ95CU=
    // -----END PUBLIC KEY-----

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}