Java 为密钥库设置证书。TrustedCertificateEntry?

Java 为密钥库设置证书。TrustedCertificateEntry?,java,x509certificate,keystore,Java,X509certificate,Keystore,我想用另一种方法剥这只猫的皮 Java有一个类密钥库。TrustedCertificateEntry,但我不知道如何将证书加载到其中。我的代码如下所示: import java.security.KeyStore.TrustedCertificateEntry; ... X509Certificate ca = (X509Certificate) CertificateFactory(...); KeyStore ks = TrustedCertificateEntry(ca); 以及: 以

我想用另一种方法剥这只猫的皮

Java有一个类
密钥库。TrustedCertificateEntry
,但我不知道如何将证书加载到其中。我的代码如下所示:

import java.security.KeyStore.TrustedCertificateEntry;
...

X509Certificate ca = (X509Certificate) CertificateFactory(...);
KeyStore ks = TrustedCertificateEntry(ca);
以及:

以及:

以及:

程序无法编译,错误如下:

SuperCert.java:33: error: cannot find symbol
KeyStore ks = TrustedCertificateEntry(ca);
                ^
  symbol:   method TrustedCertificateEntry(X509Certificate)
  location: class TestCert
将X509证书加载到
密钥库
后,我计划在
TrustManagerFactory
中使用它,并最终使用
HttpsURLConnection
获取一个网页


如何将
X509Certificate
加载到
TrustedCertificateEntry

我根据Vit Hnilica在上的回答找到了它。我将用这个答案来回答这个问题,因为大多数堆栈溢出答案都是以“使用
openssl
转换,然后使用
keytool
…”开头的

维特发布了这个答案,这让我很不高兴。Hnilica的答案是我在翻阅了关于堆栈溢出的类似问题和答案后找到的唯一答案

String CA_FILE = ...;

FileInputStream fis = new FileInputStream(CA_FILE);
X509Certificate ca = (X509Certificate) CertificateFactory.getInstance(
        "X.509").generateCertificate(new BufferedInputStream(fis));

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
ks.setCertificateEntry(Integer.toString(1), ca);

TrustManagerFactory tmf = TrustManagerFactory
        .getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);

我是根据Vit Hnilica在上的答案找到的。我将用这个答案来回答这个问题,因为大多数堆栈溢出答案都是以“使用
openssl
转换,然后使用
keytool
…”开头的

维特发布了那个答案,这对他来说是一种耻辱。Hnilica的答案是我在翻阅了关于Stack Overflow的类似问题和答案后找到的唯一答案

String CA_FILE = ...;

FileInputStream fis = new FileInputStream(CA_FILE);
X509Certificate ca = (X509Certificate) CertificateFactory.getInstance(
        "X.509").generateCertificate(new BufferedInputStream(fis));

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
ks.setCertificateEntry(Integer.toString(1), ca);

TrustManagerFactory tmf = TrustManagerFactory
        .getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);

还有另一种方法

CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(new FileInputStream(file));
keyStore.setEntry(alias, new KeyStore.TrustedCertificateEntry(certificate), null);

TrustedCertificateEntry的ProtectionParameter应为null。

还有另一种方法

CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(new FileInputStream(file));
keyStore.setEntry(alias, new KeyStore.TrustedCertificateEntry(certificate), null);
TrustedCertificateEntry的ProtectionParameter应为null

CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(new FileInputStream(file));
keyStore.setEntry(alias, new KeyStore.TrustedCertificateEntry(certificate), null);