Linux下文件加密方法总结

为了安全考虑,通常会对一些重要文件进行加密备份或加密保存,下面对linux下的文件加密方法做一简单总结:

方法一:gzexe加密

这种加密方式不是非常保险的方法,但是能够满足一般的加密用途,可以隐蔽脚本中的密码等信息。
它是使用系统自带的gzexe程序,它不但加密,同时压缩文件。示例如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

[root@ipsan-node03 ~]# echo "hahahaha" > a.txt

[root@ipsan-node03 ~]# cat a.txt

hahahaha

[root@ipsan-node03 ~]# ls a.txt

a.txt

[root@ipsan-node03 ~]# gzexe a.txt

a.txt:    22.2%

[root@ipsan-node03 ~]# ls

a.txt  a.txt~ 

gzexe方法会把原来没有加密的文件a.txt备份为a.txt~ ,同时a.txt文件变成了加密文件(即变成了密文)

[root@ipsan-node03 ~]# cat a.txt                                                                                          

쏎?螳?p¹\v£?y«0

        Fc?؃ÿE?0´ûm

Ͱ:n$9hss4¢03

   NeAEؚVºY¯׻ѿ¾¹«*霻­+]ᚚaΜ

                               Y$@:Wj%

                                      .iȣ¬Z®:J ¦b¶mC<ӿ8n

[root@ipsan-node03 ~]# cat a.txt~

hahahaha

通常使用gzexe加密后,会将备份文件(这里指a.txt~)删除

[root@ipsan-node03 ~]# ls

a.txt  a.txt~ 

[root@ipsan-node03 ~]# rm -f a.txt~

[root@ipsan-node03 ~]# ls

a.txt

使用-d参数进行解压操作

[root@ipsan-node03 ~]# gzexe --help

Usage: /usr/bin/gzexe [OPTION] FILE...

Rename each FILE with a compressed version of itself, renaming FILE to FILE~.

  -d             Decompress each FILE instead of compressing it.

      --help     display this help and exit

      --version  output version information and exit

Report bugs to <bug-gzip@gnu.org>.

解压之后的文件a.txt内容就会还原回来,同时也会将之前的加密文件变成a.txt~,同样,通常也会删除这个a.txt~的备份文件

[root@ipsan-node03 ~]# gzexe -d a.txt

[root@ipsan-node03 ~]# ls

a.txt  a.txt~ 

[root@ipsan-node03 ~]# cat a.txt

hahahaha

[root@ipsan-node03 ~]# cat a.txt~

쏎?螳?p¹\v£?y«0

        Fc?؃ÿE?0´ûm

Ͱ:n$9hss4¢03

   NeAEؚVºY¯׻ѿ¾¹«*霻­+]ᚚaΜ

                               Y$@:Wj%

                                      .iȣ¬Z®:J ¦b¶mC<ӿ8n

[root@ipsan-node03 ~]# rm -f a.txt~

[root@ipsan-node03 ~]# ls

a.txt

方法二:用tar命令 对文件加密压缩和解压

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

[root@ipsan-node03 ~]# ls

test.txt

[root@ipsan-node03 ~]# cat test.txt

hahahaha

heiheihei

  

如下命令是对filename文件(test.txt)进行加密压缩,生成filename.des3加密压缩文件,123@123为加密的密码

[root@ipsan-node03 ~]# tar -zcf - test.txt |openssl des3 -salt -k 123@123 | dd of=test.txt.des3

0+1 records in

0+1 records out

152 bytes (152 B) copied, 0.00333366 s, 45.6 kB/s

---------------------------------------------------------------------------------------------------------

也可以将/mnt目录下的所有文件全部加密压缩

[root@ipsan-node03 ~]# tar -zcf - /mnt/* |openssl des3 -salt -k 123@123 | dd of=test.des3

或者根据匹配规则进行加密压缩

[root@ipsan-node03 ~]# tar -zcf - /mnt/pass_* |openssl des3 -salt -k 123@123 | dd of=test.des3

---------------------------------------------------------------------------------------------------------

  

通常加密后,会将源文件删除

[root@ipsan-node03 ~]# ls

test.txt  test.txt.des3

[root@ipsan-node03 ~]# rm -f test.txt

[root@ipsan-node03 ~]# cat test.txt.des3

Salted__H¡+ZCHaW⃟׬

                 \bS©|>þHބ*?ܪ³?@ⴹ??qk)B㲏¡qk;ochl\cz-?/흯

¤ވտ+¾´2AuK?픏̞t悐ah¤ºʀ?d

  

解压操作:

[root@ipsan-node03 ~]# dd if=test.txt.des3 |openssl des3 -d -k 123@123 | tar zxf -

0+1 records in

0+1 records out

152 bytes (152 B) copied, 4.5873e-05 s, 3.3 MB/s

  

[root@ipsan-node03 ~]# ls

test.txt  test.txt.des3

[root@ipsan-node03 ~]# cat test.txt

hahahaha

heiheihei

  

注意命令最后面的"-",它将释放所有文件,

-k 123@123可以没有,没有时在解压时会提示输入密码

方法三:结合Tar和OpenSSL给文件和目录加密及解密

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

当有重要的敏感数据的时候,给文件和目录额外加一层保护是至关重要的,特别是当需要通过网络与他人传输数据的时候。基于这个原因,

可以用到tar(Linux 的一个压缩打包工具)和OpenSSL来解决的方案。借助这两个工具,你真的可以毫不费力地创建和加密 tar 归档文件。

下面介绍使用 OpenSSL创建和加密 tar 或 gz(gzip,另一种压缩文件)归档文件:

牢记使用 OpenSSL 的常规方式是:

# openssl command command-options arguments

示例如下:

[root@ipsan-node03 ~]# cd /mnt/

[root@ipsan-node03 mnt]# ls

[root@ipsan-node03 mnt]# echo "123" > a.txt

[root@ipsan-node03 mnt]# echo "456" > b.txt

[root@ipsan-node03 mnt]# echo "789" > c.txt

[root@ipsan-node03 mnt]# ls

a.txt  b.txt  c.txt

现在要加密当前工作目录的内容(根据文件的大小,这可能需要一点时间)

[root@ipsan-node03 mnt]# tar -czf - * | openssl enc -e -aes256 -out test.tar.gz

enter aes-256-cbc encryption password:                          //假设这里设置的密码为123456

Verifying - enter aes-256-cbc encryption password:

上述命令的解释:

enc 使用加密进行编码

-e  用来加密输入文件的 enc 命令选项,这里是指前一个 tar 命令的输出

-aes256 加密用的算法

-out 用于指定输出文件名的 enc 命令选项,这里文件名是test.tar.gz

[root@ipsan-node03 mnt]# ls

a.txt  b.txt  c.txt  test.tar.gz

[root@ipsan-node03 mnt]# rm -rf a.txt

[root@ipsan-node03 mnt]# rm -rf b.txt

[root@ipsan-node03 mnt]# rm -rf c.txt

[root@ipsan-node03 mnt]# ls

test.tar.gz

对于上面加密后的tar包直接解压肯定是不行的!

[root@ipsan-node03 mnt]# tar -zvxf test.tar.gz

gzip: stdin: not in gzip format

tar: Child returned status 1

tar: Error is not recoverable: exiting now

要解密上述tar归档内容,需要使用以下命令。

[root@ipsan-node03 mnt]# openssl enc -d -aes256 -in test.tar.gz | tar xz -C /mnt/

enter aes-256-cbc decryption password:

[root@ipsan-node03 mnt]# ls

a.txt  b.txt  c.txt  test.tar.gz

上述命令的解释:

-d  用于解密文件

-C  将加压后的文件提取到目标目录下

当你在本地网络或因特网工作的时候,你可以随时通过加密来保护你和他人共享的重要文本或文件,这有助于降低将其暴露给恶意攻击者的风险。

方法四:shc加密(仅仅对shell脚本加密)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

shc是一个专业的加密shell脚本的工具.它的作用是把shell脚本转换为一个可执行的二进制文件,这个办法很好的解决了脚本中含有IP、

密码等不希望公开的问题。

 

如果你的shell脚本包含了敏感的口令或者其它重要信息, 而且你不希望用户通过ps -ef(查看系统每个进程的状态)捕获敏感信息. 你可以

使用shc工具来给shell脚本增加一层额外的安全保护. shc是一个脚本编译工具, 使用RC4加密算法, 它能够把shell程序转换成二进制可执

行文件(支持静态链接和动态链接). 该工具能够很好的支持: 需要加密, 解密, 或者通过命令参数传递口令的环境.

 

shc的官网下载地址: 

http://www.datsi.fi.upm.es/~frosal/sources/

 

安装方法:

[root@ipsan-node03 ~]# cd /usr/local/src/

[root@ipsan-node03 src]# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz

[root@ipsan-node03 src]# tar -zvxf shc-3.8.9.tgz

[root@ipsan-node03 src]# cd shc-3.8.9

[root@ipsan-node03 shc-3.8.9]# mkdir -p /usr/local/man/man1

这步是必须的,不然安装过程中会报错,shc将安装命令到/usr/local/bin/目录下;

将帮助文档存放在/usr/local/man/man1/目录下,如果系统中无此目录,安装时会报错,可创建此目录后再执行安装

 

[root@ipsan-node03 shc-3.8.9]# make install

这是要回答yes或者y,不能直接回车,否则会报错

 

需要注意的是,sch只能能shell脚本文件进行加密,其他文件都不可以!

 

sch加密使用方法:

"-f"选项指定需要加密的程序

[root@ipsan-node03 ~]# ls

text.sh

[root@ipsan-node03 ~]# cat text.sh

#!/bin/bash

echo "hahaha"

[root@ipsan-node03 ~]# shc -r -f text.sh

[root@ipsan-node03 ~]# ls

text.sh  text.sh.x  text.sh.x.c

 

注意:要有-r选项, -f 后跟要加密的脚本名。

运行后会生成两个文件,script-name.x 和 script-name.x.c

script-name.x是加密后的可执行的二进制文件.

./script-name.x 即可运行.

script-name.x.c是生成script-name.x的原文件(c语言)

[root@ipsan-node03 ~]# ./text.sh

hahaha

[root@ipsan-node03 ~]# ./text.sh.x

hahaha

 

通常从安全角度考虑:

使用sch命令对shell脚本文件进行加密后,只需保留.x的二进制文件即可,其他两个文件均可以删除!

[root@ipsan-node03 ~]# ls

text.sh  text.sh.x  text.sh.x.c

[root@ipsan-node03 ~]# rm -rf text.sh

[root@ipsan-node03 ~]# rm -rf text.sh.x.c

[root@ipsan-node03 ~]# ls

text.sh.x

[root@ipsan-node03 ~]# ./text.sh.x

hahaha

 

另外:

shc还提供了一种设定有效执行期限的方法,可以首先使用shc将shell程序转化为二进制,并加上过期时间,如:

[root@ipsan-node03 ~]# shc -e 28/02/2018 -m "this script file is about to expire" -v -r -f text.sh

shc shll=bash

shc [-i]=-c

shc [-x]=exec '%s' "$@"

shc [-l]=

shc opts=

shc: cc  text.sh.x.c -o text.sh.x

shc: strip text.sh.x

shc: chmod go-r text.sh.x

[root@ipsan-node03 ~]# ls

text.sh  text.sh.x  text.sh.x.c

 

解释:

-e:指定过期时间为2018年2月28日

-m:过期后打印出的信息;

-v: verbose

-r: 可在相同操作系统的不同主机上执行

-f: 指定源shell

 

如果在过期后执行,则会有如下提示:

[root@ipsan-node03 ~]# ./text.sh.x

./text.sh.x: this script file is about to expire

使用以上方法要注意,需防止用户更改系统时间,可以通过在程序中加入自动更新系统时间的命令来解决此问题!!

 

sch的帮助命令:

[root@ipsan-node03 ~]# shc -help

shc Version 3.8.9, Generic Script Compiler

shc Copyright (c) 1994-2012 Francisco Rosales <frosal@fi.upm.es>

shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script

 

    -e %s  Expiration date in dd/mm/yyyy format [none]   (指定过期日期)

    -m %s  Message to display upon expiration ["Please contact your provider"]  (指定过期提示的信息)

    -f %s  File name of the script to compile   (指定要编译的shell的路径及文件名)

    -i %s  Inline option for the shell interpreter i.e: -e

    -x %s  eXec command, as a printf format i.e: exec('%s',@ARGV);

    -l %s  Last shell option i.e: --

    -r     Relax security. Make a redistributable binary   (可以相同操作系统的不同系统中执行)

    -v     Verbose compilation    (编译的详细情况)

    -D     Switch ON debug exec calls [OFF]

    -T     Allow binary to be traceable [no]

    -C     Display license and exit

    -A     Display abstract and exit

    -h     Display help and exit

 

    Environment variables used:

    Name    Default  Usage

    CC      cc       C compiler command

    CFLAGS  <none>   C compiler flags

 

    Please consult the shc(1) man page.

 

说明:

经测试,相同在操作系统,shc后的可执行二进制文件直接可以移植运行,但不同操作系统可能会出现问题,

比如将上面的test.sh.x的二进制文件在CentOS6.9上加密后移到redhat as5u4上不能运行,出现"Floating point exception"错误提示,

但移到另一台CentOS6.9上直接运行没问题。

方法五: ZIP加密
1)文件加密
使用命令"zip -e filename.zip filename" 即可出现输入密码的提示,输入2次密码。 此文件即被加密解压时候是需要密码的

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

下面开始为test.txt文件进行加密

[root@centos6-vm02 ~]# cat test.txt

this is a test!!!

[root@centos6-vm02 ~]# zip -e test.txt.zip test.txt         //如下进行加密操作时,需要输入两次密码

Enter password:                          

Verify password:

  adding: test.txt (stored 0%)

[root@centos6-vm02 ~]# ls

test.txt  test.txt.zip

进行解压的时候,需要输入密码

[root@centos6-vm02 ~]# rm -f test.txt

[root@centos6-vm02 ~]# unzip test.txt.zip

Archive:  test.txt.zip

[test.txt.zip] test.txt password:

 extracting: test.txt               

[root@centos6-vm02 ~]# cat test.txt

this is a test!!!

2)文件夹加密
使用命令"zip -re dirname.zip dirname"即可出现输入密码的提示,输入2次密码。 此文件即被加密解压时候是需要密码的。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

下面开始对目录进行加密

[root@centos6-vm02 ~]# mkdir dirtest

[root@centos6-vm02 ~]# cat dirtest/haha.txt

this is test of dir!!!

[root@centos6-vm02 ~]# zip -re dirtest.zip dirtest

Enter password:

Verify password:

  adding: dirtest/ (stored 0%)

  adding: dirtest/haha.txt (stored 0%)

解压目录时需要输入密码

[root@centos6-vm02 ~]# rm -rf dirtest

[root@centos6-vm02 ~]# unzip dirtest.zip

Archive:  dirtest.zip

   creating: dirtest/

[dirtest.zip] dirtest/haha.txt password:

 extracting: dirtest/haha.txt       

[root@centos6-vm02 ~]# ls dirtest

haha.txt

[root@centos6-vm02 ~]# cat dirtest/haha.txt

this is test of dir!!!

方法六:GnuPG加密
GnuPG的全称是GNU隐私保护(GNU Privacy Guard),常常被称为GPG,它结合了一组加密软件。它是由GNU项目用C编程语言编写的。最新的稳定版本是2.0.27。在如今的大多数Linux发行版中,gnupg程序包都是默认随带的,所以万一它没有安装,你可以使用apt或yum从软件库来安装它(yum install gnupg)。注意:gpg只能对文件进行加密,对目录则无法完成加密!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

下面开始使用GnuPG方式对test.txt文件进行加密

[root@centos6-vm02 ~]# cat test.txt

this is a test!!!

[root@centos6-vm02 ~]# gpg -c test.txt   

can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory         //这个信息可以忽略

注意:如上加密的时候,会弹出来一个对话框,要求Paraphrase输入两次密码,对这个特定的文件进行加密。

一旦运行带-c选项(完全使用对称密码算法加密)的gpc命令,它会生成一个文件.gpg文件。

[root@centos6-vm02 ~]# ll test.txt*

-rw-r--r--. 1 root root 18 Jan  4 10:08 test.txt

-rw-r--r--. 1 root root 61 Jan  4 10:04 test.txt.gpg

对文件进行加密后,最好将源文件删除!不要再保留源文件了!

[root@centos6-vm02 ~]# rm -f test.txt

文件解密操作。

注意出现Paraphrase提示时,需要提供加密时输入的同一个密码才能解密

[root@centos6-vm02 ~]# gpg test.txt.gpg  

gpg: 3DES encrypted data

can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory

gpg: encrypted with 1 passphrase

gpg: WARNING: message was not integrity protected

[root@centos6-vm02 ~]# ll test.txt*

-rw-r--r--. 1 root root 18 Jan  4 10:08 test.txt

-rw-r--r--. 1 root root 61 Jan  4 10:04 test.txt.gpg

[root@centos6-vm02 ~]# cat test.txt

this is a test!!!

***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************

Linux下文件加密方法总结相关推荐

  1. linux带密码解压密码,linux 下文件加密压缩和解压的方法

    方法一:用tar命令 对文件加密压缩和解压 压缩: [html] view plain copy tar -zcf - filename |openssl des3 -salt -k password ...

  2. linux 下文件加密压缩和解压的方法

    方法一:用tar命令 对文件加密压缩和解压 压缩: tar -zcf - filename |openssl des3 -salt -k password | dd of=filename.des3 ...

  3. linux给文件夹加密码,如何使用linux命令给文件上锁?linux命令文件加密方法

    如何用linux命令给文件夹加密上锁呢?这是很多linux用户会碰到的问题,今天小编给大家介绍下linux命令给文件加密上锁的方法. 怎么用linux命令给自己的文件上锁 原因: 有些时候一个工程需要 ...

  4. ubuntu下文件加密方法

    文件加密 openssl 密码方式加密 使用 openssl 加密一个文件 (data.zip 为原始文件,back.zip 为加密之后的文件) # openssl enc -e -aes256 -i ...

  5. linux下文件、文件夹权限的作用及设置方法(常规权限与SUID、SGID、SBIT、ACL、sudo、umask)

    linux下文件权限设置及其作用 文件权限与归属(读.写.执行权限) linux系统中一切皆文件,要说权限,有必要先了解一下文件的分类 Linux中的文件分类 在linux系统中一切都是文件,但是文件 ...

  6. linux 文件拆分 合并,Linux下文件的切分与合并的简单方法

    linux下文件分割可以通过split命令来实现,可以将一个大文件拆分成指定大小的多个文件,并且拆分速度非常的快,可以指定按行数分割和安大小分割两种模式.Linux下文件合并可以通过cat命令来实现, ...

  7. c++字符加密_linux安全Linux下RAR加密解密

    网络拓扑:Linux下RAR加密解密主机用户名:root   密码:123456第一步.打开网络拓扑,进入H-xclient-rar--1虚拟机. 第二步.创建实验文件test.txt,文件内容&qu ...

  8. linux中的文件夹压缩文件,linux将文件拷贝到目录下Linux下文件的压缩与打包详解...

    在Linux中,有很多的压缩命令.利用这些压缩命令,可以方便的从网络上下载大型的文件.同时,我们知道,Linux文件的扩展名是没有特殊意义的,不过,因为Linux下存在着许多压缩命令,所以为了方便记忆 ...

  9. Linux下文件的压缩与解压缩

    Linux下文件的压缩与解压缩与Windows环境下有较大的区别,在Windows下只需要安装类似Winrar工具就能解压缩大部分文件,而在Linux命令行下每一种文件都有不同的压缩和解压缩方法. 使 ...

最新文章

  1. html表格上下移动,Vue实现table上下移动功能示例
  2. “百亿补贴”真的能拯救一切吗?
  3. Scala模式匹配:条件守卫
  4. Visual Guide to NoSQL Systems
  5. python二进制、字符编码及文件操作
  6. 【java】docker容器内使用jstack等命令报错 The VM does not support the attach mechanism
  7. 硅谷VC想对CIO说这些
  8. 显示墙 显示服务器地址,云墙怎么看服务器地址
  9. 深度解密 Go 语言之基于信号的抢占式调度
  10. Oracle之触发器
  11. CentOS6.5搭建MySQL5.1主从复制
  12. sqlplus基础命令
  13. uniapp项目使用mescroll中mescroll-body组件记录
  14. ERP实施心得(转)
  15. 数据结构PTA 进阶实验5-3.2 新浪微博热门话题(分离链接法 )
  16. Java 结构化数据处理开源库 SPL
  17. gitup上传的坑 ! [rejected] master -> dev-gaochao (fetch first)......
  18. 教你用Python实现经典游戏《小蜜蜂》
  19. Ztree Fa-Awesome 图标使用
  20. Android 蓝牙 A2dp 编码SBC、AAC、Aptx、LDAC、LHDC aduio音频概述(2)

热门文章

  1. 自己动手架设linux下Web服务器(图)6
  2. Memcache安装 2
  3. 【高清截图】UbuntuKylin 14.04 桌面版安装步骤
  4. Python实例讲解 -- 图片处理
  5. php 显示ip所属地 (qq版)
  6. go语言sleep一纳秒不准的思考--CPU时间片
  7. mysql 命令备份恢复,binlog日志, percona工具备份恢复
  8. ubuntu环境下使用cat命令合并代码
  9. 【今日CV 计算机视觉论文速览 第112期】Mon, 6 May 2019
  10. 【今日CV 视觉论文速览】14 Nov 2018