天天看点

PKI架构的简介,如何使用OPENSSL完成加密与解密,如何自建CA完成证书的签署

        PKI(Public Key Infrastructure ) 即"公钥基础设施",是一种遵循既定标准的密钥管理平台,它能够为所有网络应用提供加密和数字签名等密码服务及所必需的密钥和证书管理体系。

        认证中心CA 作为PKI 的核心部分,CA 实现了PKI 中一些很重要的功能,概括地说,认证中心(CA)的功能有:证书发放、证书更新、证书撤销和证书验证。CA 的核心功能就是发放和管理数字证书,具体描述如下: 

(1)接收验证最终用户数字证书的申请。 

(2)确定是否接受最终用户数字证书的申请-证书的审批。 

(3)向申请者颁发、拒绝颁发数字证书-证书的发放。 

(4)接收、处理最终用户的数字证书更新请求-证书的更新。 

(5)接收最终用户数字证书的查询、撤销。 

(6)产生和发布证书废止列表(CRL)。 

(7)数字证书的归档。 

(8)密钥归档。 

(9)历史数据归档。

我们先来看看如何使用OPENSSL对数据进行加密与解密:

openssl enc -ciphername(算法名称)

    -in filename

        the input filename, standard input by default. 输入文件名,标准的默认输入。

    -out filename

        the output filename, standard output by default. 输出文件名,标准的默认输出。

    -e 

        encrypt the input data: this is the default. 默认加密输入的数据。

    -d

        decrypt the input data. 解密输入的数据。

现在我们对/tmp下的fstab文件进行加密:

# openssl idea-ofb -in fstab -e -out fstab.secret

这样就对文件完成加密了,目录下是不是多了一个fstab.secret这个就是加密后的文件。

我们看看加密文件的内容:

# cat fstab.secret

全身乱码看不懂吧,这时我们把源文件fstab删除掉,这样就很好的起到了保密工作。

# rm -f fstab

现在我们用这个加密的文件,把我们源文件给还原回来。

# openssl idea-ofb -in fstab.secret -d -out fstab

文件回来了吧,而且内容也正常。

其他扩展的openssl命令:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<code>openssl: </code>

<code>    </code><code># openssl version</code>

<code>    </code> 

<code>    </code><code># 加密文件</code>

<code>     </code><code>openssl enc -des3 -</code><code>in</code> <code>/path/to/somefile</code> <code>-e -out </code><code>/path/to/somefile</code><code>.des3</code>

<code>    </code><code># 解密文件 </code>

<code>     </code><code>openssl enc -des3 -</code><code>in</code> <code>/path/to/somefile</code><code>.des3 -d -out </code><code>/path/to/somefile</code>

<code>    </code><code># 一致性校验</code>

<code>     </code><code>openssl dgst -md5 -hex </code><code>/path/to/somefile</code>

<code>     </code><code>md5sum </code>

<code>        </code> 

<code>        </code><code># 评估加密算法的加密速度 </code>

<code>     </code><code>openssl speed </code>

<code>        </code><code># 加密密码  -salt 参数是在加密前给密码加上其他字符一并加密</code>

<code>     </code><code>man</code> <code>sslpasswd</code>

<code>     </code><code>openssl </code><code>passwd</code> <code>-1 -salt </code>

<code>    </code><code># 生成随机密码</code>

<code>     </code><code>openssl rand -base64 num</code>

<code>     </code><code>openssl rand -hex num</code>

如何使用openssl生成一个私钥:

openssl genrsa -out filename [字符位数]

        generate an RSA private key  生成一个RSA 私钥。

我们现在来生成一个2048位的私钥:

# openssl genrsa -out mytckey 2048

# chmod 600 mytckey

私钥是不能随便让人看的,所以改下权限,除了自己,所有人都不能看。

提取公钥:

# openssl rsa -in mytckey  -pubout

制作一个证书的签署请求命令介绍:

openssl

    req

        PKCS#10 X.509 Certificate Signing Request (CSR) Management. 证书签署请求管理。

    -new

        this option generates a new certificate request. 这个选项是生成一个新证书的请求。

    -key

        This specifies the file to read the private key from. It also accepts PKCS#8 format private keys for PEM format files.  这是指定读取的私钥在哪,也支持PKCS#8格式的私钥,PEM格式的文件。

    -out

        This specifies the output filename to write to or standard output by default  指定输出文件名

开始制作:

# openssl req -new -key mytckey  -out myreq.csr

接下来要输入所在国家,省份,城市,部门等信息。

这就创建好了一个证书的签署请求。

CA才可以签署证书

所以我们现在来看下,如何自建CA。

openssl配置文件CA的部分配置:

# cd /etc/pki/CA/

1、为CA生成一个私钥:

# (umask 077; openssl genrsa -out private/cakey.pem 4096)

 # openssl req -new -x509 -key private/cakey.pem  -out cacert.pem -days 3656 

自签证书

# touch serial index.txt

# echo 01 &gt; serial

创建签证数据库文件,和序列号文件,并且给serial一个初始序列。

CA自建好了,现在可以给别人签证了。

我们先来签一个刚刚我们刚刚做好的请求

# openssl ca -in myreq.csr  -out mycert.crt -days 3655

这就给别人签好了一张证书。

 接下来,我们在脚本里实现自建CA和签署证书的操作:

以下是小菜写的脚本:

这一块内容完成,有问题欢迎与我交流QQ1183710107。

本文转自qw87112 51CTO博客,原文链接:http://blog.51cto.com/tchuairen/1415425

继续阅读