设为首页 收藏本站
查看: 687|回复: 0

[经验分享] Linux加密和解密、openssl的基本应用及CA的实现过程

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-29 08:44:59 | 显示全部楼层 |阅读模式
一、数据加密方式
  加密方式分为:单向加密、对称加密和公钥加密。

1.对称加密  
  对称加密:采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密。在对称加密算法中常用的算法有:DES、3DES、TDEA、Blowfish、RC2、RC4、RC5、IDEA、SKIPJACK、AES等。
  特性:
    1、加密、解密使用同一个密钥;
    2、将原始数据分割成固定大小的块,逐个进行加密;
  缺陷:
    1、密钥过多;
    2、密钥分发;
  对称加密过程如下图所示:
wKiom1YI2R6xzzqYAAHmHXHQm9o092.jpg
2.公钥加密
  公钥加密,也叫非对称(密钥)加密(public key encryption),属于通信科技下的网络安全二级学科,指的是由对应的一对唯一性密钥(即公开密钥和私有密钥)组成的加密方法。它解决了密钥的发布和管理问题,是目前商业密码的核心。在公钥加密体制中,没有公开的是明文,公开的是密文,公钥,算法。
  常见算法有RSA、ElGamal、背包算法、Rabin(Rabin的加密法可以说是RSA方法的特例)、Diffie-Hellman (D-H) 密钥交换协议中的公钥加密算法、Elliptic Curve Cryptography(ECC,椭圆曲线加密算法)。使用最广泛的是RSA算法(由发明者Rivest、Shmir和Adleman姓氏首字母缩写而来)是著名的公开金钥加密算法,ElGamal是另一种常用的非对称加密算法。
  公钥加密过程如下图:
wKioL1YI20mwlnfKAAEE8CQDswk498.jpg
3.单向加密
  单向加密是非可逆加密,就是不可解密的加密方法,提取数据的指纹。常见的加密算法有MD5、SHA、HMAC这三种加密算法。我们通常只把他们作为加密的基础,单纯的以上三种的加密并不可靠。
  单向加密如下图:
wKiom1YI3ZehpnMxAAB1fTgDh_Q522.jpg
二、KPI
PKI是Public Key Infrastructure的首字母缩写,翻译过来就是公钥基础设施;PKI是一种遵循标准的利用公钥加密技术为电子商务的开展提供一套安全基础平台的技术和规范。

1.基本组成
  PKI公钥基础设施是提供公钥加密和数字签名服务的系统或平台,目的是为了管理密钥和证书。一个机构通过采用PKI框架管理密钥和证书可以建立一个安全的网络环境。PKI 主要包括四个部分:X.509 格式的证书(X.509 V3)和证书废止列表CRL(X.509 V2);CA 操作协议;CA 管理协议;CA 政策制定。一个典型、完整、有效的PKI 应用系统至少应具有以下五个部分:
(1)认证中心CA
  CA 是PKI 的核心,CA 负责管理PKI 结构下的所有用户(包括各种应用程序)的证书,把用户的公钥和用户的其他信息捆绑在一起,在网上验证用户的身份,CA 还要负责用户证书的黑名单登记和黑名单发布,后面有CA 的详细描述。
(2)X.500目录服务器
  X.500 目录服务器用于发布用户的证书和黑名单信息,用户可通过标准的LDAP协议查询自己或其他人的证书和下载黑名单信息。
(3)具有高强度密码算法(SSL)的安全WWW服务器
  Secure socket layer(SSL)协议最初由Netscape企业发展,现已成为网络用来鉴别网站和网页浏览者身份,以及在浏览器使用者及网页服务器之间进行加密通讯的全球化标准。
(4)Web(安全通信平台)
  Web 有Web Client端和Web Server端两部分,分别安装在客户端和服务器端,通过具有高强度密码算法的SSL协议保证客户端和服务器端数据的机密性、完整性、身份验证。
(5)自开发安全应用系统
  自开发安全应用系统是指各行业自开发的各种具体应用系统,例如银行、证券的应用系统等。完整的PKI 包括认证政策的制定(包括遵循的技术标准、各CA 之间的上下级或同级关系、安全策略、安全程度、服务对象、管理原则和框架等)、认证规则、运作制度的制定、所涉及的各方法律关系内容以及技术的实现等。
2.CA
  认证中心CA作为PKI的核心部分,CA实现了PKI中一些很重要的功能,概括地说,认证中心(CA)的功能有:证书发放、证书更新、证书撤销和证书验证。CA的核心功能就是发放和管理数字证书,具体描述如下:
  (1)接收验证最终用户数字证书的申请。
  (2)确定是否接受最终用户数字证书的申请-证书的审批。
  (3)向申请者颁发、拒绝颁发数字证书-证书的发放。
  (4)接收、处理最终用户的数字证书更新请求-证书的更新。
  (5)接收最终用户数字证书的查询、撤销。
  (6)产生和发布证书废止列表(CRL)。
  (7)数字证书的归档。
  (8)密钥归档。
  (9)历史数据归档。
  认证中心CA为了实现其功能,主要由以下三部分组成:
  注册服务器:通过 Web Server 建立的站点,可为客户提供24×7 不间断的服务。客户在网上提出证书申请和填写相应的证书申请表。
  证书申请受理和审核机构:负责证书的申请和审核。它的主要功能是接受客户证书申请并进行审核。
  认证中心服务器:是数字证书生成、发放的运行实体,同时提供发放证书的管理、证书废止列表(CRL)的生成和处理等服务。
  在具体实施时,CA的必须做到以下几点:
  1) 验证并标识证书申请者的身份。
  2) 确保CA 用于签名证书的非对称密钥的质量。
  3) 确保整个签证过程的安全性,确保签名私钥的安全性。
  4) 证书资料信息(包括公钥证书序列号,CA标识等)的管理。
  5) 确定并检查证书的有效期限。
  6) 确保证书主体标识的唯一性,防止重名。
  7) 发布并维护作废证书列表。
  8) 对整个证书签发过程做日志记录。
  9) 向申请人发出通知。
  在这其中最重要的是CA自己的一对密钥的管理,它必须确保其高度的机密性,防止他方伪造证书。CA的公钥在网上公开,因此整个网络系统必须保证完整性。CA的数字签名保证了证书(实质是持有者的公钥)的合法性和权威性。
三、OpenSSL

1.OpenSSL定义

  OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。OpenSSL整个软件包大概可以分成三个主要的功能部分:SSL协议库、应用程序以及密码算法库。OpenSSL的目录结构自然也是围绕这三个功能部分进行规划的。
2.加密解密技术常用的功能及算法
(1)对称加密
工具:openssl enc、gpg

使用openssl enc加密/etc/fstab文件:
[iyunv@localhost ~]# openssl enc -des3 -a -salt -in /etc/fstab -out /tmp/fstab
enter des-ede3-cbc encryption password:
Verifying - enter des-ede3-cbc encryption password:
[iyunv@localhost ~]# cat /tmp/fstab
U2FsdGVkX19azqfGZk8CwdjjIWuA1nl7LFrDUnmRsiIScbIIAIzoy0SFb3aatTmk
BSNm7tJkNS9qKxvEL3kWP7f5R8SQ15vE9qyjGLDmVWXN1PCbPRm+G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njWcdgL1LWjiaNqqY+AQRF3sp
MOBu+BicWX/94oeoHVzuLo8BUgfnu9pJcysEiNlBwD8=
解密/tmp/fstab文件:

[iyunv@localhost ~]# openssl enc -d -des3 -a -salt -in /tmp/fstab -out /tmp/fstab.out
enter des-ede3-cbc decryption password:
[iyunv@localhost ~]# cat /tmp/fstab.out

#
# /etc/fstab
# Created by anaconda on Fri Aug 14 11:27:13 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg0-root    /                       ext4    defaults        1 1
UUID=107e54b8-7571-4f3d-be8c-8858b6a17f93 /boot                   ext4    defaults        1 2
/dev/mapper/vg0-usr     /usr                    ext4    defaults        1 2
/dev/mapper/vg0-var     /var                    ext4    defaults        1 2
/dev/mapper/vg0-swap    swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

(2)单向加密
工具:sha1sum,md5sum,openssl dgst
[iyunv@localhost tmp]# openssl dgst -sha1 fstab SHA1(fstab)= b435ed917f8b3e6c8864f6b121e1237a4a5e3e99
[iyunv@localhost tmp]# sha1sum fstab
b435ed917f8b3e6c8864f6b121e1237a4a5e3e99  fstab
四、OpenSSL创建私有CA

1.openssl配置文件:/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
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
RANDFILE        = $dir/private/.rand        # private random number file

x509_extensions        = usr_cert                # The extentions to add to the cert
2.生成密钥对
[iyunv@localhost private]# (umask 077 ;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
......................................................+++
...........................+++
e is 65537 (0x10001)
[iyunv@localhost private]# ls -l
total 4
-rw------- 1 root root 1675 Sep 28 06:59 cakey.pem
3.生成自签证书
[iyunv@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
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) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:linux operation
Common Name (eg, your name or your server's hostname) []:ca.magedu.com
Email Address []:caadmin@magedu.com
[iyunv@localhost CA]# ls
cacert.pem  certs  crl  newcerts  private
[iyunv@localhost CA]# touch index.txt serial crlnumber
[iyunv@localhost CA]# echo 01 > serial
4.openssl实现证书申请

(1)在主机上生成密钥,保存至应用此证书的服务的配置文件目录下, 例如:

[iyunv@www httpd]# mkdir ssl
[iyunv@www httpd]# cd ssl/
[iyunv@www ssl]# (umask 077;openssl genrsa 1024 > http.key)
Generating RSA private key, 1024 bit long modulus
..++++++
...................................................................++++++
e is 65537 (0x10001)
(2)生成证书签署请求
[iyunv@www ssl]# openssl req -new -key http.key -out http.csrYou 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) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:linux operation
Common Name (eg, your name or your server's hostname) []:www.magedu.com
Email Address []:webmaster@magedu.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
(3)将请求文件发给CA
[iyunv@www ssl]# scp http.csr 192.168.1.103:/tmp/
The authenticity of host '192.168.1.103 (192.168.1.103)' can't be established.
RSA key fingerprint is 9b:a1:30:76:04:5c:e8:d4:72:01:b1:0a:7d:6a:03:77.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': ^C[iyunv@www ssl]# scp http.csr 192.168.1.103:/tmp/^C
[iyunv@www ssl]# scp http.csr 192.168.1.103:/tmp/
The authenticity of host '192.168.1.103 (192.168.1.103)' can't be established.
RSA key fingerprint is 9b:a1:30:76:04:5c:e8:d4:72:01:b1:0a:7d:6a:03:77.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.103' (RSA) to the list of known hosts.
Address 192.168.1.103 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.1.103's password:
http.csr                                                                                           100%  720     0.7KB/s   00:00
5.CA签署证书
(1)CA签署
[iyunv@localhost ~]# openssl ca -in /tmp/http.csr -out /tmp/http.crt -days 3650
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: Sep 27 23:52:58 2015 GMT
            Not After : Sep 24 23:52:58 2025 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = beijing
            organizationName          = magedu
            organizationalUnitName    = linux operation
            commonName                = www.magedu.com
            emailAddress              = webmaster@magedu.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                25:C3:E7:05:48:D7:7F:6C:53:3F:E4:7C:70:ED:2F:B4:8C:BF:8E:73
            X509v3 Authority Key Identifier:
                keyid:82:EA:8D:E8:CC:6C:1D:02:A3:A6:EE:1C:D2:51:78:A6:FE:D7:89:11

Certificate is to be certified until Sep 24 23:52:58 2025 GMT (3650 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 Update
(2)将证书传回请求者
[iyunv@localhost CA]# scp /tmp/http.crt 192.168.1.100:/etc/httpd/ssl/
The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established.
RSA key fingerprint is 03:9c:f7:7e:1e:30:4d:38:97:f6:69:bc:52:bc:3e:e8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.100' (RSA) to the list of known hosts.
Address 192.168.1.100 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.1.100's password:
http.crt                                                                                           100% 3927     3.8KB/s   00:00  
6.CA吊销证书
[iyunv@localhost tmp]# openssl ca -revoke http.crt
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 01.
Data Base Updated


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-120226-1-1.html 上篇帖子: 字符及文本处理之一:wc,cut,sort,uniq,tr,paste,split 下篇帖子: RHEL6.2详细安装greenplum-db-4.3.5.4(part one) Linux 加密
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表