加密与解密以及OpenSSL的应用

本文主要简单介绍下;

一、数据加密的三种方式;

二、Openssl的基础应用以及创建CA证书与发证;

一、数据加密的三种方式;

1、对称加密;加密与解密方使用同一个算法, 将数据切割成数据块逐步加密,并且前后块之间有关联关系,被称为块量,解密就是拿到块量做运算,进行解密。

常用的对称加密算法有:

DES(56bits), 3DES, AES(128bits), Blowfish

Twofish, IDEA, RC6, CAST5, Serpent

特点;加密与解密使用同一个口令

      将原文分割成固定大小数据块,对这些数据块进行加密


      加密数度快


缺点;口令传输,安全性不高

      口令太多,不易于记忆

 

2、公钥加密(也称非对称加密);与对称加密算法的主要差别在于,加密和解密的密钥不相同,一个公开(公钥),一个保密(私钥)。解决了对称加密算法密钥分配管理的问题,提高了算法安全性。

非对称算法有;RSA、EIGamal、DSA

特性;发送方数据传输时是用接收方的公钥加密,接收方用自己的私钥解密。

      身份认证是发送方用自己的私钥加密,接收方用对方的公钥解密以此来保证数据安全性。

缺点;非对称加密算法的加密、解密的效率比较低。

 


3、单向加密,对数据按照一定和算法生成特征码,具有不可逆性。

抽取数据特征码的算法有;MD5, SHA1, SHA512, CRC-32

消息认证算法;CBC-MAC、HMAC

特性;

消息认证;只用于验证消息本身,需要加密特征码。

雪崩效应;输入数据的微小改变,会导致结果大的变化。

定长输出;无任输入数据多长,只要使用同一种单向加密算法,输出结果的长度都相同。

结合以上三种加密方法:如下图所示;

加密与解密以及OpenSSL的应用

上图所示,把三种加密算法结合起来使用,对称加密实现了数据的机密性,公钥加密实现身份认证,而单向加密则实现了数据的完整性。

二、Openssl的基础应用以及创建CA证书与发证;

1)、Openss是一套用于SSL(安全套接字层)/TLS(传输层安全)协议的加密工具,有如下三个组件组成;

    1、libcrypto;通用功能加密库;

    2、libssl;用于实现SSL/TLS的功能

    3、多功能命令工具

其作用还可以生成密钥、创建数字证书、计算信息摘要、手动加密解密数据。

2)、对称加密以及解密的方法;

常用算法有:DES,3DES,AES,Blowfish,Twofish,RC6,CAST5


加密; openssl enc -des3 -a -salt -in /etc/issue -out /tmp/issue_cipher
[root@localhost ~]# cat /etc/issue  没加密前的内容
CentOS release 6.5 (Final)
Kernel \r on an \m
[root@localhost ~]# openssl enc -des3 -a -salt -in /etc/issue -out /tmp/issue_cipher
enter des-ede3-cbc encryption password:    输入密码
Verifying - enter des-ede3-cbc encryption password:  再次输入
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# ls
issue_cipher  yum.log
[root@localhost tmp]# cat issue_cipher      查看加密后文件内容
U2FsdGVkX1+A3cLqRI09pTWDT6BhqierBK69evESmUcH9SOHUaA+0nw87hM5sDCT
2/PlBNgiqTMiiKelkoAyBw==
解密;openssl enc -d -des3 -a -salt -in /tmp/issue_cipher -out /mnt/issue
[root@localhost ~]# openssl enc -d -des3 -a -salt -in /tmp/issue_cipher -out /mnt/issue
enter des-ede3-cbc decryption password:    输入解密密码
[root@localhost ~]# cat /mnt/issue    查看解密后的内容
CentOS release 6.5 (Final)
Kernel \r on an \m
    用户认证;公钥加密,私钥解密

    数字签名;私钥加密,公钥解密

加密与解密以及OpenSSL的应用

3)、数字证书的证书格式(x509);

    公钥和有效期;

    持有者的个人身份信息;

    证书的使用方式;

    证书发放机构的信息;

    验证CA的数字签名是否合法;


4)、基于openssl生成私有CA证书;

    实验环境;Server,172.16.34.200

              Client, 172.16.34.2

1,server端先生成密钥对;
[root@station154 CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
.....................................................................................................................+++
......+++
e is 65537 (0x10001)
2、server完成自签证书;


3、创建需要的文件;
[root@station154 CA]# touch index.txt serial crlnumber
[root@station154 CA]# ls
cacert.pem  certs  crl  crlnumber  index.txt  newcerts  private  serial
[root@station154 CA]# echo 1 > serial
[root@station154 CA]# cat serial
1
[root@station154 CA]#
4、 client实现证书申请;

  在主机上生成密钥,保存至应用此证书的服务的配置文件目录下;
mkdir /etc/httpd/ssl
cd /etc/httpd/ssl
[root@localhost ssl]# (umask 077; openssl genrsa -out httpd.key 1024)
Generating RSA private key, 1024 bit long modulus
.++++++
........++++++
e is 65537 (0x10001)
[root@localhost ssl]# ls
httpd.key
[root@localhost ssl]#
5、生成证书签署请求:

[root@localhost ssl]# openssl req -new -key httpd.key -out httpd.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) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:Ouyang
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:ca.ouyang.com
Email Address []:caadmin@ouyang.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:RedHat
An optional company name []:redhat
    (Client端所填的信息要与服务器一致)

6、Clientc端将请求文件发往Server端;

  scp httpd.csr 172.16.34.200:/tmp

7、CA签署Client发来的证书申请;

  openssl ca -in /tmp/httpd.csr -out /tmp/httpd.csr -days 3655

8、签署完全后将证书发回申请者; 

  scp httpd.crt 172.16.34.20:/etc/httpd/ssl
9、吊销证书;吊销证书要到Server端;

  openssl ca -revoke /tmp/httpd.crt 

OpenSSL 的详细介绍:请点这里
OpenSSL 的下载地址:请点这里

推荐阅读:

相关推荐