셸 Base 64 복호화 실현

셸 Base 64 복호화 실현
중국어 문자 지원 하지 않 음
스 크 립 트:
#!/bin/bash
#
#####################################################################
#
#Author:                LiangDong
#Email:                 [email protected]
#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

좋은 웹페이지 즐겨찾기