Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > BOOK: Beginning Cryptography with Java
|
BOOK: Beginning Cryptography with Java
This is the forum to discuss the Wrox book Beginning Cryptography with Java by David Hook; ISBN: 9780764596339
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Cryptography with Java section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 13th, 2013, 02:51 PM
Registered User
 
Join Date: Nov 2012
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Default Error on chapter8 SignedMailExample - android

I'm now working on making message sign app for android

First, the following code works successfully on my desktop with JDK 1.6.
(pure java environment)

However,when I trying to run on eclipse for android platform,
It shows "The method setKeyEntry(String, Key, char[], Certificate[]) in the type KeyStore is not applicable for the arguments (String, PrivateKey, char[], Certificate[])" around the line " store.setKeyEntry();"..

I know it is due to the desktop use JDK security provider, while the android use spongy castle security provider.

thus, could you give me any advise on

1. using JDK security provider manually on android.
=> for example I use "Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider ());" to use bouncy castle provider

2. how to modify the above error with "The method setKeyEntry()..."

regards

-SignedMailExample.java

package exam.blowfishcipher;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.security.*;
import java.security.cert.*;
import java.util.Arrays;

import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.spongycastle.asn1.ASN1EncodableVector;
import org.spongycastle.asn1.cms.AttributeTable;
import org.spongycastle.asn1.smime.SMIMECapabilitiesAttri bute;
import org.spongycastle.asn1.smime.SMIMECapability;
import org.spongycastle.asn1.smime.SMIMECapabilityVector;
import org.spongycastle.asn1.smime.SMIMEEncryptionKeyPref erenceAttribute;
import org.spongycastle.asn1.x509.Certificate;
import org.spongycastle.jce.PKCS10CertificationRequest;
import org.spongycastle.mail.smime.SMIMESigned;
import org.spongycastle.mail.smime.SMIMESignedGenerator;
import org.spongycastle.mail.smime.SMIMEUtil;
import org.spongycastle.openssl.PEMReader;
import org.spongycastle.openssl.PEMWriter;

import android.os.*;
import android.util.*;

/**
* a simple example that creates and processes a signed mail message.
*/

public class SignedMailExample
extends SignedDataProcessor
{
//static String userCert;
//static String userPrivate;

//add 20130510
//static String signedMail;
static {
Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider ());

}

public static MimeMultipart createMultipartWithSignature(
PrivateKey key,
X509Certificate cert,
CertStore certsAndCRLs,
MimeBodyPart dataPart)
throws Exception
{
// create some smime capabilities in case someone wants to respond
ASN1EncodableVector signedAttrs = new ASN1EncodableVector();
SMIMECapabilityVector caps = new SMIMECapabilityVector();

caps.addCapability(SMIMECapability.aES256_CBC);
caps.addCapability(SMIMECapability.dES_EDE3_CBC);
caps.addCapability(SMIMECapability.rC2_CBC, 128);

signedAttrs.add(new SMIMECapabilitiesAttribute(caps));
signedAttrs.add(new SMIMEEncryptionKeyPreferenceAttribute(SMIMEUtil.cr eateIssuerAndSerialNumberFor(cert)));

// set up the generator
SMIMESignedGenerator gen = new SMIMESignedGenerator();
gen.addSigner(key, cert, SMIMESignedGenerator.DIGEST_SHA256, new AttributeTable(signedAttrs), null);
gen.addCertificatesAndCRLs(certsAndCRLs);

// create the signed message
return gen.generate(dataPart, "BC");
}
////////////////////////////////////////////added by jeon
public static void pemEncodeToFile(String filename, Object obj, char[] password) throws Exception{
PEMWriter pw = new PEMWriter(new FileWriter(filename));
if (password != null && password.length > 0) {
pw.writeObject(obj, "DESEDE", password, new SecureRandom());
} else {
pw.writeObject(obj);
}
pw.flush();
pw.close();
}
////////////////////////////////////////////added by jeon

////////////////////////////////////////////added by jeon
public static KeyStore createCredentials_modified()
throws Exception
{
KeyStore store = KeyStore.getInstance("BKS");

store.load(null, null);

X500PrivateCredential rootCredential = createRootCredential_modified();

store.setCertificateEntry(rootCredential.getAlias( ), rootCredential.getCertificate());
store.setKeyEntry(rootCredential.getAlias(), rootCredential.getPrivateKey(), "password".toCharArray(),
new Certificate[] { rootCredential.getCertificate(), rootCredential.getCertificate(), rootCredential.getCertificate() });

return store;
}

public static X500PrivateCredential createRootCredential_modified()
throws Exception
{
PEMReader rootPriva = new PEMReader(
new InputStreamReader(
new FileInputStream(Environment.getExternalStorageDire ctory()+"/pkcs10priv.key"))); //modified 20130510

PEMReader rootCerti = new PEMReader(
new InputStreamReader(
new FileInputStream(Environment.getExternalStorageDire ctory()+"/userCert.cer")));

KeyPair rootPrivate = (KeyPair)rootPriva.readObject();
X509Certificate rootCert = (X509Certificate)rootCerti.readObject();
//KeyPair rootPair = generateRSAKeyPair();
//X509Certificate rootCert = generateRootCert(rootPair);
return new X500PrivateCredential(rootCert, rootPrivate.getPrivate(), "root"); //?????? root
}
////////////////////////////////////////////added by jeon


public static String signMail(String plainMessage) throws Exception{

KeyStore credentials = createCredentials_modified();
PrivateKey key = (PrivateKey)credentials.getKey("root", "password".toCharArray());
java.security.cert.Certificate[] chain = credentials.getCertificateChain("root");
CertStore certsAndCRLs = CertStore.getInstance("Collection",
new CollectionCertStoreParameters(Arrays.asList(chain) ), "BC");
X509Certificate cert = (X509Certificate)chain[0];
Log.e("Position", "position1");
// create the message we want signed
MimeBodyPart dataPart = new MimeBodyPart();
dataPart.setText(plainMessage);
Log.e("Position", "position2");
// create the signed message
MimeMultipart multiPart = createMultipartWithSignature(key, cert, certsAndCRLs, dataPart);

// create the mail message
MimeMessage mail = Utils.createMimeMessage("my signed message", multiPart, multiPart.getContentType());
Log.e("Position", "position3");
//added by JEON
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDir ectory()+"/SignedSMS.mail");
String SingedSMS = mail.toString();
fos.write(SingedSMS.getBytes());
fos.close();


////
return SingedSMS;
}
////

}
 
Old May 13th, 2013, 03:34 PM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

Sorry, you need to address this one to an Android developers list.

The only thing I could suggest is that there is an issue with your class path. The error message makes no sense to me.

Regards,

David





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter8-Page 277-step 2-4 bingfengqi BOOK: Beginning ASP.NET 4 : in C# and VB 7 May 28th, 2012 03:19 AM
Android SDK/Android.bat does not find SWT.jar file chaoticandroid BOOK: Professional Android 2 Application Development 1 March 23rd, 2011 06:39 PM
Android Emulator error Keekslb BOOK: Professional Android 2 Application Development 0 September 2nd, 2010 10:58 AM
chapter8-viewstate collection lustigon BOOK: Beginning ASP.NET 4 : in C# and VB 3 June 29th, 2010 06:45 AM
Chapter 9 - SignedMailExample BKD BOOK: Beginning Cryptography with Java 4 June 18th, 2010 01:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.