MessageDigest md5 = MessageDigest.getInstance("MD5");
for (int i = 0; ichain.length; i++) {
X509Certificate cert = chain[i];
System.out.println(" " + (i + 1) + " Subject "
+ cert.getSubjectDN());
System.out.println("Issuer" + cert.getIssuerDN());
sha1.update(cert.getEncoded());
System.out.println("sha1" + toHexString(sha1.digest()));
md5.update(cert.getEncoded());
System.out.println("md5" + toHexString(md5.digest()));
System.out.println();
}
System.out
.println("Enter certificate to add to trusted keystore or 'q' to quit: [1]");
String line = reader.readLine().trim();
int k;
try {
k = (line.length() == 0) ? 0 : Integer.parseInt(line) - 1;
} catch (NumberFormatException e) {
System.out.println("KeyStore not changed");
return;
}
X509Certificate cert = chain[k];
String alias = host + "-" + (k + 1);
ks.setCertificateEntry(alias, cert);
OutputStream out = new FileOutputStream("jssecacerts");
ks.store(out, passphrase);
out.close();
System.out.println();
System.out.println(cert);
System.out.println();
System.out
.println("Added certificate to keystore 'jssecacerts' using alias '"
+ alias + "'");
}
private static final char[] HEXDIGITS = "0123456789abcdef".toCharArray();
private static String toHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 3);
for (int b : bytes) {
b = 0xff;
sb.append(HEXDIGITS[b4]);
sb.append(HEXDIGITS[b15]);
sb.append(' ');
}
return sb.toString();
}
private static class SavingTrustManager implements X509TrustManager {
private final X509TrustManager tm;
private X509Certificate[] chain;
SavingTrustManager(X509TrustManager tm) {
this.tm = tm;
}
public X509Certificate[] getAcceptedIssuers() {
throw new UnsupportedOperationException();
}
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
throw new UnsupportedOperationException();
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
this.chain = chain;
tm.checkServerTrusted(chain, authType);
}
}
}
第三
将上面的InstallCert.java编译成InstallCert.class文件放到自己电脑的D盘根目录下 。这是正常的情况下D盘根目录下会有3个文件
第四
打开cmd进入到d盘开始执行生成证书命令,我这里不便于那我的网址测试我用支付宝的网址来测试的
当出现了:Enter certificate to add to trusted keystore or 'q' to quit: [1]
这行代码时,输入1,回车 。正常执行完后在D盘根目录下就会出现证书“jssecacerts”文件
得到证书后将证书拷贝到$JAVA_HOME/jre/lib/security目录下,我这里是win7系统,在尝试的过程中需要将证书重命名为:cacerts 放进去才会有用 。
去年在ITjob刘老师那边听他说过,tlsjava代码你试试看行不行 。
求解java怎样发送https请求使用httpClient可以发送tlsjava代码 , 具体tlsjava代码的可以参考下面tlsjava代码的代码
SSLClient类tlsjava代码,继承至HttpClient
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
//用于进行Https请求tlsjava代码的HttpClient
public class SSLClient extends DefaultHttpClient{
推荐阅读
- ppt怎么转换为pdf,pdf如何转化为ppt
- cpu什么好些,cpu哪些比较好
- windows下redis启动服务器,redis启动服务端
- word你如何输入横线,word里如何输入横线
- vb.net文件下文件 vbnet fileopen
- c上传文件到服务器,c 文件传输
- p40微信没有视频号,华为p40微信里为什么没有视频号
- php组数新增数据库 php创建数据库
- yunos系统好还是鸿蒙好,yunos好用吗