基本文件加密和解密

加密

$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加密。

解密

让我们解密我们的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使用不同的加密算法

使用以下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

使用-h选项指定加密算法:

$mcrypt -k abc123 -h whirlpool file1
Warning: It is insecure to specify keywords in the command line
File file1 was encrypted.

使用mcrypt目录加密

要加密目录,我们需要使用tar

$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,用于保存解密文件

$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

比较md5

$md5sum dir1/file[1,2]
bccd44aaa84c7c9d04a268f670ae92c5  dir1/file1
4034379ecc54213fc9a51785a9d0e8e2  dir1/file2

配置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.
在Linux系统上如何使用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

压缩加密

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在用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

使用md5确认文件是否一致

$md5sum file1
bccd44aaa84c7c9d04a268f670ae92c5  file1
日期:2020-07-07 20:54:39 来源:oir作者:oir