如何解决在Android海绵城堡的错误:找不到类java.awt.datatransfer.Data

2019-08-02 16:15发布

我用海绵LIB城堡签署并根据Android上的加密邮件这个例子 。

            /* 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();

当我运行我的应用程序,它返回以下错误:

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)

海绵城堡LIB的源代码似乎需要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中。*)到我的项目,但它仍然有此错误。

Answer 1:

我定我的错误:
+我下载的源代码spongycastle LIB
+更换线“进口java.awt.datatransfer.DataFlavor中;” 通过“进口myjava.awt.datatransfer.DataFlavor;” (包myjava.awt.datatransfer被包括在其它附加的lib )
+重建spongycastle LIB
+使用新spongycastle LIB



Answer 2:

我设法解决这个问题:

0)上的邮件包运行(从“海绵状的脚本”分支)become-spongy.sh脚本1)在邮件创建的AndroidManifest.xml / SRC /主/

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.spongycastle">
</manifest>

2)改变邮件/的build.gradle

apply plugin: 'com.android.library'
dependencies {
    compile 'com.sun.mail:android-mail:1.5.5'
    compile 'com.sun.mail:android:1.5.5'
    compile 'com.madgag.spongycastle:pkix:1.54.0.0'
    compile 'com.madgag.spongycastle:prov:1.54.0.0'
    compile 'com.madgag.spongycastle:core:1.54.0.0'
    testCompile 'junit:junit:4.12'
}

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

3)在处理程序文件(spongycastle /邮件/ src目录/主/ JAVA /组织/ spongycastle /邮件/ SMIME /处理器/):a)将import java.awt.datatransfer.DataFlavor b)将更换的java.awt.datatransfer .DataFlavor变量/返回类型与javax.activation.ActivationDataFlavor

4)在你的Android项目包括文件夹模块

5)建立和乐趣; d

在这里你可以看到一个工作spongycastle邮件包怎么样子: https://github.com/Skywalker-11/spongycastle

这里是一个Android应用程序看起来像这样的例子使用了固定的版本,以通过SMTP发送加密SMIME消息https://github.com/Skywalker-11/SpongyCastleMailTestApp



文章来源: How to fix error of Spongy Castle on Android: could not find class java.awt.datatransfer.DataFlavor