java - 无法通过 bouncycaSTLe 库在 Android 上生成证书请求

标签 java android certificate

我现在正在 Android 上开发数字签名应用程序。

但是,我未能制作 apk 文件,因为 eclipse 显示以下消息

[2013-01-27 20:43:25 - BlowfishCipher] Dx 意外的顶级异常: java.lang.IllegalArgumentException:已添加:Lorg/bouncycaSTLe/mozilla/SignedPublicKeyAndChallenge;

[2013-01-27 20:43:25 - BlowfishCipher] Dx at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123) [2013-01-27 20:43:25 - BlowfishCipher] Dx at com.android.dx.dex.file.DexFile.add(DexFile.java:163)

我知道错误消息表明存在重复的 jar 文件,但我不知道如何 解决这个问题,因为重复的 jar (Lorg/bouncycaSTLe/mozilla/SignedPublicKeyAndChallenge;) 是 bouncy caslte 库的核心文件。

下面的java代码没有显示语法错误,但它导致java.lang.IllegalArgumentException:已经添加异常

package exam.blowfishcipher;

import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.security.KeyPair;
import java.security.SecureRandom;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.jce.PKCS10CertificationRequest;
import org.bouncycastle.openssl.PEMWriter;
import android.os.Environment;
import chapter6.PKCS10ExtensionExample;



public class PKCS10Generater
{
    public static PKCS10CertificationRequest generateRequest(
            KeyPair pair)
            throws Exception

            {           
             return new PKCS10CertificationRequest(
                     "SHA256withRSA",
                     new X500Principal("CN=Test CA Certificate"),
                     //new X500Principal("CN=end"),
                     pair.getPublic(),
                     null,
                     pair.getPrivate());
            }

    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();
    }

    public static void reqGen() throws Exception
    {
        //create the keys
        /*
        KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
        //KeyPairGenerator kpGen = KeyPairGenerator.getInstance()

        kpGen.initialize(512, chapter4.Utils.createFixedRandom());

        KeyPair pair=kpGen.generateKeyPair();
        */
        //PKCS10CertificationRequest request = generateRequest(pair);

        KeyPair pair = chapter8.Utils.generateRSAKeyPair();
        PKCS10CertificationRequest request = PKCS    10ExtensionExample.generateRequest(pair);


        pemEncodeToFile(Environment.getExternalStorageDirectory()+"pkcs10.req",     request, null);
        PEMWriter pemWrt = new PEMWriter( new OutputStreamWriter(System.out));
        pemWrt.writeObject(request);
        pemWrt.close();     
    }


}

最佳答案

Android 系统中已经包含 BouncyCaSTLe,这就是为什么您在尝试再次添加它时会收到错误(在最近的版本中,它实际上位于 com.android.org.bouncycaSTLe 或类似的位置)所以理论上你不应该得到这个错误)。如果你想将 BC 包含在你的项目中(使用 jarjar 等),你必须重命名 BC 的包。 SpongyCaSTLe 是一个已经为您完成此任务的项目。尝试使用它代替常规的 BC jar。

https://github.com/rtyley/spongycastle

关于java - 无法通过 bouncycaSTLe 库在 Android 上生成证书请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14547151/

相关文章:

java - Android 无法对服务器进行 GET 改造 API 调用

javascript - 如何在我的 Node.js 服务器上验证 GKLocalPlayer

java - 在测试环境中禁用 mongoEmbeddedServer

android - RelativeLayout 两个 TextViews 在按钮上方居中

java - Apache Flink 测试中是否有像 Reactor 和 RxJava 中那样的虚拟时间概念

android - Kivy 添加小部件错误

Asp.net、Silverlight 和 HTTPS - 始终验证证书

ssl - 使用 TLS 创建自签名证书

Java - 允许用户将节点添加到链表的方法

java - 使用 log4j 进行日志记录的 JUnit 默认配置是什么以及如何使用 eclipse 更改它?