,當我試圖使用生成或BouncyCastle的Sun.Security證書我面對的問題。*問題在Java中生成自簽名證書(X509),私鑰和公鑰編程方式
要求 - Android的API支持 - 對於15 API和API 8
我嘗試以下的方法來做到這一點..
1)使用BouncyCastle的罐子下面的代碼
我試着面對這種代碼個X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();
v3CertGen.setSerialNumber(BigInteger.valueOf(new SecureRandom().nextInt()));
v3CertGen.setIssuerDN(new X509Principal("CN=" + domainName + ", OU=None, O=None L=None, C=None"));
v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));
v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365*10)));
v3CertGen.setSubjectDN(new X509Principal("CN=" + domainName + ", OU=None, O=None L=None, C=None"));
//
v3CertGen.setPublicKey(KPair.getPublic());
v3CertGen.setSignatureAlgorithm("MD5WithRSAEncryption");
X509Certificate PKCertificate = v3CertGen.generateX509Certificate(KPair.getPrivate());
問題:
- CertificateGenerator是depricated
- X509V3CertificateGenerator類是未鑑定
- 嘗試與不同版本的BouncyCastle的罐子(1.45,1.46,1.47 & 1.57)
- 嘗試使用CertificateBuilder(代碼如下)
SubjectPublicKeyInfo當我使用這段代碼時,這個類沒有被識別。
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(kp.getPublic().getEncoded()); X509v3CertificateBuilder myX509v3CertificateBuilder = new X509v3CertificateBuilder(new X500Name("c=sree"), BigInteger.valueOf(new Random().nextInt(1000000)), new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 *365 * 100)), new X500Name("c=sree"), publicKeyInfo); ContentSigner signer = new JcaContentSignerBuilder("Sha256withRSA").build(myCAPrivateKey); X509CertificateHolder certHolder = myX509v3CertificateBuilder.build(signer); X509Certificate cert = (new JcaX509CertificateConverter().getCertificate(certHolder)); CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC"); Certificate certcert = cf.generateCertificate(new ByteArrayInputStream(cert.getEncoded()));
2)我試圖與Sun.Security *包與下面碼
import java.security.cert.X509Certificate;
import sun.security.tools.keytool.CertAndKeyGen;
import sun.security.x509.X500Name;
public class SelfSignedCertificateGeneration {
public static void main(String[] args){
try{
CertAndKeyGen keyGen=new CertAndKeyGen("RSA","SHA1WithRSA",null);
keyGen.generate(1024);
//Generate self signed certificate
X509Certificate[] chain=new X509Certificate[1];
chain[0]=keyGen.getSelfCertificate(new X500Name("CN=ROOT"), (long)365*24*3600);
System.out.println("Certificate : "+chain[0].toString());
}catch(Exception ex){
ex.printStackTrace();
}
}
}面對這種代碼
的問題:
- CertAndKeyGen和其他幾個班都不能訪問
**
有沒有其他辦法?請給我建議。
**
這不是工作@pedrofb爲例再次面臨同樣的問題。 – SreeTej
你的意思是你不能編譯代碼? – pedrofb
是的,我無法編譯代碼。錯誤是(無法識別類)找到No.Class.defination。 – SreeTej