是否有可能在不使用Bouncy Castle X509V * CertificateGenerator类的情况下正确创建Java代码中的X509证书?在没有BouncyCastle的情况下在Java中创建X509证书?
回答
签名证书的能力不是标准Java库或扩展的一部分。
很多需要自己做的代码都是核心的一部分。有些类可以对X.500名称,X.509证书扩展名,各种算法的公钥进行编码和解码,当然也可以用于实际执行数字签名。
自己实现这个并不是微不足道的,但它绝对是可行的—我第一次为证书签名制作了一个工作原型,可能会花费4或5天的时间。这对我来说是一个梦幻般的学习练习,但是当有可用的免费库时,很难证明这个花费是合理的。
JRE中提供了所有用于制作自签名证书(签名,X509编码等)的基本组件。与BC不同,Sun的JCE不提供任何公共电话来签署证书。但是,所有功能都可以在Keytool中使用。您可以简单地复制keytool中的代码来执行此操作。您需要复制的方法是doSelfCert()
。
不幸的是,Keytool为此使用'sun。*'类。所以这不适用于每个JRE。但是,这里是[源代码](https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/sun/security/tools/KeyTool.java) – Pith 2015-09-14 07:30:23
是的,但没有公开记录的类。我已经记录了过程in this article。
import sun.security.x509.*;
import java.security.cert.*;
import java.security.*;
import java.math.BigInteger;
import java.util.Date;
import java.io.IOException
/**
* Create a self-signed X.509 Certificate
* @param dn the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"
* @param pair the KeyPair
* @param days how many days from now the Certificate is valid for
* @param algorithm the signing algorithm, eg "SHA1withRSA"
*/
X509Certificate generateCertificate(String dn, KeyPair pair, int days, String algorithm)
throws GeneralSecurityException, IOException
{
PrivateKey privkey = pair.getPrivate();
X509CertInfo info = new X509CertInfo();
Date from = new Date();
Date to = new Date(from.getTime() + days * 86400000l);
CertificateValidity interval = new CertificateValidity(from, to);
BigInteger sn = new BigInteger(64, new SecureRandom());
X500Name owner = new X500Name(dn);
info.set(X509CertInfo.VALIDITY, interval);
info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner));
info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner));
info.set(X509CertInfo.KEY, new CertificateX509Key(pair.getPublic()));
info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
AlgorithmId algo = new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid);
info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algo));
// Sign the cert to identify the algorithm that's used.
X509CertImpl cert = new X509CertImpl(info);
cert.sign(privkey, algorithm);
// Update the algorith, and resign.
algo = (AlgorithmId)cert.get(X509CertImpl.SIG_ALG);
info.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, algo);
cert = new X509CertImpl(info);
cert.sign(privkey, algorithm);
return cert;
}
一个非常好的提示。通过导入可怕的(和心爱的)Bouncycastle lib保存了我。膨胀起来! – 2011-07-08 11:29:23
有没有办法做到这一点,不涉及到sun.security.x509。*?鉴于它实际上不是你应该使用的东西。 – 2011-11-26 20:23:04
优秀。为我节省了很多工作。代码很好,很干净。我在代码中进行编辑以确保它不会消失。 – Suma 2012-03-20 13:58:04
import sun.security.x509.*;
import java.security.cert.*;
import java.security.*;
import java.math.BigInteger;
import java.security.cert.Certificate;
import java.util.Date;
import java.io.IOException;
public class Example {
/**
* Create a self-signed X.509 Example
*
* @param dn the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"
* @param pair the KeyPair
* @param days how many days from now the Example is valid for
* @param algorithm the signing algorithm, eg "SHA1withRSA"
*/
public X509Certificate generateCertificate(String dn, KeyPair pair, int days, String algorithm)
throws GeneralSecurityException, IOException {
PrivateKey privkey = pair.getPrivate();
X509CertInfo info = new X509CertInfo();
Date from = new Date();
Date to = new Date(from.getTime() + days * 86400000l);
CertificateValidity interval = new CertificateValidity(from, to);
BigInteger sn = new BigInteger(64, new SecureRandom());
X500Name owner = new X500Name(dn);
info.set(X509CertInfo.VALIDITY, interval);
info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
info.set(X509CertInfo.SUBJECT, owner);
info.set(X509CertInfo.ISSUER, owner);
info.set(X509CertInfo.KEY, new CertificateX509Key(pair.getPublic()));
info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
AlgorithmId algo = new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid);
info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algo));
// Sign the cert to identify the algorithm that's used.
X509CertImpl cert = new X509CertImpl(info);
cert.sign(privkey, algorithm);
// Update the algorith, and resign.
algo = (AlgorithmId) cert.get(X509CertImpl.SIG_ALG);
info.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, algo);
cert = new X509CertImpl(info);
cert.sign(privkey, algorithm);
return cert;
}
public static void main (String[] argv) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keyPairGenerator.generateKeyPair();
Example example = new Example();
String distinguishedName = "CN=Test, L=London, C=GB";
Certificate certificate = example.generateCertificateOriginal(distinguishedName, keyPair, 365, "SHA256withRSA");
System.out.println("it worked!");
}
}
我喜欢vbence的答案,但我一直得到以下异常:
java.security.cert.CertificateException:主题类类型无效。
很多尝试找出为一个有效的主题类我发现X509CerInfo想X500Name的实例后。
1 info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
2 info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner));
3 info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner));
4 info.set(X509CertInfo.KEY, new CertificateX509Key(pair.getPublic()));
所以线2 & 3需要改变到
2 info.set(X509CertInfo.SUBJECT, owner);
3 info.set(X509CertInfo.ISSUER, owner);
- 1. 在没有openssl的情况下创建证书
- 2. 如何在不导入root证书的情况下验证X509证书?
- 3. c#在代码中创建x509证书
- 4. 在iOS中动态创建x509证书
- 5. 在没有证书的情况下在mongodb replicaset中启用ssl
- 6. 使用bouncycastle创建证书
- 7. x509证书为byte []在Java和回X509证书在C#
- 8. 如何在没有钥匙串的情况下创建Apple Push证书?
- 9. 从BouncyCastle X509证书获取私钥? C#
- 10. 不能在没有身份验证的情况下创建桶
- 11. 在没有ISBN编号的情况下在Open Graph中创建一本书
- 12. 在B的BouncyCastle中建立证书链#
- 13. 在没有Java的情况下创建Android应用程序
- 14. 将X509Certificate2证书转换为BouncyCastle X509证书
- 15. 将BouncyCastle X509证书+私钥(RSA)导入Windows证书存储
- 16. iOS SSL使用AFNetworking在没有证书的情况下锁定?
- 17. 如何在没有证书的情况下编译设备?
- 18. 在没有证书的情况下使用SSL
- 19. 如何在java中打开X509证书?
- 20. 在没有JSON.Net的情况下在UWP中创建json
- 21. 在没有XmlDocument的情况下在C#中创建XmlNode/XmlElement?
- 22. 在没有Doctrine的情况下在Symfony2中创建实体
- 23. 有没有办法在没有SSL证书的情况下清除HSTS?
- 24. 在没有建立SSL \ TLS连接的情况下获取服务器证书
- 25. 在没有设置对象数的情况下在Java中创建数组?
- 26. 在没有证书的情况下在Xcode 4.3+中部署iOS应用程序
- 27. 使用SAN创建X509证书
- 28. X509证书创建颁发者值
- 29. 在没有证书验证的情况下使用SSL - Oracle SQL Developer
- 30. 如何使用x509证书在eclipse中创建soap客户端
这仍然是准确的2017年? – user674669 2017-10-31 20:32:59