Java 无法通过bouncycastle库在android上生成证书请求
我现在正在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位于com.android.Dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123) [2013-01-27 20:43:25-BlowfishCipher]Dx位于com.android.Dx.dex.file.DexFile.add(DexFile.java:163) 我知道错误消息表明复制了jar文件,但我不知道如何 解决这个问题是因为复制的jar(Lorg/bouncycastle/mozilla/SignedPublicKeyAndChallenge;)是bouncy caslte库的核心文件 下面的java代码没有显示语法错误,但它会导致java.lang.IllegalArgumentException:已添加异常Java 无法通过bouncycastle库在android上生成证书请求,java,android,certificate,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位于com.android.Dx.
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等),就必须重命名它。一个已经为你做这件事的项目是SpongyCastle。试着用它来代替普通的BC-jar