1

我正在尝试在 Android 上创建加密邮件,使用:

  1. 来自这里的Javamail 库,
  2. SpongyCastel 库 Core-1.51.0.0.jar、pg-1.51.0.0.jar、pkix-1.51.0.0.jar、prov-1.51.0.0.jar 和 scmail-jdk15on-1.47.0.3.jar。

下面是代码[从 spongycastel 示例源复制]:

MailcapCommandMap mailcap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mailcap.addMailcap("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
mailcap.addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
mailcap.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature");
mailcap.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
mailcap.addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");

CommandMap.setDefaultCommandMap(mailcap);

if (Security.getProvider("SC") == null) {
    Security.addProvider(new BouncyCastleProvider());
}

KeyStore ks = KeyStore.getInstance("PKCS12", "SC");
ks.load(new FileInputStream("keystorefile.pfx"), "passwd".toCharArray());
Enumeration e = ks.aliases();
String keyAlias = null;
while (e.hasMoreElements()) {
    String alias = (String) e.nextElement();
    if (ks.isKeyEntry(alias)) {
        keyAlias = alias;
    }
}
if (keyAlias == null) {
    System.err.println("can't find a private key!");
    System.exit(0);
}
Certificate[] chain = ks.getCertificateChain(keyAlias);
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator((X509Certificate) chain[0]).setProvider("SC"));
MimeBodyPart msg = new MimeBodyPart();
msg.setText("Hello world welcome!");
MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC).setProvider("SC").build());
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
Address fromUser = new InternetAddress("\"abc cdf\"<abc.cdf@gmail.com>");
Address toUser = new InternetAddress("abc.cdf@gmail.com");
MimeMessage body = new MimeMessage(session);
body.setFrom(fromUser);
body.setRecipient(Message.RecipientType.TO, toUser);
body.setSubject("example encrypted message");
body.setContent(mp.getContent(), mp.getContentType());
body.saveChanges();
body.writeTo(new FileOutputStream("/sdcard/smime.p7m"));

当它调用时body.writeTo(),会抛出以下异常。请让我知道如何解决此问题:

javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/pkcs7-mime; name="smime.p7m"; smime-type=enveloped-data
W/System.err( 7226):    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:905)
W/System.err( 7226):    at javax.activation.DataHandler.writeTo(DataHandler.java:330)
W/System.err( 7226):    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
W/System.err( 7226):    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
W/System.err( 7226):    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1721)
W/System.err( 7226):    at com.agy.createmail.SignEncryptMail.createSignedEncryptedMail(EncryptMail.java:151)
W/System.err( 7226):    at com.agy.createmail.MainActivity$CreateMailTask.doInBackground(MainActivity.java:32)
W/System.err( 7226):    at com.agy.createmail.MainActivity$CreateMailTask.doInBackground(MainActivity.java:1)
W/System.err( 7226):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err( 7226):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err( 7226):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err( 7226):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err( 7226):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err( 7226):    at java.lang.Thread.run(Thread.java:841)
4

1 回答 1

0

该行的定义存在问题,

application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature

smime.handlers.*spongycastle 中的子包不再存在org.bouncycastle,所以你必须更改bouncyCastle并重spongyCastle试,但它不会工作老兄,我正在努力解决它,因为它依赖于java.awt.*JDK,我什至尝试依赖于 android-additions.jar邮件项目,但仍然无效,我不知道如何使它工作,你成功使它工作了吗?

于 2015-08-18T08:07:09.453 回答