shell實作Base64加解密
暫時不支持中文字符
腳本:
#!/bin/bash
#
#####################################################################
#
#Author: LiangDong
#Email: 395539184@qq.com
#Date: 2021-04-23
#FileName: base64.sh
#URL: https://github.com/ledrsnet
#Description: base64加解碼,暫不支持中文
#Copyright (C): 2021 All rights reserved
#
#####################################################################
#初始化base64編碼表和索引表(解碼使用)
baseCode=(`echo {A..Z} {a..z} {0..9} + /`)
declare -A baseIndexCode
for i in ${!baseCode[@]};do
baseIndexCode[${baseCode[$i]}]=$i
done
#錯誤碼
ERR_NULLPARM=2
#base64編碼
baseEncode(){
for((i=0;i<${#binaryStr};i+=24));do
buffers=${binaryStr:$i:24}
[ ${#buffers} -lt 24 ] && eqFlag=true
buffers+=`echo 000000000000000000000000|head -c $[24-${#buffers}]`
for((j=0;j<${#buffers};j+=6));do
tempBin=${buffers:$j:6}
#echo $tempBin
if [ $eqFlag = true ] && [ `echo "ibase=2;$tempBin"|bc` -eq 0 ];then
dataStr+="="
else
dataStr+=${baseCode[`echo "ibase=2;$tempBin"|bc`]}
fi
done
done
}
#base64解碼
baseDecode(){
for((i=0;i<${#binaryStr};i+=8));do
buffers=${binaryStr:$i:8}
[ ${#buffers} -lt 8 ] && break;
dataStr+=`echo "ibase=2;$buffers"|bc|awk '{printf("%c"),$buffers}'`
done
}
#臨時變數
binaryStr=""
dataStr=""
eqFlag=false
[ $# -lt 2 ] && { echo "Usage: `basename $0` str encode|decode ";exit $ERR_NULLPARM; }
#展開二進制位
for((i=0;i<${#1};i++));do
if [ "$2" = "encode" ];then
binaryStr+=$(echo "obase=2;`printf "%d" "'${1:$i:1}"`"|bc|xargs printf "%08d")
elif [ "$2" = "decode" ];then
[ ${1:$i:1} = "=" ] && continue
binaryStr+=$(echo "obase=2;${baseIndexCode[${1:$i:1}]}"|bc|xargs printf "%06d")
#echo ${baseIndexCode[${1:$i:1}]}
#echo $binaryStr
fi
done
#echo $binaryStr
#echo ${#binaryStr}
case "$2" in
"encode")
baseEncode
;;
"decode")
baseDecode
;;
*)
;;
esac
echo $dataStr
效果:
[root@maple-c8 ~]# ./base64.sh hahanihaoa encode
aGFoYW5paGFvYQ==
[root@maple-c8 ~]# ./base64.sh aGFoYW5paGFvYQ== decode
hahanihaoa
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/279518.html
標籤:其他