gpg加密及CA创建颁发

99 阅读2分钟

gpg加密及CA创建颁发

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

[root@centos8  ~]#gpg --gen-key    #生成公钥/私钥对
[root@centos8  ~]#gpg --list-key   #查看生成公钥

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

[root@centos7  ~]#gpg -a --export  -o  wei.pubkey     #导出公钥
[root@centos7  ~]#scp wei.pubkey  10.0.0.8:      #拷贝公钥到centos8上
[root@centos8  ~]#gpg --gen-key            #在centos8上生成公钥/私钥对
[root@centos8  ~]#gpg -a --import  wei.pubkey      #将centos7的公钥导入
[root@centos8  ~]#gpg  -e  -r  centos7   file.txt      #用centos7的公钥加密centos8文件file,生成file.txt.gpg

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

[root@centos7  ~]#scp 10.0.0.8:/root/file.txt.gpg  .    #远程拷贝centos8file.txt.gpg文件到本机家目录
[root@centos7  ~]#gpg  -o  file.txt.bak  -d  file.txt.gpg   #使用centos7本机私钥解密并指定解密后文件为file.txt.bak,[root@centos7  ~]#gpg   -d  file.txt.gpg file.txt.bak

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

**1.创建CA相关目录和文件**

**#mkdir  -pv  /etc/pki/CA/{certs,crl,newcerts,private}**
**#tree  /etc/pki/CA**
**#touch  /etc/pki/CA/index.txt**
**#echo  0F  >  /etc/pki/CA/serial**

**2.创建CA的私钥**

**[root@centos8 ~]** **#cd /etc/pki/CA/**
**[root@centos8 CA]#(umask 066; openssl genrsa -out private/cakey.pem 2048)**

**3.给CA颁发自签名证书**
**[root@centos8 ~]#openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days** ******3650 -out     /etc/pki/CA/cacert.pem**

**#输入CA证书相关信息**
Country Name (2 letter code) [XX **]:CN**       #国家
State or Province Name (full name) []:**beijing** **#省会**
Locality Name (eg, city) [Default City] **:beijing** **#城市**
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 [] **:admin@magedu.org** **#邮件**

**#查看CA证书信息**
[root@centos8 ~] **#openssl x509 -in /etc/pki/CA/cacert.pem -noout -text**

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

**用户生成私钥和证书申请**

**#mkdir /data/app1** **#创建用户文件夹**
**#(umask 066; openssl genrsa -out   /data/app1/app1.key 2048)**  #用户生成私钥
**#openssl req -new -key /data/app1/app1.key -out** ******/data/app1/app1.csr** **#证书申请**

**#输入证书申请信息**
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:**beijing**
Locality Name (eg, city) [Default City]:**bj**
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 []:**root@magedu.org**
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

**#用户CA证书签署颁发**
**#openssl ca -in /data/app1/app1.csr -out** ******/etc/pki/CA/certs/app1.crt -days 1000******

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

**#openssl ca -revoke /etc/pki/CA/newcerts/11.pem** **#吊销证书**
**#openssl ca -gencrl -out /etc/pki/CA/crl.pem** **#生成证书吊销列表**
**#openssl crl -in /etc/pki/CA/crl.pem -noout -text** ******#查看吊销证书信息**
**#cat   /etc/pki/CA/index.txt                   #查看证书列表吊销信息**
**#openssl ca -status 11** **#查看指定编号对应证书的有效性**