gpt4 book ai didi

java - 将证书添加到 keystore 和信任库

转载 作者:太空宇宙 更新时间:2023-11-03 13:00:25 25 4
gpt4 key购买 nike

certs private key(client) and client side certs all in pem format我必须将它们添加到信任库和 keystore 中。我该怎么做?到目前为止,我在 cmd(windows) 中有命令:

  1. 生成 pkcs( key :客户端私钥,证书:客户端证书,ca:cacert):openssl pkcs12 -inkey key.pem -in cert.pem -export -out keystored.p12 -certfile ca。 pem

  2. keytool -importkeystore -destkeystore mykeystore.jks -srckeystore keystore.p12 -srcstoretype pkcs12

这些命令正确吗?

我编写了一个通过 ssl 连接服务器的程序。但是它显示错误线程“main”中的异常 javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated在 sun.security.ssl.SSLSessionImpl.getPeerCertificates(未知来源)

程序如下:(程序是获取docker info)

package test_abc;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class TrustHttp {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
SSLContext ctx = SSLContext.getInstance("TLS");
TrustManager[] trustManagers = getTrustManagers("jks", new FileInputStream(new File("C:\\Users\\akarki\\Desktop\\certi\\abcd\\mykeystore.jks")), "password1");
KeyManager[] keyManagers = getKeyManagers("pkcs12", new FileInputStream(new File("C:\\Users\\akarki\\Desktop\\certi\\abcd\\KeyStore.p12")), "password1");
ctx.init(keyManagers, trustManagers, new SecureRandom());
SSLSocketFactory factory = new SSLSocketFactory(ctx, new StrictHostnameVerifier());
ClientConnectionManager manager = httpClient.getConnectionManager();
manager.getSchemeRegistry().register(new Scheme("https", 443, factory));
//as before
HttpGet httpget = new HttpGet("https://URL:2376/images/json");
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
}finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
}
protected static KeyManager[] getKeyManagers(String keyStoreType, InputStream keyStoreFile, String keyStorePassword) throws Exception {
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(keyStoreFile, keyStorePassword.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, keyStorePassword.toCharArray());
return kmf.getKeyManagers();
}
protected static TrustManager[] getTrustManagers(String trustStoreType, InputStream trustStoreFile, String trustStorePassword) throws Exception {
KeyStore trustStore = KeyStore.getInstance(trustStoreType);
trustStore.load(trustStoreFile, trustStorePassword.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustStore);
return tmf.getTrustManagers();
}
}

最佳答案

使用此命令,您可以将证书导入现有或新的 keystore :

keytool -import -alias aliasForCert -file /path/to/ca.pem -keystore cacerts –storepass changeit

根据您的需要重命名 aliasForCert、cacerts 并更改密码(如果它是新的 keystore )。

关于java - 将证书添加到 keystore 和信任库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45711911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com