我需要通过java以编程方式获取安全网站的公钥.我看了这个,这个,这个和这个和其他的方面.但我还没有找到通过java获得它的解决方案.
编辑:::
根据Zielu的回答,我写了以下程序:
import java.security.PublicKey;
import java.security.cert.Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class RetrievePublicKey {
private static PublicKey getKey(String hostname, int port) throws Exception {
SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
SSLSocket socket = (SSLSocket) factory.createSocket(hostname, port);
socket.startHandshake();
Certificate[] certs = socket.getSession().getPeerCertificates();
Certificate cert = certs[0];
PublicKey key = cert.getPublicKey();
System.out.println(key);
return key;
}
public static void main(String[] args) throws Exception {
System.out.println(getKey("bctcl-parasuram.bctchn.local", 8443));
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我得到以下异常:
Exception in thread "main" javax.net.ssl.SSLHandshakeException: …Run Code Online (Sandbox Code Playgroud)