openssl生成https证书、转换证书格式的各种相关操作

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

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

---
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 选项,一步完成

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

  

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

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

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

       -nodes 私钥不需要密码加密

 

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

openssl req \
       -key domain.key \
       -new -out domain.csr

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

 

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

openssl x509 \
       -in domain.crt \
       -signkey domain.key \
       -x509toreq -out domain.csr

  -x509toreq 使用 X509 证书生成 CSR

 

 

第二步:生成 SSL 证书

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

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

  -days 365  365天有效期

 

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

openssl req \
       -key domain.key \
       -new \
       -x509 -days 365 -out domain.crt

  

 

第三步:查看证书

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

 

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

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

  

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

openssl x509 -text -noout -in domain.crt

  

 

 

其他:

创建私钥:

openssl genrsa -des3 -out domain.key 2048

  

检验私钥:

openssl rsa -check -in domain.key

  

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

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

  

加密私钥:

openssl rsa -des3 \
       -in unencrypted.key \
       -out encrypted.key

  

解密私钥:

openssl rsa \
       -in encrypted.key \
       -out decrypted.key

  

 

转换证书格式:

 

转换 PEM到 DER:

openssl x509 \
       -in domain.crt \
       -outform der -out domain.der

  

转换 DER 到 PEM:

openssl x509 \
       -inform der -in domain.der \
       -out domain.crt

  

 

转换 PEM 到 PKCS7:

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

openssl crl2pkcs7 -nocrl \
       -certfile domain.crt \
       -certfile ca-chain.crt \
       -out domain.p7b

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

 

转换 PKCS7 到 PEM:

openssl pkcs7 \
       -in domain.p7b \
       -print_certs -out domain.crt

  

转换 PEM 到 PKCS12:

openssl pkcs12 \
       -inkey domain.key \
       -in domain.crt \
       -export -out domain.pfx

  

转换 PKCS12 到 PEM 中:

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

  

 

转换 PEM 到 CER:

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

  

转载于:https://www.cnblogs.com/eleven24/p/7993327.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值