zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    EXCEL 函数手册
    php 总结(11)php常用整理
    php 总结(10) PDO 连接数据库 预处理
    Dedecms 常用替换sql命令
    php 总结(9) mysql 字段 编码 相关
    解析XML【C#】
    shell安全脚本
    mysql --secure-file-priv写入文件的说明
    Typora使用技巧及markdown语法
    F-MiddlewareScan中间件扫描工具简单试用
  • 原文地址:https://www.cnblogs.com/eleven24/p/7993327.html
Copyright © 2011-2022 走看看