Android™
Android™
Get a Private Key's Raw Hex Value
See more Private Key Examples
Demonstrates the Chilkat PrivateKey.GetRawHex method, which returns the raw private value as lowercase hexadecimal and replaces the contents of the supplied StringBuilder with the corresponding raw public key.
Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: This drops below the structured container formats to the bare key bytes as hex, which is meaningful chiefly for fixed-size keys such as EC and Ed25519 where a key is essentially a fixed byte string. It is the export to use when another system wants raw values rather than PEM or JWK — the counterpart to
LoadEd25519. Both the returned private hex and the public value are produced in one call.Chilkat Android™ Downloads
// 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;
// Load a private key to export.
CkPrivateKey privKey = new CkPrivateKey();
success = privKey.LoadPemFile("qa_data/private.pem");
if (success == false) {
Log.i(TAG, privKey.lastErrorText());
return;
}
// GetRawHex returns the raw private value as lowercase hex, and replaces the contents of the
// StringBuilder argument with the corresponding raw public key. This is most meaningful for
// fixed-size keys such as EC and Ed25519.
CkStringBuilder sbPublic = new CkStringBuilder();
String privHex = privKey.getRawHex(sbPublic);
if (privKey.get_LastMethodSuccess() == false) {
Log.i(TAG, privKey.lastErrorText());
return;
}
Log.i(TAG, "Raw private value: " + privHex);
Log.i(TAG, "Raw public key: " + sbPublic.getAsString());
}
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."
}
}