Hello, I downloaded the 30 days test license of iText for Android and want to test it with a prototype for digital signing pdfs with a *.p12 certificate.
Then I installed the *.p12 certificate correctly in the Android Keychain, but when I want to sign a pdf I get a Nullpointer Exception from the Spongy Castle API (enclosed the source code and the exception). Exist any example code for Android because I didn´t find any? regards, Florian Gruber Siemens AG Infrastructure & Cities Sector Rail Systems Division Information Technology IC RL IT PLM WEB Sieboldstr. 16 91052 Erlangen, Deutschland Tel: +49 9131 7-22942 mailto:[email protected] Siemens Aktiengesellschaft: Vorsitzender des Aufsichtsrats: Gerhard Cromme; Vorstand: Peter Löscher, Vorsitzender; Roland Busch, Brigitte Ederer, Klaus Helmrich, Joe Kaeser, Barbara Kux, Hermann Requardt, Siegfried Russwurm, Peter Y. Solmssen, Michael Süß; Sitz der Gesellschaft: Berlin und München, Deutschland; Registergericht: Berlin Charlottenburg, HRB 12300, München, HRB 6684; WEEE-Reg.-Nr. DE 23691322
<<inline: Exception.png>>
public class MainActivity extends Activity implements KeyChainAliasCallback {
// Log tag for this class
private static final String TAG = "ESignaturMobile";
// Alias for certificate
private static final String DEFAULT_ALIAS = "My Key Chain";
// Name of the application preference
private static final String KEYCHAIN_PREF = "keychain";
// Name of preference name that saves the alias
private static final String KEYCHAIN_PREF_ALIAS = "alias";
private BouncyCastleProvider provider = new BouncyCastleProvider();
private Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Security.addProvider(provider);
LicenseKey.loadLicenseFile(android.os.Environment
.getExternalStorageDirectory()
+ java.io.File.separator
+ "temp/itextkey.xml");
signPdf();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private Context getContext() {
return this;
}
/**
* This method returns the alias of the key chain from the application
* preference
*
* @return The alias of the key chain
*/
private String getAlias() {
SharedPreferences pref = getSharedPreferences(KEYCHAIN_PREF,
MODE_PRIVATE);
return pref.getString(KEYCHAIN_PREF_ALIAS, DEFAULT_ALIAS);
}
/**
* This method sets the alias of the key chain to the application
preference
*/
private void setAlias(String alias) {
SharedPreferences pref = getSharedPreferences(KEYCHAIN_PREF,
MODE_PRIVATE);
Editor editor = pref.edit();
editor.putString(KEYCHAIN_PREF_ALIAS, alias);
editor.commit();
}
private void signPdf() {
KeyChain.choosePrivateKeyAlias(this, this, new String[] {},
null,
"localhost", -1, DEFAULT_ALIAS);
}
@Override
public void alias(String alias) {
if (alias != null) {
setAlias(alias); // Set the alias in the application
preference
} else {
Log.d(TAG, "User hit Disallow");
}
Log.d(TAG, "Thread: " + Thread.currentThread().getName());
Log.d(TAG, "selected alias: " + alias);
new AsyncTask<Void, Void, Boolean>() {
private Exception error;
@Override
protected Boolean doInBackground(Void... arg) {
try {
// Generate Document with signature
PrivateKey pk;
pk =
KeyChain.getPrivateKey(getContext(), getAlias());
X509Certificate[] chain;
chain =
KeyChain.getCertificateChain(getContext(),
getAlias());
String FileName = android.os.Environment
.getExternalStorageDirectory()
+
java.io.File.separator + "temp"
// "/storage/sdcard0" +
java.io.File.separator +
// "temp"
+
java.io.File.separator + "signature_signed.pdf";
// Creating the reader and the stamper
PdfReader reader = new PdfReader(
android.os.Environment
.getExternalStorageDirectory()
+
java.io.File.separator
//
"/storage/sdcard0" +
//
java.io.File.separator
+ "temp"
+
java.io.File.separator
+
"signature.pdf");
FileOutputStream os = new
FileOutputStream(FileName);
PdfStamper stamper =
PdfStamper.createSignature(reader, os,
'\0');
// Creating the appearance
PdfSignatureAppearance appearance =
stamper
.getSignatureAppearance();
appearance.setVisibleSignature(new
Rectangle(36, 748, 144,
780), 1, "sig");
// Creating the signature
Log.d(TAG, "Provider: " +
provider.getName());
ExternalSignature es = new
PrivateKeySignature(pk,
DigestAlgorithms.SHA256, provider.getName());
ExternalDigest digest = new
BouncyCastleDigest();
MakeSignature.signDetached(appearance,
digest, es, chain,
null, null, null, 0,
CryptoStandard.CMS);
Log.d(TAG, "signed");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(Boolean valid) {
if (error != null) {
Toast.makeText(getContext(),
"Error: " +
error.getMessage(), Toast.LENGTH_LONG)
.show();
return;
}
Toast.makeText(getContext(), "Signiert: " +
valid,
Toast.LENGTH_LONG).show();
}
}.execute();
}
}------------------------------------------------------------------------------ This SF.net email is sponsored by Windows: Build for Windows Store. http://p.sf.net/sfu/windows-dev2dev
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
