一、数据加密的分类
数据加密算法通常可以分为三类:对称加密;非对称加密;单向加密。先对它们的区别与作用作说明
1、对称加密算法
所谓对称加密算法就是加密和解密使用同一个密钥。其基本算法有DES、3DES、AES等。特性:加密、解密使用同一个密钥;将原始数据分割成固定大小的块,逐个进行加密。缺陷:密钥过多;密钥分发。
2、非对称加密
密钥是成对出现。实现算法有RSA, DSA, ELGama等。公钥(pubkey):公开给所有人;私钥(secret key):自己留存,必须保证其私密性。特点:用公钥加密的数据,只能使用与之配对儿的私钥解密;反之亦然。其主要应用场景有:
数字签名:主要在于让接收方确认发送方身份; 密钥交换:发送方用对方的公钥加密一个对称密钥,并发送给对方; 数据加密:由于加密数据时间缓慢,一般不用来加密数据。 |
3、单向加密
其基本算法有:MD5,SHA1等。特点:只能解密,不能解密;可用于提取数据指纹;定长输出、雪崩效应(即数据内容的一丁点改变,可能造成输出结果的巨大变化)。
二、openssl的使用
openssl是用于实现加密的工具。其内部集成了大多数加密算法的实现。现在对其基本用法做一个说明。
1、对称数据加密
使用des3算法,加密 fdisk.sh文件数据,加密后的文件数据存放入code.file中
[[email protected] home]# openssl enc -des3 -a -salt -in fdisk.sh -out code.file;加密 enter des-ede3-cbc encryption password: Verifying - enter des-ede3-cbc encryption password: [[email protected] home]# cat fdisk.sh qeqweqeq [[email protected] home]# cat code.file U2FsdGVkX1+JUB/kio926cofroD0rRypfUcYmh+eIho= [[email protected] home]# openssl enc -d -des3 -a -salt -in code.file -out fdisk2.sh;解密 enter des-ede3-cbc decryption password: [[email protected] home]# cat fdisk2.sh qeqweqeq [[email protected] home]#
2、单向加密
一般用于提取文件的特征码,文件数据的微小改变会造成雪崩效应。 [[email protected] home]# cat fdisk.sh qeqweqeq [[email protected] home]# openssl dgst -md5 fdisk.sh MD5(fdisk.sh)= 44892aaa22ea31022cf8af4e0521e3ff [[email protected] home]# echo "qeqweqee" >fdisk.sh [[email protected] home]# cat fdisk.sh qeqweqee [[email protected] home]# openssl dgst -md5 fdisk.sh MD5(fdisk.sh)= bb123d0f209942acdb0d640c3e1c32f3 [[email protected] home]#
3、非对称加密
主要用于密钥交换(对方的公钥)、数据加密(对方的公钥)、身份认证(自己的私钥) [[email protected] home]# openssl genrsa -out rsa.key;生成私钥 Generating RSA private key, 1024 bit long modulus .....++++++ ............................++++++ e is 65537 (0x10001) [[email protected] home]# cat rsa.key -----BEGIN RSA PRIVATE KEY----- MIICWwIBAAKBgQDPGeA1ir63tb4C8TkRUvEvoQkPMk2NE5389ErMtCB7t4Njif7A VVTWn1zC/WWHfwaWfRb4fNdMCyv0aLPRvMCfw8TKTd6thlD0WJ5nIYAWyOA4XN2A Jz1o80/iMMkdx1TBju5sjvvOPRYRVeBsta/x/hsDyo3F+OSHSmgBqDVv3QIDAQAB AoGAIRbKSuOzGxkPldrCunDdFjYNg38PAvZabLr/ZHt3IS0t8e5vZH19w5jMsl7Y JPD3tX2g+OzBXWX/jNpZuK4GvHY2GMNRbI0cwQTtr5oNEBFCmdeO9gETVglcLmwN SgDQSo1iK1mEEroL7n2D8pBxOqEdsXgyQAKoGT2miyQckwECQQD5BEnBzs3ddkH7 QjjOnxEpxE3MsLSCJqUWJ6eI/ZuKCfxgiAWjHPKny/OA5F/Dr+apSJB34BwS6g5l ClThEIjBAkEA1Oir7tqOA1SkfFfwOyXRK/C83XYgjbU2gp594hAPmRbCZu6DYxht j+UOz0cB9VrLlfHmvzZG1d08AD10+HByHQJAMp5mh9kMC/qgCv05Ivc+u+jFj9hT uSk14b0J11UGKxdAkDbWPoQB544T352MwfCdyMeRZmzOCCI/7tYczutXgQJADoZd DGt+mr2QhyccrXxT+9JtoBIqAGDnehvD7MM5vTDvTERjVERbXSC8VOm8OXfbQbyC CHN0e6yfJkkLDNPnBQJAPP+G+T8PvVZlYOYjptCR+H7ofylVgkGK3eh/6pwtyoxR AJ3mTOQydIpLw3w1cOIDTNZOF8hXxRAJa7imJF74ew== -----END RSA PRIVATE KEY----- [[email protected] home]# [[email protected] home]# openssl rsa -in rsa.key -pubout ;从私钥中提取公钥 writing RSA key -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPGeA1ir63tb4C8TkRUvEvoQkP Mk2NE5389ErMtCB7t4Njif7AVVTWn1zC/WWHfwaWfRb4fNdMCyv0aLPRvMCfw8TK Td6thlD0WJ5nIYAWyOA4XN2AJz1o80/iMMkdx1TBju5sjvvOPRYRVeBsta/x/hsD yo3F+OSHSmgBqDVv3QIDAQAB -----END PUBLIC KEY-----
三、远程连接SSH服务
1、SSH远程连接原理
SSH: Secure SHell,建立在应用层和传输层基础上的安全协议。通信及认证过程是加密的。监听tcp的22号端口,是文本协议。其主要工作原理如下图所示:
对于SSH服务协议通常使用的是sshv2版本。sshv2基于双方主机协商选择最安全的MAC实现机制;加密机制及MAC机制是双方协商选定;基于DH实现密钥交换,基于RSA或DSA实现身份认证;客户通过检查服务端的主机密钥来判定是否与其进一步通信。
2、SSH的开源实现工具OpenSSH
配置文件/etc/ssh/ssh_config
ssh认证机制:基于口令、基于秘钥。
ssh [-p PORT][email protected] (登录远程主机)
ssh -l USERNAME HOST
ssh [email protected] ‘COMMAND‘ (不登录远程主机,操作远程主机)
scp: 类cp命令,基于ssh协议跨主机复制
scp [email protected]:/path/to/somefile /path/to/local(远程复制到本地)
scp /path/to/local [email protected]:/path/to/somewhere(复制到远程主机)
-r: 源文件为目录时使用,以实现递归复制; -p: 保留源文件的复制及修改时间戳,以及权限;
-q: 静默模式-P PORT:指定服务端端口;
3、基于秘钥的免密通信
[[email protected] home]# ssh 192.168.1.108 The authenticity of host ‘192.168.1.108 (192.168.1.108)‘ can‘t be established. RSA key fingerprint is 6b:d7:f0:61:fe:b9:b7:2c:93:f5:5b:be:80:49:77:7d. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘192.168.1.108‘ (RSA) to the list of known hosts. [email protected]‘s password: Last login: Tue Sep 6 21:13:57 2016 from 192.168.1.5 [[email protected] ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:8B:8C:FE inet addr:192.168.1.108 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe8b:8cfe/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4372 errors:0 dropped:0 overruns:0 frame:0 TX packets:2446 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:5094407 (4.8 MiB) TX bytes:166449 (162.5 KiB) Interrupt:19 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:38 errors:0 dropped:0 overruns:0 frame:0 TX packets:38 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:3344 (3.2 KiB) TX bytes:3344 (3.2 KiB)
通常我们使用ssh登录远程主机的时候是要密码的,当我们想要免密登录对方的主机时,这时就要用到基于秘钥得认证机制。使用方法也十分简单,只需要两步:
1、客户端本地生成一对秘钥
2、将公钥传输至远程服务器,追加保存到远程主机某用户的家目录 的.ssh/authorized_keys文件或.ssh/authorized_keys2文件中。
在这里我使用192.168.1.106位客户端。192.168.1.108作为远程服务器。
[[email protected] home]# ssh-keygen -t rsa ;生成一对秘钥 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: e9:ae:a4:2c:bd:68:64:6a:ae:a4:81:20:39:9f:ac:2f [email protected] The key‘s randomart image is: +--[ RSA 2048]----+ | | | | | | | . . | |= S | |++o. . | |o=+. . . | |E+oo.o . | |B=o.+.... | +-----------------+ [[email protected] home]# cd ~/.ssh/ [[email protected] .ssh]# ls id_rsa id_rsa.pub known_hosts [[email protected] .ssh]# ssh-copy-id -i id_rsa.pub [email protected] ;将公钥传送至远程服务器 [email protected]‘s password: Now try logging into the machine, with "ssh ‘[email protected]‘", and check in: .ssh/authorized_keys to make sure we haven‘t added extra keys that you weren‘t expecting. [[email protected] .ssh]# ssh [email protected] ;免密登录成功 Last login: Tue Sep 6 21:18:21 2016 from 192.168.1.106 [[email protected] ~]#