我正在尝试使用以下方法在Android上创建加密邮件:
下面是代码[从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)
答案 0 :(得分:0)
此行的定义中存在问题,
application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature
spongycastle中的 smime.handlers.*
子包已不再存在于org.bouncycastle
下,因此您必须将bouncyCastle
更改为spongyCastle
并再试一次,但这不会有效,我我正在努力解决它,因为它依赖于JDK的java.awt.*
,我甚至尝试依赖来自android-mail项目的additionals.jar,但仍然徒劳无功,我无法弄清楚如何让它工作,你成功地使它发挥作用了吗?