加密解密基础、PKI及SSL、建立私有CA

加密解密基础node


加密解密的技术:web

对称加密算法

加密方和解密方使用是同一个密钥,加密解密的速度都很快,先将数据明文安全

分红数据块儿,通常来说是大小相同的,若是到最后剩下的不能与其余数据块儿的网络

大小相同,那么就给它添加一些填充物,而后对每一个数据块儿逐个加密,dom

而后把加密后的数据块儿发给对方,每一次管理一起,ide

可是,加密后的块儿怎么处理,由于每个块儿都是单独处理,对方在破解数据时工具

每一起独立破解,也就是说这样的加密过程对反破解并无任何帮助,测试

对于加密之后的数据块儿的处理有如下两种方法ui

ECB:每一起单独加密,加密一个传递一个,

CBC:加密或密文块儿链,经过抑或运算实现,每个数据块儿,在发送给对方

以前会实现将此数据块儿与此前的数据块儿作一次抑或运算,并把结果发送给对方

因此得不到第一个块儿,获得其余也就没有用,即便是第一块,也会和一个随机数

进行抑或运算

其最大好处在于,作两次抑或运算后能够将数据还原

 

算法:DES:数据加密标准,使用56位的密钥长度

AES:高级加密标准,可使用12八、19二、256三种长度的密钥

3DES:对原有加密3次,

Blowfish

Twofish

RC6

IDEA

CAST5

缺陷:一、一我的跟众多对象通讯的时候须要记的密码过多

二、密钥分发困难,是最大的难题,没有一种可靠的手段将密钥送给一个

没有见过面的对象

 

非对称加密

公钥加密算法:DSA,RSA,EIGamal

加密方和解密方使用不一样的密钥

功能:加密解密

用户的身份认证,RSA二者均可以实现,而DSA只能加密数据

公钥,私钥

公钥是从私钥中抽出的一段特征,公钥隐含在私钥中

如今主流的密钥长度是2048




PKI


PKI技术与安全服务

公钥基础设施(Public Key Infrastructure,简称PKI)是一种遵循标准的,采用非对称密码算法和技术来实现并提供安全服务的,通用的安全基础设施。它可以透明地为网络应用提供加密和数字签名所必需的密钥和证书管理。PKI体系其实是计算机软硬件、权威机构及应用系统的结合。

PKI体系结构:

一个基本的PKI系统由认证中心(CA )、注册中心(RA)、密钥管理中心(KMC)、证书发布系统几个核心组件组成。其中CA认证中心做为电子证书认证系统的核心,负责全部证书的签发、注销以及证书注销列表的签发等管理功能。RA注册中心是认证中心证书发放、管理等业务的延伸。它负责全部证书申请者的信息录入、审核等工做,同时对发放的证书进行管理。密钥管理中心(KMC)为CA Server提供用户加密密钥的生成及管理服务。证书发布系统提供颁发证书和CRL证书撤销列表的发布及查询服务,其服务方式包括LDAP目录发布、Web发布及OCSP线证书状态查询服务等。




SSL


首先说一下什么是SSL

1. 为网络通讯提供安全及数据完整性的一种安全协议,囊括了主要的密码算法、经常使用的密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用;

2. OpenSSL只是OpenSSL开源套件中的多功能命令工具;

3. OpenSSL套件的组成部分有:

libcrypto:通用功能的加密库

libssl:用于实现TSL/SSL功能的库;

OpenSSL:多功能命令工具


OpenSSL:

三个组件:

openssl: 多用途的命令行工具;

libcrypto: 加密解密库;

libssl:ssl协议的实现;




建立私有CA

CA和申请证书都在同一台机器上进行

一、CA端操做流程
操做步骤:
一、生成私钥文件;      
二、生成自签署证书;        
三、相关辅助文件
touch/etc/pki/CA/index.txt
echo01 > /etc/pki/CA/serial
再次说明:
(1)私钥用于签发证书时,向证书添加数字签名使用;
(2)证书:每一个通讯方都导入此证书至“受信任的证书颁发机构”;
1)、配置文件
  /etc/pki/tls/openssl.cnf,此文件格式为INI格式的
………省略……………
[ ca ]
default_ca      =CA_default        # The default casection

[ CA_default ]  #定义Openssl作为CA时本身的相关路径
 
dir            =/etc/pki/CA # Where everythingis kept  #指明CA本身的工做目录
certs          =$dir/certs  # Where the issuedcerts are kept #已颁发证书的存取位置
crl_dir        =$dir/crl    # Where the issuedcrl are kept #已吊销证书的吊销链表存取位置
database        =$dir/index.txt  # database indexfile.  #证书索引文件
#unique_subject = no          # Set to 'no' to allowcreation of
#several ctificates with same subject.
new_certs_dir  =$dir/newcerts    # default place fornew certs.  #新证书的存放位置
    
certificate    =$dir/cacert.pem  # The CAcertificate    #指CA本身证书文件名
serial          =$dir/serial  # The currentserial number #序列号,下一个要发的证书的序列号,每签一个序列号会怎么加1
crlnumber      =$dir/crlnumber  # the current crlnumber  #吊销证书列表的编号
# mustbe commented out to leave a V1 CRL
crl            =$dir/crl.pem # The current CRL  #当前正在使用的吊销链
private_key    =$dir/private/cakey.pem# The private key  #CA本身的私钥文件
RANDFILE        =$dir/private/.rand # private randomnumber file #随机数的获取位置
 
x509_extensions = usr_cert        # The extentions to add to thecert #定义了x.509的扩展信息中记录为“用户证书”
 
…………省略……………………

2)、工做目录
/etc/pki/CA/
注意:CentOS要想构建CA必须是基于/etc/pki/CA目录进行
[root@node-2 ~]# cd /etc/pki/CA
[root@node-2 CA]# ll
total 16
drwxr-xr-x  2 root root4096 Oct 15  2014 certs        #证书文件存放位置,
drwxr-xr-x  2 root root4096 Oct 15  2014 crl          #证书吊销列表存取文件
drwxr-xr-x. 2 root root 4096 Oct 15  2014 newcerts      #新建立证书存取位置
drwx------. 2 root root 4096 Oct 15  2014 private        #CA本身私钥存储位置

3)、建立CA本身的私钥
[root@node-2 CA]# pwd
/etc/pki/CA
[root@node-2 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit longmodulus
........................................................+++
..............+++
e is 65537 (0x10001)
[root@node-2 CA]# ll ./private/
total 4
-rw------- 1 root root 1675 Apr 22 22:23cakey.pem

4)、生成一个自签署证书
此自签署证书在/etc/pki/tls/openssl.cnf文件中定义必须放在CA目录下叫cacert.pem;从私钥中提取公钥,并构建成证书签署请求,而后让CA给签署;
格式:openssl req -new -x509-key  /etc/pki/CA/private/cakey.pem -out/etc/pki/CA/cacert.pem -days #
-x509:表示自签署证书,不加表示证书签署请求;
-key:从那个私钥中自动提取出公钥,并建立一个证书签署请求;
-days:指明给本身签署证书的有效期
12345678910111213141516171819202122 [root@node-2 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days3360
You are about to be asked to enterinformation that will be incorporated
into your certificate request.
What you are about to enter is what is calleda Distinguished Name or a DN.
There are quite a few fields but you canleave some blank
For some fields there will be a defaultvalue,
If you enter '.', the field will be leftblank.
-----
Country Name (2 letter code) [XX]:CN            #国家代码,必须使用两位数字来描述
State or Province Name (full name) []:HA          #省名或州名
Locality Name (eg, city) [Default City]:ZZ          #城市名
Organization Name (eg, company) [DefaultCompany Ltd]:MagEdu  #组织名或公司名
Organizational Unit Name (eg, section)[]:Ops    #部门
Common Name (eg, your name or your server'shostname) []:www.magedu.com  #此项很重要,证书主题本身的名字,别人和你通讯是就使用此名字来联系你,然后你证书中的名字和通讯时所使用名字必需要保持一致;不然就认为证书验证没法经过;
Email Address []:caadmin@magedu.com                    #主机的联系人
[root@node-2 CA]# ll
total 20
-rw-r--r--  1 root root 1395 Apr 22 22:40 cacert.pem
drwxr-xr-x  2 root root4096 Oct 15  2014 certs
drwxr-xr-x  2 root root4096 Oct 15  2014 crl
drwxr-xr-x. 2 root root 4096 Oct 15  2014 newcerts
drwx------. 2 root root 4096 Apr 22 22:23 private

注意:构建Ca是给通讯方签发证书的

5)、生成相关的辅助文件
[root@node-2 ~]# cd /etc/pki/CA/
[root@node-2 CA]# ls
cacert.pem certs  crl  newcerts private
[root@node-2 CA]# touch index.txt
[root@node-2 CA]# echo 01 > serial
[root@node-2 CA]# cat serial
01

二、给节点发证书
操做步骤:
一、节点申请证书
在证书申请的主机上进行以下步骤;
(1)生成私钥
(2)生成证书签署请求;              
(3)把请求发送给CA;
二、CA签发证书
(1)验证请求者信息
(2)签署证书
(3)把签署好的证书发还给请求者
1)、以http目录为示例,本机给本地签署证书:
123 [root@node-2 ~]# cd /etc/httpd/
[root@node-2 httpd]# mkdir ssl
[root@node-2 httpd]# cd ssl/

2)、生成节点私钥:
12345678 [root@node-2 ssl]# (umask 077; openssl genrsa -out httpd.key 1024)
Generating RSA private key, 1024 bit longmodulus
....................++++++
......++++++
e is 65537 (0x10001)
[root@node-2 ssl]# ll
total 4
-rw------- 1 root root 891 Apr 23 08:19httpd.key
 
3)、生成签署证书请求:
[root@node-2 ssl]# openssl  req -new -key httpd.key-out httpd.csr
You are about to be asked to enterinformation that will be incorporated
into your certificate request.
What you are about to enter is what is calleda Distinguished Name or a DN.
There are quite a few fields but you canleave some blank
For some fields there will be a defaultvalue,
If you enter '.', the field will be leftblank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [DefaultCompany Ltd]:MagEdu
Organizational Unit Name (eg, section)[]:Ops
Common Name (eg, your name or your server'shostname) []:www.zh.com  #此名称要和证书申请者的FQDN的保持一致
Email Address []:webadmin@zh.com
 
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@node-2 ssl]# ls
httpd.csr httpd.key
#提示:以上的国家、省名、城市名要和CA的保持一致
三、CA签署证书
[root@node-2 ssl]# openssl ca -in /etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt-days 3360
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:Apr 23 00:30:38 2015 GMT
Not After :Jul  4 00:30:38 2024 GMT
Subject:
countryName          = CN
stateOrProvinceName      = HA
organizationName        = MagEdu
organizationalUnitName      = Ops
commonName            =www.zh.com
emailAddress          =webadmin@zh.com
X509v3 extensions:
X509v3 BasicConstraints:
CA:FALSE
NetscapeComment:
OpenSSLGenerated Certificate
X509v3 SubjectKey Identifier:
C9:1A:C2:19:08:28:FF:1F:B4:85:90:32:00:E1:5B:E8:FF:6E:7E:95
X509v3Authority Key Identifier:
keyid:66:1E:36:DB:E7:40:30:ED:B1:04:24:86:11:08:73:0C:DF:5E:DD:52
 
Certificate is to be certified until Jul  4 00:30:38 2024 GMT (3360 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

操做过程遇到如下问题1:[root@node-2 ssl]# openssl ca -in/etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt -days 3360Using configuration from /etc/pki/tls/openssl.cnf/etc/pki/CA/index.txt:No such file or directory  #没有/etc/pki/CA/index.txt文件unable to open '/etc/pki/CA/index.txt'139960522676040:error:02001002:systemlibrary:fopen:No such file or directory:bss_file.c:398:fopen('/etc/pki/CA/index.txt','r')139960522676040:error:20074002:BIOroutines:FILE_CTRL:system lib:bss_file.c:400:操做过程遇到如下问题2:[root@node-2 CA]# openssl ca -in/etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt -days 3360Using configuration from/etc/pki/tls/openssl.cnf/etc/pki/CA/serial:No such file or directory      #没有/etc/pki/CA/serial文件error while loading serial number140170056320840:error:02001002:systemlibrary:fopen:No such file ordirectory:bss_file.c:398:fopen('/etc/pki/CA/serial','r')140170056320840:error:20074002:BIOroutines:FILE_CTRL:system lib:bss_file.c:400:四、查看节点所生成的文件:[root@node-2 ~]# cd /etc/httpd/ssl/[root@node-2 ssl]# lltotal 8-rw-r--r-- 1 root root  0 Apr 23 19:47 httpd.crt      #证书文件-rw-r--r-- 1 root root 676 Apr 23 08:22  httpd.csr      #证书请求文件-rw------- 1 root root 891 Apr 23 08:19  httpd.key      #私钥文件

相关文章
相关标签/搜索