linux配置文件上使用密文,如何在Linux系统上使用Mcrypt轻松加密任何文件或目录...

在此配置中,我们将向您展示一些如何使用的示例mcrypt工具轻松加密文件,无论文件大小。我们还将使用Mcrypt即时加密和压缩文件和目录,这对于许多备份和脚本编写目的很有用。

Mcrypt安装

UBUNTU/DEBIAN

# apt-get install mcrypt

REDHAT/FEDORA/CENTOS

# yum install mcrypt

创建一个测试沙箱

首先,创建一个包含一些我们可以使用的文件的目录:

$ mkdir dir1

$ cd dir1/

$ echo "My File to Encrypt" > file1

$ cat file1

My File to Encrypt

$ fallocate -l 500MB file2

$ md5sum file*

bccd44aaa84c7c9d04a268f670ae92c5 file1

4034379ecc54213fc9a51785a9d0e8e2 file2

通过以上命令,我们创建了一个目录dir1。在我们的目录中,我们创建了两个文件file1一个简单的文本文件,以及file2大小为500MB,并包含一些随机的二进制数据。接下来,我们为两个文件都生成了md5sum,因此我们可以在解密后比较文件。

基本文件加密和解密

加密

在这一阶段,我们可以从一个简单的文件加密和解密示例开始。下列linux命令将加密file1以及用户在mcrypt命令执行:

$ mcrypt file1

Enter the passphrase (maximum of 512 characters)

Please use a combination of upper and lower case letters and numbers.

Enter passphrase:

Enter passphrase:

File file1 was encrypted.

$ ls -l

total 488292

-rw-rw-r--. 1 lrendek lrendek 19 Jan 15 18:24 file1

-rw-------. 1 lrendek lrendek 125 Jan 15 18:24 file1.nc

-rw-r--r--. 1 lrendek lrendek 500000000 Jan 15 18:24 file2

上面的加密Mcrypt命令的输出是file1.nc。要一次加密两个文件,我们可以在命令行上提供两个文件名,然后分别为两个文件输入加密密码。相反,在命令行上使用密码短语更容易,但安全性较低。例:

$ mcrypt file1 file2 -k abc123

Warning: It is insecure to specify keywords in the command line

File file1 was encrypted.

File file2 was encrypted.

这两个文件都已使用密码加密abc123。

加密

在此阶段,我们可以尝试使用Mcrypt的解压缩功能。让我们解密file1.nc:

-rw-------. 1 lrendek lrendek 124 Jan 15 18:24 file1.nc

mkdir dir2

$ mv file*.nc dir2/

$ cd dir2/

$ ls

file1.nc file2.nc

$ mcrypt -d file1.nc

Enter passphrase:

File file1.nc was decrypted.

同样,我们也可以一次解密两个文件:

$ mcrypt -k abc123 -d file1.nc file2.nc

Warning: It is insecure to specify keywords in the command line

File file1.nc was decrypted.

File file2.nc was decrypted.

并将解密的文件与先前的md5sum输出进行比较:

$ md5sum file[1,2]

bccd44aaa84c7c9d04a268f670ae92c5 file1

4034379ecc54213fc9a51785a9d0e8e2 file2

压缩加密

Mcrypt还提供了在实际压缩发生之前使用gzip压缩文件的选项。考虑以下示例:

$ mcrypt -k abc123 -z file1

Warning: It is insecure to specify keywords in the command line

File file1 was encrypted.

$ file file1.gz.nc

file1.gz.nc: mcrypt 2.5 encrypted data, algorithm: rijndael-128, keysize: 32 bytes, mode: cbc,

在上面的示例文件中file1已使用gzip压缩,然后使用mcrypt加密。要解密gzip压缩文件,我们只需要反向进行即可。首先解密您的文件:

$ mcrypt -k abc123 -d file1.gz.nc

Warning: It is insecure to specify keywords in the command line

File file1.gz.nc was decrypted.

然后用解压缩输出gunzip:

$ gunzip -v file1.gz

file1.gz:-10.5% -- replaced with file1

再次确认上述过程的有效性,我们使用md5sum:

$ md5sum file1

bccd44aaa84c7c9d04a268f670ae92c5 file1

使用Mcrypt进行目录加密

为了加密目录mcrypt我们首先需要使用tar在目录上。下一个命令示例将加密我们的整个初始目录dir1:

$ tar cz dir1/ | mcrypt -k abc123 > dir1.tar.gz.nc

Warning: It is insecure to specify keywords in the command line

Stdin was encrypted.

$ file dir1.tar.gz.nc

dir1.tar.gz.nc: mcrypt 2.5 encrypted data, algorithm: rijndael-128, keysize: 32 bytes, mode: cbc,

让我们创建另一个目录dir3我们将使用它来解密上述目录dir1从文件dir1.tar.gz.nc。

$ mkdir dir3

$ mv dir1.tar.gz.nc dir3/

$ cd dir3/

$ ls

dir1.tar.gz.nc

与文件一样,我们首先需要解密我们的加密档案:

$ mcrypt -k abc123 -d dir1.tar.gz.nc

Warning: It is insecure to specify keywords in the command line

File dir1.tar.gz.nc was decrypted.

归档文件解密后,我们可以使用tar命令:

$ tar xzf dir1.tar.gz

并比较md5sum

$ md5sum dir1/file[1,2]

bccd44aaa84c7c9d04a268f670ae92c5 dir1/file1

4034379ecc54213fc9a51785a9d0e8e2 dir1/file2

更改Mcrypt的加密算法

使用以下linux命令列出所有可用的加密算法:

$ mcrypt --list-hash

Supported Hash Algorithms:

crc32

md5

sha1

haval256

ripemd160

tiger

gost

crc32b

haval224

haval192

haval160

haval128

tiger128

tiger160

md4

sha256

adler32

sha224

sha512

sha384

whirlpool

ripemd128

ripemd256

ripemd320

snefru128

snefru256

md2

更改加密算法是mcrypt的一项非常容易的任务-h选项。只需选择以上列出的算法之一并使用-h在命令行上指定它。例如下面的算法将加密我们的file1与whirlpool加密演算法:

$ mcrypt -k abc123 -h whirlpool file1

Warning: It is insecure to specify keywords in the command line

File file1 was encrypted.

配置mcrypt

也可以创建配置文件,以便可以在命令行上提交mcrypt的选项。这是一个很棒的功能,尤其是对于脚本编写等。例如,我们可以使用默认密码创建一个配置文件abc123:

$ echo "key abc123" > ~/.mcryptrc

$ mcrypt file1

Warning: It is insecure to specify keywords in the command line

File file1 was encrypted.

$ mcrypt -k abc123 -d file1.nc

Warning: It is insecure to specify keywords in the command line

File file1.nc was decrypted.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值