12.19 生成ssl密钥对

时间:2022-04-27
本文章向大家介绍12.19 生成ssl密钥对,主要内容包括生成ssl密钥对目录概要、生成ssl密钥对、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

生成ssl密钥对目录概要

  • cd /usr/local/nginx/conf
  • openssl genrsa -des3 -out tmp.key 2048//key文件为私钥
  • openssl rsa -in tmp.key -out aminglinux.key //转换key,取消密码
  • rm -f tmp.key
  • openssl req -new -key aminglinux.key -out aminglinux.csr//生成证书请求文件,需要拿这个文件和私钥一起生产公钥文件
  • openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt 这里的aminglinux.crt为公钥

生成ssl密钥对

在自己的虚拟机生成ssl 需要用到openssl工具

  • 在虚拟上颁发一套证书,生成ssl
  1. 首先得有一个openssl工具
  2. 切换到/usr/local/nginx/conf/目录下
[root@hf-01 ~]# cd /usr/local/nginx/conf/
[root@hf-01 conf]# 
  1. 若是没有openssl工具,可以安装下
  2. 查看openssl工具是由哪个安装包安装的
[root@hf-01 conf]# rpm -qf `which openssl`
openssl-1.0.2k-8.el7.x86_64
[root@hf-01 conf]# 
  1. 生成一个私钥,命令openssl genrsa -des3 -out tmp.key 2048
[root@hf-01 conf]# openssl genrsa -des3 -out tmp.key 2048
Generating RSA private key, 2048 bit long modulus
.......+++
......................................................................+++
e is 65537 (0x10001)
Enter pass phrase for tmp.key:        //输入密码
Verifying - Enter pass phrase for tmp.key:        //再次输入密码
[root@hf-01 conf]# 
  • openssl genrsa -des3 -out tmp.key 2048
    • genrsa ,表示生成rsa的私钥
    • 2048 ,2048长度
    • 名字为 tmp.key
  • 生成这个秘钥必须要有密码
  1. 在生成这个秘钥后比较麻烦,在nginx的配置文件里指定密码,每次访问浏览器,在https这个网址输入这个密码会很不方便,所以还需要去除这个密码
  2. 转换key,取消密码,命令 openssl rsa -in tmp.key -out gurui.key
  • -in 表示指定哪一个秘钥要被转换
  • -out 表示指定输出的
[root@hf-01 conf]# openssl rsa -in tmp.key -out gurui.key
Enter pass phrase for tmp.key:    //输入tmp.key的密码
writing RSA key
[root@hf-01 conf]# 
  1. 这时候tmp.key和gurui.key是属于同一个
  • tmp.key,有密码
  • gurui.key,没有密码
  1. 删除tmp.key
[root@hf-01 conf]# rm -f tmp.key
[root@hf-01 conf]# 
  1. 生成证书请求文件,需要拿这个请求文件和私钥一起生产公钥文件
[root@hf-01 conf]# openssl req -new -key gurui.key -out gurui.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn              //国家,2个字母
State or Province Name (full name) []:JiangSu      //省或州
Locality Name (eg, city) [Default City]:YanCheng   //城市
Organization Name (eg, company) [Default Company Ltd]:han  //公司
Organizational Unit Name (eg, section) []:han   //组织
Common Name (eg, your name or your server’s hostname) []:hanfeng  //您的主机名
Email Address []:han1118feng@163.com   //邮箱
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:hanfeng  //设置密码
An optional company name []:  //一个可选的公司名称
用请求证书文件和私钥文件,生成一个公钥
[root@hf-01 conf]# 
  • 这里的信息可以不用填写,直接回车也行
  1. 因为这是自己给自己颁发的证书,可以随意填写,若是购买那些正式的证书,那证书的信息就需要填写相对应的信息
  2. 生成公钥,命令openssl x509 -req -days 365 -in gurui.csr -signkey gurui.key -out gurui.crt
[root@hf-01 conf]# openssl x509 -req -days 365 -in gurui.csr -signkey gurui.key -out gurui.crt
Signature ok
subject=/C=11/ST=BEIJING/L=BeiJing/O=hanfeng/OU=hanfeng/CN=hanfeng/emailAddress=han1118femx08
Getting Private key
[root@hf-01 conf]# 
  • -days 365 证书的日期是一年
  1. gui.crt是公钥,gurui.key是私钥