linux中常用的加密总结–base64编码解码与openssl实现
一、base64
!!!base64是一种数据编码方式,可逆,不是严格意义上的加密算法,不能作为安全加密使用
-d,–decode //解码数据
-i,–ignore-garbag //解码时忽略非字母字符
-w,–wrap=字符数 //在指定的字符数后自动换行(默认为76),0为禁用自动换行
三种编码密与对应解码形式:
1.echo 要编码的字符串 | base64
# 编码:
> echo "运维小菜" | base64
6L+Q57u05bCP6I+cCg==
# 解码:
> echo 6L+Q57u05bCP6I+cCg== | base64 -d
运维小菜
2.base64 file.txt,其中file.txt中内容为“运维小菜”
# 编码:
> base64 file.txt | tee encrypt.txt
6L+Q57u05bCP6I+cCg==
# 解码:
> base64 -d encrypt.txt
运维小菜
3.交互方式
# 编码:
> base64
运维小菜 #先回车后Ctrl+d
6L+Q57u05bCP6I+cCg==
# 解码:
> base64 -d
6L+Q57u05bCP6I+cCg== #先回车后Ctrl+d
运维小菜
4.高级用法–将多个文件编码为字符串
-rw-r–r-- 1 root root 21 Mar 5 14:57 a.txt
-rw-r–r-- 1 root root 13 Mar 5 14:53 b.txt
-rw-r–r-- 1 root root 757 Mar 3 17:11 c.txt
# 打包压缩:
> zip abc.zip a.txt b.txt c.txt
adding: a.txt (stored 0%)
adding: b.txt (stored 0%)
adding: c.txt (deflated 36%)
# 编码:
> base64 abc.zip | tee encrypt.txt
UEsDBA………………AAAAAA== #省略编码字符串
# 解码:
> base64 -d encrypt.txt > abcdecrypt.zip
> ll
total 8
-rw-r--r-- 1 root root 955 Mar 5 15:07 abcdecrypt.zip
-rw-r--r-- 1 root root 1293 Mar 5 15:05 encrypt.txt
# 解压:
> unzip abcdecrypt.zip -d unzip
> ls unzip/
a.txt b.txt c.txt
二、openssl
使用openssl实现base64编码与解码
1.简单编码字符串(base64编码与解码)
# 编码:
> echo 运维小菜 | openssl enc -a [-e]#-a等价于-base64 加密使用-e(默认,可省略)
6L+Q57u05bCP6I+cCg==
# 解码:
> echo 6L+Q57u05bCP6I+cCg== | openssl enc -a -d
运维小菜
2.编码文件(base64编码与解码)
# 编码:
> openssl enc -a -in file.txt | tee encrypt.txt
6L+Q57u05bCP6I+cCg==
# 解码:
> openssl enc -a -in encrypt.txt -d
运维小菜
3.将多个文件编码(base64编码与反编码)
# 压缩后编码:
> openssl enc -a -in abcdecrypt.zip -out encrypt.txt
> cat encrypt.txt
UEsDBAoAAAA…………AAMQCAAAAAA== #省略编码字符串
# 解码:
> openssl enc -a -d -in encrypt.txt -out abcdecrypt.zip
# 解压:
> unzip abcdecrypt.zip -d abcdecrypt
> ls abcdecrypt
a.txt b.txt c.txt
!!!以上都为base64(-a代表-base64)编码与反编码,没有使用加密算法,下一篇介绍使用加解密算法的openssl加解密(点击进入)