Sample code for 30+ languages & platforms
Android™

RSA Sign using Base64 Private Key

See more RSA Examples

Signs a string using a non-encrypted RSA private key in base64 encoding. Returns the RSA signature as a base64 string.

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;

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkPrivateKey privKey = new CkPrivateKey();

    CkStringBuilder sbPem = new CkStringBuilder();
    sbPem.AppendLine("-----BEGIN RSA PRIVATE KEY-----",true);
    sbPem.AppendLine("MIIC .... j5A==",true);
    sbPem.AppendLine("-----END RSA PRIVATE KEY-----",true);

    success = privKey.LoadPem(sbPem.getAsString());
    if (success == false) {
        Log.i(TAG, privKey.lastErrorText());
        return;
        }

    CkRsa rsa = new CkRsa();

    success = rsa.UsePrivateKey(privKey);
    if (success == false) {
        Log.i(TAG, rsa.lastErrorText());
        return;
        }

    CkBinData bd = new CkBinData();
    bd.AppendString("12345678","utf-8");

    success = rsa.SignRawBd(bd);
    if (success == false) {
        Log.i(TAG, rsa.lastErrorText());
        return;
        }

    // Get the base64 RSA signature.
    Log.i(TAG, bd.getEncoded("base64"));

    success = rsa.VerifyRawBd(bd);
    if (success == false) {
        Log.i(TAG, rsa.lastErrorText());
        return;
        }

    String strOriginal = bd.getString("utf-8");
    Log.i(TAG, strOriginal);

  }

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