|
|
(Chilkat for Android™ API) Create PKCS1 RSA Signature with PEM Private Key
Demonstrates how to load a private key from a PEM file and create a PKCS1 RSA digital signature.
Download: Chilkat for Android™ Java Libraries
// 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 {
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
String outStr = "";
CkPrivateKey pkey = new CkPrivateKey();
// Load the private key from an RSA PEM file:
pkey.LoadPemFile("pvkey2_rsa.pem");
boolean success;
String pkeyXml;
// Get the private key in XML format:
pkeyXml = pkey.getXml();
CkRsa rsa = new CkRsa();
// Any string argument automatically begins the 30-day trial.
success = rsa.UnlockComponent("30-day trial");
if (success != true) {
outStr += "RSA component unlock failed" + "\n";
tv.setText(outStr);
setContentView(tv);
return;
}
// Import the private key into the RSA component:
success = rsa.ImportPrivateKey(pkeyXml);
if (success != true) {
outStr += rsa.lastErrorText() + "\n";
tv.setText(outStr);
setContentView(tv);
return;
}
// This example will sign a string, and receive the signature
// in a hex-encoded string. Therefore, set the encoding mode
// to "hex":
rsa.put_EncodingMode("hex");
// If some other non-Chilkat software is going to verify
// the signature, it is important to match the byte-ordering.
// The LittleEndian property may be set to true
// for little-endian byte ordering,
// or false for big-endian byte ordering.
// Microsoft apps typically use little-endian, while
// OpenSSL and other services (such as Amazon CloudFront)
// use big-endian.
rsa.put_LittleEndian(false);
String strData;
strData = "This is the string to be signed.";
// Sign the string using the sha-1 hash algorithm.
// Other valid choices are "md2", "md5", "sha256",
// "sha384", and "sha512".
String hexSig;
hexSig = rsa.signStringENC(strData,"sha-1");
outStr += hexSig + "\n";
outStr += "Success!" + "\n";
tv.setText(outStr);
setContentView(tv);
}
static {
// Important: Make sure the name passed to loadLibrary matches the shared library
// found in your project's libs/armeabi directory.
// for "libchilkat.so", pass "chilkat" to loadLibrary
// for "libchilkatemail.so", pass "chilkatemail" to loadLibrary
// etc.
//
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."
}
}
|