问题描述
我根据使用的lib海绵城堡的签名和加密的邮件在Android 这个例子。
I use lib Spongy Castle for signing and encrypting mail on Android according to this example.
/* Add BC */
Security.addProvider(new BouncyCastleProvider());
/* Open the keystore */
KeyStore keystore = KeyStore.getInstance("PKCS12", "SC");
keystore.load(new FileInputStream(pkcs12Keystore),
password.toCharArray());
Certificate[] chain = keystore.getCertificateChain(keyalias);
/* Get the private key to sign the message with */
PrivateKey privateKey = (PrivateKey) keystore.getKey(keyalias,
password.toCharArray());
/* Create the SMIMESignedGenerator */
SMIMECapabilityVector capabilities = new SMIMECapabilityVector();
capabilities.addCapability(SMIMECapability.dES_EDE3_CBC);
capabilities.addCapability(SMIMECapability.rC2_CBC, 128);
capabilities.addCapability(SMIMECapability.dES_CBC);
ASN1EncodableVector attributes = new ASN1EncodableVector();
attributes.add(new SMIMEEncryptionKeyPreferenceAttribute(
new IssuerAndSerialNumber(new X500Name(
((X509Certificate) chain[0]).getIssuerDN()
.getName()), ((X509Certificate) chain[0])
.getSerialNumber())));
attributes.add(new SMIMECapabilitiesAttribute(capabilities));
SMIMESignedGenerator signer = new SMIMESignedGenerator();
signer.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder()
.setProvider("SC")
.setSignedAttributeGenerator(new AttributeTable(attributes))
.build("DSA".equals(privateKey.getAlgorithm()) ? "SHA1withDSA"
: "MD5withDSA", privateKey,
(X509Certificate) chain[0]));
/* Add the list of certs to the generator */
List certList = new ArrayList();
certList.add(chain[0]);
Store certs = new JcaCertStore(certList);
signer.addCertificates(certs);
/* Sign the message */
MimeMultipart mm = signer.generate(originalMessage, "SC");
signedMessage = new MimeMessage(session);
/* Set all original MIME headers in the signed message */
Enumeration headers = originalMessage.getAllHeaderLines();
while (headers.hasMoreElements()) {
signedMessage.addHeaderLine((String) headers.nextElement());
}
/* Set the content of the signed message */
signedMessage.setContent(mm);
signedMessage.saveChanges();
ByteArrayOutputStream out = new ByteArrayOutputStream();
signedMessage.writeTo(out);
FileOutputStream f = new FileOutputStream(new File(
"/sdcard/MobilePKI/out.txt"));
f.write(out.toByteArray());
f.close();
当我跑我的应用程序,它返回以下错误:
When I run my app, it returns the following error:
11-13 08:54:11.867: E/dalvikvm(13188): Could not find class '[Ljava.awt.datatransfer.DataFlavor;', referenced from method org.spongycastle.mail.smime.handlers.multipart_signed.<clinit>
11-13 08:54:11.890: E/AndroidRuntime(13188): FATAL EXCEPTION: main
11-13 08:54:11.890: E/AndroidRuntime(13188): java.lang.VerifyError: org.spongycastle.mail.smime.handlers.multipart_signed
11-13 08:54:11.890: E/AndroidRuntime(13188): at java.lang.Class.newInstanceImpl(Native Method)
11-13 08:54:11.890: E/AndroidRuntime(13188): at java.lang.Class.newInstance(Class.java:1409)
11-13 08:54:11.890: E/AndroidRuntime(13188): at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:609)
11-13 08:54:11.890: E/AndroidRuntime(13188): at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:563)
11-13 08:54:11.890: E/AndroidRuntime(13188): at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:626)
11-13 08:54:11.890: E/AndroidRuntime(13188): at javax.activation.DataHandler.writeTo(DataHandler.java:329)
11-13 08:54:11.890: E/AndroidRuntime(13188): at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
11-13 08:54:11.890: E/AndroidRuntime(13188): at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
11-13 08:54:11.890: E/AndroidRuntime(13188): at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1721)
来源$海绵城堡ç$ C的lib似乎需要java.awt.datatransfer中的:
Source code of Spongy Castle lib seems to need java.awt.datatransfer:
package org.spongycastle.mail.smime.handlers;
import org.spongycastle.mail.smime.SMIMEStreamingProcessor;
import javax.activation.ActivationDataFlavor;
import javax.activation.DataContentHandler;
import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import java.awt.datatransfer.DataFlavor;
import java.io.BufferedInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
(链接:https://github.com/rtyley/spongycastle/blob/spongy-master/scmail-jdk15on/src/main/java/org/spongycastle/mail/smime/handlers/multipart_signed.java)
我觉得它必须导入包java.awt中和我导入rt.jar中(包括java.awt中。*)到我的项目,但它仍然有此错误。
I think it must import package java.awt and I imported rt.jar (including java.awt.*) into my project but it still has this error.
推荐答案
我固定我的错误:结果
+我下载结果源$ C $ C
+取代线进口java.awt.datatransfer.DataFlavor中;通过进口myjava.awt.datatransfer.DataFlavor; (包myjava.awt.datatransfer包含在 )结果
+重建spongycastle LIB结果
+使用新spongycastle的lib
I fixed my error:
+ I download source code of spongycastle lib
+ replace line "import java.awt.datatransfer.DataFlavor;" by "import myjava.awt.datatransfer.DataFlavor;" (package myjava.awt.datatransfer is included in additionnal lib)
+ Rebuild spongycastle lib
+ Using new spongycastle lib
这篇关于如何修复Android上的海绵状城堡错误:找不到类java.awt.datatransfer.DataFlavor中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!