Java,Web,https,读取网站申请SSL证书,JKS、PFX、CRT格式

  网站配置https,需要为域名申请证书,例如域名为:www.xxx.com,申请的证书为:

  

Java,Web,https,读取网站申请SSL证书,JKS、PFX、CRT格式

  从上图可以看出,证书除了公钥/私钥,直接为:Nginx、Apache、IIS、Tomcat做好了准备。

  读取.pem、.key、.crt

  package com.what21.demo.cert;

  import cn.hutool.crypto.PemUtil;

  import org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPrivateCrtKey;

  import org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPublicKey;

  import java.io.FileInputStream;

  import java.io.InputStream;

  import java.security.cert.CertificateFactory;

  import java.security.cert.X509Certificate;

  public class CertKeyUtils {

  /**

  * 读取 .pem 文件

  *

  * @param file

  * @return

  * @throws Exception

  */

  public static BCRSAPublicKey readPemToPublicKey(String file) throws Exception {

  InputStream input=new FileInputStream(file);

  BCRSAPublicKey key=(BCRSAPublicKey) PemUtil.readPemKey(input);

  return key;

  }

  /**

  * 读取 .key 文件

  *

  * @param file

  * @return

  * @throws Exception

  */

  public static BCRSAPrivateCrtKey readKeyToPrivateKey(String file) throws Exception {

  InputStream input=new FileInputStream(file);

  BCRSAPrivateCrtKey key=(BCRSAPrivateCrtKey) PemUtil.readPemPrivateKey(input);

  return key;

  }

  /**

  * 读取 .crt 文件

  *

  * @param file

  * @return

  * @throws Exception

  */

  public static X509Certificate readCrtToPublicKey(String file) throws Exception {

  InputStream input=new FileInputStream(file);

  CertificateFactory cf=CertificateFactory.getInstance("X.509");

  X509Certificate cert=(X509Certificate) cf.generateCertificate(input);

  return cert;

  }

  public static void main(String[] args) throws Exception {

  String pemFile="D:\\wx\\www.xxx.com\\www.xxx.com.pem";

  System.out.println(readPemToPublicKey(pemFile));

  String keyFile="D:\\wx\\www.xxx.com\\www.xxx.com.key";

  System.out.println(readKeyToPrivateKey(keyFile));

  String crtFile="D:\\wx\\www.xxx.com\\Nginx\\1_www.xxx.com_bundle.crt";

  System.out.println(readCrtToPublicKey(crtFile));

  }

  }

  读取JKS

  package com.what21.demo.cert;

  import java.io.FileInputStream;

  import java.security.KeyPair;

  import java.security.KeyStore;

  import java.security.PrivateKey;

  import java.security.PublicKey;

  public class JksUtils {

  /**

  * @param storePath 秘钥库文件路径

  * @param storePasswd 秘钥库密码

  * @return

  * @throws Exception

  */

  public static KeyStore keyStore(String storePath, String storePasswd)

  throws Exception {

  KeyStore keyStore=KeyStore.getInstance("JKS");

  keyStore.load(new FileInputStream(storePath), storePasswd.toCharArray());

  return keyStore;

  }

  /**

  * @param keyStore 秘钥库

  * @param alias 证书别名

  * @param trustPasswd 证书密码

  * @return

  * @throws Exception

  */

  public static KeyPair keyPair(KeyStore keyStore, String alias, String trustPasswd)

  throws Exception {

  PrivateKey privateKey=(PrivateKey) keyStore.getKey(alias, trustPasswd.toCharArray());

  PublicKey publicKey=keyStore.getCertificate(alias).getPublicKey();

  return new KeyPair(publicKey, privateKey);

  }

  public static void main(String[] args) throws Exception {

  String jksFile="D:\\wx\\www.xxx.com\\Tomcat\\www.xxx.com.jks";

  String keystorePass="y696wyxqvsy3i";

  KeyStore keyStore=keyStore(jksFile, keystorePass);

  System.out.println(keyStore);

  // 别名为域名

  String alias="www.xxx.com";

  // 证书秘钥与秘钥库密码一致

  String trustPasswd="y696wyxqvsy3i";

  KeyPair keyPair=keyPair(keyStore, alias, trustPasswd);

  System.out.println(keyPair.getPublic());

  System.out.println(keyPair.getPrivate());

  }

  }

  读取PFX

  package com.what21.demo.cert;

  import java.io.FileInputStream;

  import java.security.KeyPair;

  import java.security.KeyStore;

  import java.security.PrivateKey;

  import java.security.PublicKey;

  public class PfxUtils {

  /**

  * @param storePath 秘钥库文件路径

  * @param storePasswd 秘钥库密码

  * @return

  * @throws Exception

  */

  public static KeyStore keyStore(String storePath, String storePasswd)

  throws Exception {

  KeyStore keyStore=KeyStore.getInstance("PKCS12");

  keyStore.load(new FileInputStream(storePath), storePasswd.toCharArray());

  return keyStore;

  }

  /**

  * @param keyStore 秘钥库

  * @param alias 证书别名

  * @param trustPasswd 证书密码

  * @return

  * @throws Exception

  */

  public static KeyPair keyPair(KeyStore keyStore, String alias, String trustPasswd)

  throws Exception {

  PrivateKey privateKey=(PrivateKey) keyStore.getKey(alias, trustPasswd.toCharArray());

  PublicKey publicKey=keyStore.getCertificate(alias).getPublicKey();

  return new KeyPair(publicKey, privateKey);

  }

  public static void main(String[] args) throws Exception {

  String pfxFile="D:\\wx\\www.xxx.com\\IIS\\www.xxx.com.pfx";

  String keystorePass="y696wyxqvsy3i";

  KeyStore keyStore=keyStore(pfxFile, keystorePass);

  System.out.println(keyStore);

  // 别名为域名

  String alias="www.xxx.com";

  // 证书秘钥与秘钥库密码一致

  String trustPasswd="y696wyxqvsy3i";

  KeyPair keyPair=keyPair(keyStore, alias, trustPasswd);

  System.out.println(keyPair.getPublic());

  System.out.println(keyPair.getPrivate());

  }

  }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PFX(Personal Exchange Format)和JKSJava KeyStore)是两种常见的证书格式,它们在存储和使用证书方面有一些区别。下面是它们的主要区别: 1. 格式: - PFXPFX是一种基于PKCS#12标准的证书格式,通常用于Windows系统。PFX文件通常包含私钥和关联的公钥证书。 - JKSJKSJava KeyStore的缩写,是Java密钥库的一种格式,用于存储密钥、证书和可信任的证书链。JKS文件通常用于Java应用程序和服务器。 2. 支持平台: - PFXPFX格式通常在Windows操作系统上使用,如IIS服务器、Exchange服务器等。 - JKSJKS格式通常在Java平台上使用,如Tomcat、Jetty等Java应用程序和服务器。 3. 存储内容: - PFXPFX文件可以包含一个或多个私钥和关联的公钥证书,通常用于在服务器上安装SSL证书。 - JKSJKS文件可以包含密钥对、证书、信任链等信息,用于存储和管理Java应用程序的密钥和证书。 4. 密码保护: - PFXPFX文件通常使用密码保护私钥和关联的公钥证书。 - JKSJKS文件也可以使用密码进行保护,以确保私钥和证书的安全性。 在使用证书时,您需要根据实际情况选择适合您的系统和应用程序的证书格式。如果您的系统是Windows平台,通常会使用PFX格式证书。如果您的应用程序是基于Java的,通常会使用JKS格式证书。 希望这些信息对您有所帮助!如果您还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值