您的位置:首页 > 其它

初级加密技术之块设备加密

2012-06-05 15:50 218 查看
熬死了,下面一大堆的块加密。嗷嗷嗷嗷。。。。不过发现在google游览器上看这个字体特别爽,(*^__^*) 嘻嘻……。说明:加红色字体是需要注意的地方。 加绿色的地方是备注哦。情景一:某男在自己的D盘下放了海量的XX,你懂的。(XX代指重要,不想让人知道的东西)某男外出,其色友接管他电脑,色友死命打不开D盘!某男是怎么样做到的呢?下面就是见证奇迹的时刻! 基础知识:块设备加密的话很有用,经过加密过的块设备里面的数据会相对安全,要使用数据时必须提供密码!但是此种加密只能对整个块设备不能对文件。然后要了解一些在linux里用什么命令来完成整个加密过成,这里有两个很重要的东西!!#man cryptsetup //只截取一部分哦。NAME cryptsetup - setup cryptographic volumes for dm-crypt (including LUKS extension) //dm-crypt 这个东西很神奇,支持luks哦。
SYNOPSIS cryptsetup <options> <action> <action args>
DESCRIPTION cryptsetup is used to conveniently setup dm-crypt managed device-mapper mappings. For basic (plain) dm-crypt mappings, there are four operations。
什么是device-mapperdevice-mapper:Device-mapper is a new infrastructure in the Linux 2.6 kernel that provides a generic way to create virtual layers of block devices that can do different things on top of real block devices like striping, concatenation, mirroring, snapshotting, etc...(dm是在2.6内核里的新架构它提供了在块设备上创建一般的虚拟层这样我们就可以在真实的块设备上做像条带化,镜像,快照等事情) The device-mapper is used by the LVM2 and EVMS 2.x ,dm-crypt disk encryption, and offers additional features such as file-system snapshots.(dm被lvm2,evms2.x,dm-crypt磁盘加密所采用,而且能提供额外的像文件系统快照的功能)
Device-mapper works by processing data passed in from a virtual block device, that it itself provides, and then passing the resultant data on to another block device.(dm的工作原理:)
dm-crypt:a device-mapper crypto target.dm-crypt is such a device-mapper target that provides transparent encryption of block devices using the new Linux 2.6 cryptoapi.(在2.6内核里,dm-crypt是dm的一个目标它提供对块设备透明的加密) The user can basically specify one of the symmetric ciphers(用对称加密), a key (of any allowed size)(任意长度的key), an iv generation mode and then the user can create a new block device in /dev(在/dev下会生成对应的dm设备). Writes to this device will be encrypted and reads decrypted.(写进去会被加密,读的时候会解密) You can mount your filesystem on it as usual. But without the key you can't access your data.(你可以像平常那样挂载你的文件系统,但是如果没有key的话就不能访问你的数据咯).
.....英文很吃力。。。。。漏掉的不好意思咯。
现在理解了么?我们用cryptsetup可以建一般的加密,也可以做标准的luks加密。成功后会在/dev/mapper/ 下生成一个虚拟的块设备,链接到/dev/下的dm-crypt设备。
########################################################
[root@localhost dev]# cryptsetup -y create kiku /dev/sdc1 Enter passphrase://redhat,我设的密码Verify passphrase://redhat[root@localhost dev]#
[root@localhost dev]# mkfs.ext4 /dev/mapper/kiku //创建文件系统。[root@localhost dev]# mount /dev/mapper/kiku /mnt[root@localhost dev]# cd /mnt[root@localhost mnt]# lslost+found[root@localhost mnt]# touch crypt.txt //随便放个东西进去。[root@localhost mnt]# lscrypt.txt lost+found[root@localhost mnt]#[root@localhost mnt]# cd[root@localhost ~]# umount /mnt[root@localhost ~]# cryptsetup remove kiku //在mapper里去掉虚拟设备,就是剪掉/dev/mapper下与/dev下的dm设备的链接关系,因为dm设备还在所以下一次再用的时候,在做个“软链接”就好了。
[root@localhost ~]# cd /dev/mapper/[root@localhost mapper]# lscontrol //看不到kiku了[root@localhost mapper]# cryptsetup create kiku Enter passphrase:[root@localhost mapper]# lscontrol kiku //又有了[root@localhost mapper]# mount /dev/mapper/kiku /mnt[root@localhost mapper]# cd /mnt[root@localhost mnt]# lscrypt.txt lost+found //你看还在!crypt.txt[root@localhost mnt]#这种加密有缺点:不能开机自动挂载,而且不能该密码呀,呼呼。所以我一般选择luks,能解决以上的两个毛病更安全些。#################################################################
下面做luks加密!其实都差不多,最关键是mapper 理解了就好说了。[root@localhost ~]# cryptsetup luksFormat /dev/sdc1 //记得要大写哦,亲。 WARNING!========This will overwrite data on /dev/sdc1 irrevocably.
Are you sure? (Type uppercase yes): YES //用大写的YESEnter LUKS passphrase:Verify passphrase:[root@localhost ~]#[root@localhost ~]# cryptsetup luksOpen /dev/sdc1 kikuEnter passphrase for /dev/sdc1://redhat 我还是用rehdat做密码[root@localhost ~]# cd /dev/mapper/[root@localhost mapper]# lscontrol kiku //又看到它了[root@localhost mapper]# mount /dev/mapper/kiku /mnt[root@localhost mapper]# cd /mnt[root@localhost mnt]# touch kiku.txt //做个文件哈。[root@localhost mnt]# lskiku.txt lost+found[root@localhost mnt]#
开机自动挂载加密卷:要自动挂载的话一定要写fstab文件啦,一行六列!先找到UUID (我喜欢用这个啦,也可以用设备)[root@localhost ~]# cryptsetup luksUUID /dev/sdc17123e454-403a-4a31-81f2-58d355c08458[root@localhost ~]# vim /etc/crypttab //加密的东东要写到这个配置文件。这个文件是一行四列:第一列:在/dev/mapper/下的名字!第二列:设备:UUID 或者/dev/sdax第三列:密钥的文件用绝对路径,这个文件要注意做成随机数哦,如果没有密码的话写成none第四列:翻译不出来啊。。。只知道用luks[root@localhost ~]# dd if=/dev/urandom of=/root/2.txtbs=64 count=1 //做一个装密码文件。伪随机数。这个很重要。记录了1+0 的读入记录了1+0 的写出64字节(64 B)已复制,4.7107e-05 秒,1.4 MB/秒[root@localhost ~]# cryptsetup luksAddKey /dev/sdc1 2.txt#将密钥加到刚刚生成的那个文件里。Enter any passphrase: [root@localhost ~]#[root@localhost ~]# vim /etc/crypttabyoudata UUID=7123e454-403a-4a31-81f2-58d355c08458 /root/2.txt luks
[root@localhost ~]# vim /etc/fstab/dev/mapper/youdata /kiku2 ext4 defaults 0 0。。。。可以重启你的pc了。。。。
文件系统 容量 已用 可用 已用%% 挂载点/dev/sda1 8.7G 2.7G 5.6G 33% /tmpfs 246M 0 246M 0% /dev/shm/dev/mapper/youdata 394M 11M 363M 3% /kiku2 //挂上了#################################################################
情景二:某男带着最爱的XX放在U盘里,通过LUKS加密该文件。顺利在各种场所XX。文件做成loop设备,用luks加密使用。[root@localhost ~]# dd if=/dev/zero of=妹子 bs=1M count=100 //恶搞一下,嘿嘿。记录了100+0 的读入记录了100+0 的写出104857600字节(105 MB)已复制,3.45935 秒,30.3 MB/秒[root@localhost ~]# losetup /dev/loop0 妹子 //把妹子做成loop设备[root@localhost ~]# losetup /dev/loop0/dev/loop0: [0801]:277713 (/root/妹子)[root@localhost ~]# cryptsetup luksFormat /dev/loop0 //是块设备以后,我们就用上面那一套就可以咯。
WARNING!========This will overwrite data on /dev/loop0 irrevocably.
Are you sure? (Type uppercase yes): YESEnter LUKS passphrase:Verify passphrase:[root@localhost ~]# cryptsetup luksOpen /dev/loop0 kikuEnter passphrase for /dev/loop0:[root@localhost ~]# cd /dev/mapper/[root@localhost mapper]# lscontrol kiku[root@localhost mapper]#[root@localhost mapper]# mkfs.ext4 /dev/mapper/kiku[root@localhost mapper]# mount /dev/mapper/kiku /mnt[root@localhost mapper]# cd /mnt[root@localhost mnt]# lslost+found[root@localhost mnt]# touch loop0.txt[root@localhost mnt]# lsloop0.txt lost+found
[root@localhost ~]# umount /mnt[root@localhost ~]# cryptsetup luksClose kiku[root@localhost ~]# cd /dev/mapper/[root@localhost mapper]# lscontrol [root@localhost mapper]#[root@localhost mapper]# losetup -d /dev/loop0[root@localhost mapper]# losetup /dev/loop0loop: can't get info on device /dev/loop0: 没有那个设备或地址
[root@localhost ~]# losetup /dev/loop1 妹子[root@localhost ~]# losetup /dev/loop1/dev/loop1: [0801]:277713 (/root/妹子)[root@localhost ~]#[root@localhost ~]# cryptsetup luksOpen /dev/sdc1 妹子Enter passphrase for /dev/sdc1:[root@localhost ~]# cd /dev/mapper/[root@localhost mapper]# lscontrol 妹子[root@localhost mapper]#[root@localhost mapper]# mount /dev/mapper/妹子 /mnt[root@localhost mapper]# cd /mnt[root@localhost mnt]# lskiku.txt lost+found[root@localhost mnt]# 结束了,最好是写成脚本,不然会搞死人纳。我看着都头晕了。对不起各位了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  luks加密 加密卷