作业一

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

[root@centos7 ~]# 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.

gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
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?
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: zhaoyang
Email address: 987678498@qq.com
Comment:
You selected this USER-ID:
"zhaoyang <987678498@qq.com>"

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: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 9B02778E 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/9B02778E 2020-09-06
Key fingerprint = FA21 208B 23CA 2C22 95E8 3794 CDB6 ED83 9B02 778E
uid zhaoyang <987678498@qq.com>
sub 2048R/E9D08690 2020-09-06

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

[root@centos7 ~]# gpg -a --export -o zhao.pubkey
[root@centos7 ~]# scp zhao.pubkey 10.0.0.133:
The authenticity of host '10.0.0.133 (10.0.0.133)' can't be established.
ECDSA key fingerprint is SHA256:YoOzFpGvLCEKkNwtyTqWMSroTT8mEAEP1qg520/npNw.
ECDSA key fingerprint is MD5:11:9b:e7:93:e4:63:a5:67:b2:9e:a2:37:56:3e:62:b0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.133' (ECDSA) to the list of known hosts.
root@10.0.0.133's password:
zhao.pubkey 100% 1711 1.8MB/s 00:00

[root@centos8 ~]# cp /etc/fstab .
[root@centos8 ~]# gpg --import zhao.pubkey
gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key CDB6ED839B02778E: public key "zhaoyang <987678498@qq.com>" imported
gpg: Total number processed: 1
gpg: imported: 1
[root@centos8 ~]# gpg -e -r zhaoyang fstab
gpg: 4E582E88E9D08690: There is no assurance this key belongs to the named user
sub rsa2048/4E582E88E9D08690 2020-09-06 zhaoyang <987678498@qq.com>
Primary key fingerprint: FA21 208B 23CA 2C22 95E8 3794 CDB6 ED83 9B02 778E
Subkey fingerprint: 8FAB BE45 D236 852A 7E2E 302E 4E58 2E88 E9D0 8690

It is NOT certain that the key belongs to the person named
in the user ID. If you *really* know what you are doing,
you may answer the next question with yes.

Use this key anyway? (y/N) y

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

[root@centos7 ~]# scp 10.0.0.133:/root/fstab.gpg .
root@10.0.0.133's password:
fstab.gpg 100% 736 893.0KB/s 00:00
[root@centos7 ~]# gpg -o fstab -d fstab.gpg

You need a passphrase to unlock the secret key for
user: "zhaoyang <987678498@qq.com>"
2048-bit RSA key, ID E9D08690, created 2020-09-06 (main key ID 9B02778E)

gpg: encrypted with 2048-bit RSA key, ID E9D08690, created 2020-09-06
"zhaoyang <987678498@qq.com>"

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

[root@centos7 ~]# cd /etc/pki/CA/
[root@centos7 CA]# touch index.txt
[root@centos7 CA]# echo 01 > serial
[root@centos7 CA]# (umask=066;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
......+++
.........................................................................................................................................................................+++
e is 65537 (0x10001)
[root@centos7 CA]# openssl req -new -x509 -key private/cakey.pem -days 3650 -out 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) []:henan
Locality Name (eg, city) [Default City]:zhengzhou
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:devops
Common Name (eg, your name or your server's hostname) []:ca.magedu.org
Email Address []:

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

[root@centos7 CA]# mkdir /data/app1
[root@centos7 CA]# (umask 066;openssl genrsa -out /data/app1/app1.key 2048)
Generating RSA private key, 2048 bit long modulus
........+++
...............+++
e is 65537 (0x10001)
[root@centos7 CA]# openssl req -new -key /data/app1/app1.key -out /data/app1/app1.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) []:henan
Locality Name (eg, city) [Default City]:zhengzhou
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:app1.magedu.org
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@centos7 CA]# openssl ca -in /data/app1/app1.csr -out certs/app1.crt -days 1000
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Sep 6 00:53:11 2020 GMT
Not After : Jun 3 00:53:11 2023 GMT
Subject:
countryName = CN
stateOrProvinceName = henan
organizationName = magedu
organizationalUnitName = it
commonName = app1.magedu.org
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
25:A8:77:FE:2E:D4:D9:E0:7A:ED:DB:7E:96:00:F0:71:D8:96:DF:A4
X509v3 Authority Key Identifier:
keyid:2D:D8:B9:62:B5:D9:E2:9D:AB:FB:EB:13:B2:14:47:53:45:51:E5:87

Certificate is to be certified until Jun 3 00:53:11 2023 GMT (1000 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

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

[root@centos7 CA]# openssl ca -revoke newcerts/01.pem
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 01.
Data Base Updated

[root@centos7 CA]# echo 01 > crlnumber
[root@centos7 CA]# openssl ca -gencrl -out crl.pem
Using configuration from /etc/pki/tls/openssl.cnf
[root@centos7 CA]# cat crlnumber
02

posted @ 2020-09-06 09:43  Zintent  阅读(146)  评论(0)    收藏  举报