加解密基础及openssl命令

2016-10-07  本文已影响0人  ckhzw

加密算法和协议:

  1. 对称加密:
    。加解密使用同一个密钥;将数据分割成固定大小的块,逐块加密;且块与块之间有关联关系;
    。算法:DES,3DES,AES,Blowfish,Twofish,IDEA,RC6,CAST5
    。缺陷:密钥过多;密钥分发成为难题;

2.公钥加密:
。密钥分为公钥和私钥;
。公钥:从私钥中提取产生;可公开给所有人;pubkey
。私钥:通过工具创建,使用者自己保留,必须保证其私密性;secret key
。用公钥加密的数据,只能用与之配对的私钥解密;私钥加密的数据,只能用与之配对的公钥解密;
。用途:
(1)数字签名:确认发送方的身份;
(2)密钥交换:发送方用对方的公钥加密一个对称密钥,并发送给对方;
。算法:RSA,DSA,ELGamal

3.单向加密:
。提取数据指纹;只能加密,不能解密;
。定长输出;雪崩效应
。作用:数据完整性验证;
。算法:md5(128bits),sha1(160bits),sha224,sha256,sha384,sha512

4.密钥交换:
(1)公钥加密:
(2)DH算法(Deffie-Hellman):

DH算法:
A与B要交换密钥:
A-->p,g;B-->p,g(p,g为大素数)
A-->x;B-->y
A:px%g-->B==>(px%g)^y
B:py%g-->A==>(py%g)^x
密钥:(px%g)y=(py%g)x

Bob和Alice安全通信过程:

。加密:
(1)A生成数据,用单向加密提取数据特征码;
(2)用A的私钥加密特征码(加密后的特征码就叫做数字签名);
(3)A生成临时对称加密钥,加密数据及特征码;
(4)获取B的公钥加密临时对称密钥;并附加在最后面;

。解密:
(1)B用自己的私钥解密对称密钥;并用它来解密获得数据和特征码;(保密性)
(2)用A的公钥解密特征码;能解密,身份认证;
(3)B用同样的单向加密算法提取数据特征码;并对比解密后的特征码;如相同,数据完整性验证;

上述过程中,存在巨大问题:如何可靠的获取对方的公钥?

PKI(Public Key Infrastructure)

(1)SSL:Secure Socket Layer
。NetScape公司研发;版本:v1/2/3

(2)TLS:Transport Layer Security
。IETF研发;版本:v1.0/1.1/1.2/1.3

SSL Handshake Protocol

发送以下信息给Server:
。发送一个随机数给Server;
。编码变更通知(表示随后的信息都将用双方商定的加密方法和密钥发送)
。客户端握手结束通知;

发送以下信息给Clinet:
。编码变更通知;
。服务器握手结束通知;

openssl:

注意:生成用户密码可简写为:openssl passwd -1 -salt $(openssl rand -hex 4)
。linux系统上的随机数生成器:
/dev/random:仅从熵池返回随机数;随机数用尽,阻塞;
/dev/urandom:从熵池返回随机数;随机数用尽,会利用软件生成伪随机数,非阻塞;(伪随机数,不安全)
熵池中随机数的来源:
(1)硬盘IO中断时间间隔
(2)键盘IO中断时间间隔

建立私有CA:

。工具:openssl
。openssl配置文件:/etc/pki/tls/openssl.cnf

vim /etc/pki/tls/openssl.cnf
[ ca ]
default_ca  = CA_default        # The default ca section

####################################################################
[ CA_default ]

dir     = /etc/pki/CA       # Where everything is kept ##工作目录
certs       = $dir/certs        # Where the issued certs are kept ##已经签发的证书存放的目录
crl_dir     = $dir/crl      # Where the issued crl are kept##吊销的证书位置
database    = $dir/index.txt    # database index file.##数据库;已经颁发的所有证书列表
#unique_subject = no            # Set to 'no' to allow creation of
                    # several ctificates with same subject.
new_certs_dir   = $dir/newcerts     # default place for new certs.

certificate = $dir/cacert.pem   # The CA certificate##CA自签证书
serial      = $dir/serial       # The current serial number##证书序列号
crlnumber   = $dir/crlnumber    # the current crl number
                    # must be 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 random number file

x509_extensions = usr_cert      # The extentions to add to the cert

default_days    = 365           # how long to certify for##证书有效期
default_crl_days= 30            # how long before next CRL
default_md  = sha256        # use SHA-256 by defaultxxx
preserve    = no            # keep passed DN ordering

1.生成私钥:
(umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 4096)

2.生成自签证书:

[root@localhost ~]# openssl req -x509 -new -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3655
//-new:生成新证书
//-x509:生成自签证书;CA服务器专用
//-key:指明私钥文件的路径
//-out:生成的请求文件路径;如果自签将直接生成签署过的证书

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) []:shanghai
Locality Name (eg, city) [Default City]:shanghai
Organization Name (eg, company) [Default Company Ltd]:ops
Organizational Unit Name (eg, section) []:magedu
Common Name (eg, your name or your server's hostname) []:www.magedu.com
Email Address []:acadmin.magedu.com

3.为CA提供所需的目录及文件:
mkdir /etc/pki/CA/{certs,crl,newcerts}
touch /etc/pki/CA/{serial,index.txt}
echo 01 > /etc/pki/CA/serial

要用到证书进行安全通信的主机,要向CA服务器请求签署证书:

以http服务为例:
(1)要用到证书的主机生成私钥:
mkidr /etc/httpd/ssl
cd /etc/httpd/ssl;(umask 077;openssl genrsa -out httpd.key 2048)
(2)生成证书签署请求:
openssl req -new -key /etc/httpd/ssl/httpd.key -out httpd.csr -days 365
(3)将httpd.csr通过有效途径发送给CA服务器(scp)
(4)在CA服务器上签署证书:
openssl ca -in xxx/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
(5)将httpd.crt通过有效途径发送给请求证书的主机:
scp httpd.crt root@172.16.18.130:/etc/httpd/ssl

上一篇 下一篇

猜你喜欢

热点阅读