马哥教育第一次作业(密钥与 CA)

文章目录

    • 一、在 CentOS7 中使用 gpg 创建 RSA 非对称密钥对
    • 二、将 CentOS7 导出的公钥,拷贝到 CentOS8 中,在 CentOS8 中使用 CentOS7 的公钥加密一个文件
    • 三、回到 CentOS7 服务器,远程拷贝 file.txt.gpg 文件到本地,使用 CentOS7的私钥解密文件
    • 四、在 CentOS7 中使用 openssl 软件创建 CA
    • 五、 在 CentOS7 中使用 openssl 软件创建一个证书申请请求文件,并使用上面的跟证书对其进行签署
    • 六、吊销已经签署成功的证书

一、在 CentOS7 中使用 gpg 创建 RSA 非对称密钥对

实验步骤大纲:
创建 RSA 非对称密钥对需要两步
1. 创建 RSA 非对称密钥对
2. 查看是否创建成功
`1. 创建 RSA 非对称密钥对`

# 在创建时需要输入密码,输入的密码是对称密钥对私钥的加密,可以输入,也可以不输入
[root@centos7 filecode]# gpg --gen-key
gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 1			<-------------- # 选择 rsa 密钥进行加密
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 		<-------------- # 密钥位数选择
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) <-------------- # 密钥有效期
Key does not expire at all
Is this correct? (y/N) y

GnuPG needs to construct a user ID to identify your key.

Real name: chenchen	<-------------- # 真实名称
Email address: 	<-------------- # 邮箱地址
Comment: 	<-------------- # 注释
You selected this USER-ID:
    "chenchen"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.

We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: key CE878036 marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/CE878036 2020-09-06
      Key fingerprint = 2284 6905 355C 4FCD 1033  B0EA 404A D4B3 CE87 8036
uid                  chenchen
sub   2048R/C4A93B79 2020-09-06
`2. 查看是否创建成功`

[root@centos7 filecode]# gpg --list-keys
/root/.gnupg/pubring.gpg	<-------------- # 密钥存储位置 
------------------------
pub   2048R/CE878036 2020-09-06	<-------------- # 公钥创建时间
uid                  chenchen	<-------------- #  创建用户名称
sub   2048R/C4A93B79 2020-09-06	<-------------- # 创建私钥时间

二、将 CentOS7 导出的公钥,拷贝到 CentOS8 中,在 CentOS8 中使用 CentOS7 的公钥加密一个文件

实验步骤大纲:
1. 将 Centos 7 的公钥导出
2. 拷贝到 Centos 8 中
3. 在 Centos 8 中使用 Centos 7 公钥加密文件
`1. 将 Centos 7 的公钥导出`
# 导出公钥文件名为 centos7.pub
[root@centos7 filecode]# gpg -a --export -o centos7.pub
-a		# 导出 ascii 格式公钥
--export	# 导出公钥的关键字
-o		# 导出文件名称
`2. 拷贝到 Centos 8 中`
[root@centos7 filecode]# scp centos7.pub centos8:/filecode		# 此主机名通过 hosts 文件解析
`3. 在 Centos 8 中使用 Centos 7 公钥加密文件`
# 创建文件
[root@centos8 filecode]# echo hello,my name is centos8 > hello.txt
[root@centos8 filecode]# cat hello.txt 
hello,my name is centos8
# 导入公钥
[root@centos8 filecode]# gpg --import centos7.pub 
# 加密文件
[root@centos8 filecode]# gpg -e -r chenchen hello.txt
# 生成加密文件
[root@centos8 filecode]# ll hello.txt.gpg hello.txt
-rw-r--r-- 1 root root  25 Sep  6 01:44 hello.txt
-rw-r--r-- 1 root root 363 Sep  6 01:46 hello.txt.gpg

三、回到 CentOS7 服务器,远程拷贝 file.txt.gpg 文件到本地,使用 CentOS7的私钥解密文件

实验步骤大纲:
1. 使用 scp 复制文件到本地
2. 使用私钥解密文件
`1. 使用 scp 复制文件到本地`
[root@centos8 filecode]# scp hello.txt.gpg centos7:/filecode
`2. 使用私钥解密文件`
[root@centos7 filecode]# gpg --decrypt hello.txt.gpg          

You need a passphrase to unlock the secret key for
user: "chenchen"
2048-bit RSA key, ID 612635A8, created 2020-09-06 (main key ID 15FB8F96)

gpg: encrypted with 2048-bit RSA key, ID 612635A8, created 2020-09-06
      "chenchen"
hello,my name is centos8

四、在 CentOS7 中使用 openssl 软件创建 CA

实验步骤大纲:
1. 创建 CA 私钥
2. 创建 CA 自签证书
因为  Centos 7 自带证书文件以及必须目录,所以就不需要创建了,如果是 Centos 8 是需要创建的
`1. 创建 CA 私钥`
[root@centos7 CA]# cd /etc/pki/CA/
[root@centos7 CA]# (umask 066;openssl genrsa -ou private/cakey.pem 2048)
`2. 创建 CA 自签证书`
[root@centos7 CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 3650 -out /etc/pki/CA/cacert.pem 
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
State or Province Name (full name) []:shandong
Locality Name (eg, city) [Default City]:taian
Organization Name (eg, company) [Default Company Ltd]:ali
Organizational Unit Name (eg, section) []:devops   
Common Name (eg, your name or your server's hostname) []:www.dingchen.club
Email Address []:[email protected]            

五、 在 CentOS7 中使用 openssl 软件创建一个证书申请请求文件,并使用上面的跟证书对其进行签署

实验步骤大纲:
1. 生成证书请求
2. 颁发证书
`1. 生成证书请求`
[root@centos7 CA]# openssl req -new -key /filecode/app.key -out /filecode/app.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
State or Province Name (full name) []:shandong
Locality Name (eg, city) [Default City]:taian
Organization Name (eg, company) [Default Company Ltd]:ali
Organizational Unit Name (eg, section) []:devops
Common Name (eg, your name or your server's hostname) []:www.dingchen.club
Email Address []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:       
An optional company name []:
`2. 颁发证书`
[root@centos7 CA]# openssl ca -in /filecode/app.csr -out /etc/pki/CA/certs/app.crt -days 365

六、吊销已经签署成功的证书

实验步骤大纲:
1. 查看证书编号
2. 吊销证书
`1. 查看证书编号`
[root@centos7 CA]# openssl x509 -in /etc/pki/CA/certs/app.crt -noout -serial -subject
serial=04
subject= /C=CN/ST=shandong/O=ali/OU=devops/CN=www.dingchen.club/emailAddress=[email protected]
`2. 吊销证书`
[root@centos7 CA]# openssl ca -revoke /etc/pki/CA/newcerts/04.pem 
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 04.
Data Base Updated

你可能感兴趣的:(马哥教育,openssl,ssl,密码学,运维,服务器)