https证书的生成

第一步:生成 private key、csr等文件

我们可能需要输入以下信息(交互式):

1
2
3
4
5
6
7
8
---
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:New York
Locality Name (eg, city) []:Brooklyn
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Example Brooklyn Company
Organizational Unit Name (eg, section) []:Technology Division
Common Name (e.g. server FQDN or YOUR name) []:examplebrooklyn.com
Email Address []:

 上面的信息是一行一行输入的,也可以通过使用 -subj 选项,一步完成

1
-subj "/C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=examplebrooklyn.com"

  

也可以同时生成 private key 和一个 CSR 文件:

1
2
3
openssl req \
       -newkey rsa:2048 -nodes -keyout domain.key \
       -out domain.csr

  -newkey rsa:2048 选项的意思是生成的 key 是通过 RSA 算法生成的 2048 位的。

       -nodes 私钥不需要密码加密

 

根据现有的私钥生成 CSR 文件:

1
2
3
openssl req \
       -key domain.key \
       -new -out domain.csr

  -key 指定了现有的私钥(private key)

 

根据现有的 crt 文件和 私钥生成 CSR

1
2
3
4
openssl x509 \
       -in domain.crt \
       -signkey domain.key \
       -x509toreq -out domain.csr

  -x509toreq 使用 X509 证书生成 CSR

 

 

第二步:生成 SSL 证书

生成一个私钥和一个自签名证书:

1
2
3
openssl req \
       -newkey rsa:2048 -nodes -keyout domain.key \
       -x509 -days 365 -out domain.crt

  -days 365  365天有效期

 

根据现有的私钥生成自签名证书:

1
2
3
4
openssl req \
       -key domain.key \
       -new \
       -x509 -days 365 -out domain.crt

  

 

第三步:查看证书

crt 和 csr 文件是使用 PEM 格式编码的,我们无法直接读取文件获取实际的信息。

 

检查 csr 文件里面的配置信息:

1
openssl req -text -noout -verify -in domain.csr

  

检查 crt 文件里面的配置信息:

1
openssl x509 -text -noout -in domain.crt

  

 

 

其他:

创建私钥:

1
openssl genrsa -des3 -out domain.key 2048

  

检验私钥:

1
openssl rsa -check -in domain.key

  

验证私钥是否匹配 crt 和 csr 文件:

1
2
3
openssl rsa -noout -modulus -in domain.key | openssl md5
openssl x509 -noout -modulus -in domain.crt | openssl md5
openssl req -noout -modulus -in domain.csr | openssl md5

  

加密私钥:

1
2
3
openssl rsa -des3 \
       -in unencrypted.key \
       -out encrypted.key

  

解密私钥:

1
2
3
openssl rsa \
       -in encrypted.key \
       -out decrypted.key

  

 

转换证书格式:

 

转换 PEM到 DER:

1
2
3
openssl x509 \
       -in domain.crt \
       -outform der -out domain.der

  

转换 DER 到 PEM:

1
2
3
openssl x509 \
       -inform der -in domain.der \
       -out domain.crt

  

 

转换 PEM 到 PKCS7:

可以加入一个或多个 crt 文件。

1
2
3
4
openssl crl2pkcs7 -nocrl \
       -certfile domain.crt \
       -certfile ca-chain.crt \
       -out domain.p7b

  PKCS7(P7B),被用在 java keystores 和 IIS 中,是一种可以包含 crt 和 ca 证书信息的 ASCII 文件

 

转换 PKCS7 到 PEM:

1
2
3
openssl pkcs7 \
       -in domain.p7b \
       -print_certs -out domain.crt

  

转换 PEM 到 PKCS12:

1
2
3
4
openssl pkcs12 \
       -inkey domain.key \
       -in domain.crt \
       -export -out domain.pfx

  

转换 PKCS12 到 PEM 中:

1
2
3
openssl pkcs12 \
       -in domain.pfx \
       -nodes -out domain.combined.crt

  

 

转换 PEM 到 CER:

1
openssl x509 -inform PEM -in cacert.pem -outform DER -out certificate.cer

  

原文链接:https://www.cnblogs.com/eleven24/p/7993327.html

 

安装openssl工具
centos

yum install openssl

ubuntu

apt-get install openssl

windows
从网上下载已经编译好的openssl
http://gnuwin32.sourceforge.net/packages/openssl.htm
然后将安装路径加到环境变量中

自签名证书
生成私钥和证书

openssl req -newkey rsa:2048 -keyout my.key -subj "/C=XX/O=XX/OU=XX/OU=XX/OU=XX/CN=wzj.com" -x509 -days 36500 -out my.crt

上面操作会给私钥加上密码, 可以通过参数-nodes取消密码. (为了私钥的安全性, 最好加上密码)
如果是使用windows上的gnu openssl工具, 需要指定-config ../share/openssl.cnf,

生成公钥
证书中包含了公钥, 因此直接使用证书生成

openssl x509 -in my.crt -outform PEM -out my.pem

得到了三个文件, 私钥my.key, 公钥my.pem, 证书my.crt

生成pfx文件
pfx需要私钥, 公钥, 证书

openssl pkcs12 -export -out my.pfx -inkey my.key -in my.pem -certfile my.crt

从pfx中导出公钥/私钥/证书
导出私钥

openssl pkcs12 -in my.pfx -out my2.key -nocerts

导出证书

openssl pkcs12 -in my.pfx -out my2.crt-nodes -nokeys -nokeys

生成公钥

openssl x509 -in my2.crt -outform PEM -out my2.pem

pkcs12转pkcs8
一些比较旧的系统还在使用pkcs8格式, 比如netty, 需要转换

openssl pkcs8 -in my.key -topk8 -out my.pk8
————————————————

原文链接:https://blog.csdn.net/wzj_whut/java/article/details/85715347

posted @ 2020-04-24 16:06  罐头1992  阅读(588)  评论(0编辑  收藏  举报