几种字符串加密解密的方法

第一种:〔 Python 与 Bash Shell 的结合 〕
这个命令会让你输入一个字符串,然后会再输出一串加密了的数字。

加密代码[照直输入]:
python -c 'print reduce(lambda a,b: a*256+ord(b), raw_input("string: "), 0)'

解密代码[数字后+P]:
dc -e 输出的数字P


第二种:〔 应该是纯 Bash Shell,含 VIM 的 xxd 〕
用 gtalk@gmail.com 作为明文,加密分两步,当然了,也是可以一步过的,呆会说~

加密代码:
1、echo "gtalk@gmail.com" |xxd -ps -u
得到:6774616C6B40676D61696C2E636F6D0A
2、echo "ibase=16; 6774616C6B40676D61696C2E636F6D0A" |bc
得到:137514765985002236391382606438443478282

一步加密代码:
echo "ibase=16; $(echo "gtalk@gmail.com" |xxd -ps -u)" |bc
得到:137514765985002236391382606438443478282

解密代码:
3、dc -e 137514765985002236391382606438443478282P
得到:gtalk@gmail.com


第三种:〔 Base64 编码,这个很好很强大,适合写加密脚本 〕
同样用 gtalk@gmail.com 作为明文,来看代码:

加密代码:
echo "gtalk@gmail.com" |base64 -i
得到:Z3RhbGtAZ21haWwuY29tCg==

解密代码:
echo "Z3RhbGtAZ21haWwuY29tCg==" |base64 -d
得到:gtalk@gmail.com

 

 

用shell脚本对系统进行自动化维护,简单,便捷而且可移植性好.
但shell脚本是可读写的,很有可能会泄露敏感信息,如用户名,密码,路径,IP等.
同样,在shell脚本运行时会也泄露敏感信息.
请问如何不影响脚本运行的前提下,对脚本进行加密?

 

一、shc方法

 

shc是一个加密shell脚本的工具.它的作用是把shell脚本转换为一个可执行的二进制文件,这就很好的解决了上述问题.

 

yum安装:

 

yum -y install shc

 

编译安装:

 

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz
tar xvfz shc-3.8.7.tgz
cd shc-3.8.7
make

 

验证shc是否正确安装

 

[root@martin shc-3.8.7]# ./shc -v
shc parse(-f): No source file specified

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

创建一个示例Shell脚本

复制代码
[root@martin shc-3.8.7]# vi random.sh
#!/bin/bash
read -p "How many random numbers do you want to generate?" max
for (( start = 1; start <= $max; start++ ))
do
  echo -e $RANDOM
done
复制代码

给脚本增加可执行权限

[root@martin shc-3.8.7]# chmod u+x random.sh

执行示例脚本

复制代码
[root@martin shc-3.8.7]# ./random.sh
How many random numbers do you want to generate?3
14235
9555
7671
复制代码

使用shc加密Shell脚本

复制代码
[root@martin shc-3.8.7]# ./shc -v -r -T -f random.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc  random.sh.x.c -o random.sh.x
shc: strip random.sh.x
shc: chmod go-r random.sh.x
复制代码

运行后会生成两个文件,script-name.x 和 script-name.x.c
script-name.x是加密后的可执行的二进制文件
script-name.x.c是生成script-name.x的原文件(c语言)

[root@martin shc-3.8.7]# ll random.sh*
-rwxr-xr-x 1 root root   146 Aug  2 10:26 random.sh
-rwx--x--x 1 root root  9424 Aug  2 10:30 random.sh.x
-rw-r--r-- 1 root root 10080 Aug  2 10:30 random.sh.x.c

执行加密后的脚本

复制代码
[root@martin shc-3.8.7]# ./random.sh.x 
How many random numbers do you want to generate?3
28955
21487
29513
复制代码

还不完善,只能全路径执行shc命令或者进入目录内,加入全局环境变量/etc/profile未生效

二、gzexe

它是使用系统自带的gzexe程序,它不但加密,同时压缩文件

这种加密方式不是非常保险的方法,但是能够满足一般的加密用途,可以隐蔽脚本中的密码等信息。

使用方法:

[root@martin home]# gzexe random.sh 
random.sh:     20.5%
[root@martin home]# ll random.sh*
-rwxr-xr-x 1 root root 953 Aug  2 10:45 random.sh
-rwxr-xr-x 1 root root 146 Aug  2 10:45 random.sh~

它会把原来没有加密的文件备份为 file.sh~ ,同时 file.sh 即被变成加密文件

参考地址:

http://lidao.blog.51cto.com/3388056/1914205

https://yq.aliyun.com/ziliao/65848

 

posted @ 2018-06-25 23:22  konglingbin  阅读(38634)  评论(0编辑  收藏  举报