是否有可能在不使用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