✨Shell脚本实现Base64 加密解密

加密算法


# !/bin/bash

# 全局变量
str=""

base64_encode_string(){
	# 源数据	
	source_string=$1
	echo "源数据:$1"
	# 判断是否为空
	if [ 0 -eq "${#source_string}" ]; then
		echo "输入为空,退出"

		return
	fi
	
	echo "正在执行..."
	# 编码集Wnrm0b7QPpI1FGBYVizZXN84vTJqocOuD9aHdgSlt2jRyAKEe6kCxhsfwL5M3U+/
	BASE64_CODE_TABLE=(W n r m 0 b 7 Q P p I 1 F G B Y V i z Z X N 8 4 v T J q o c O u D 9 a H d g S l t 2 j R y A K E e 6 k C x h s f w L 5 M 3 U + /)
	# 不知道
	FIRST_SOURCE_BYTE_DIVIDEND=0X04
	SECOND_SOURCE_BYTE_DIVIDEND=0X10
	THIRD_SOURCE_BYTE_DIVIDEND=0X40
	# 标记
	encode_state=1
	#
	last_source_byte=""
	# 结果
	result=""

	# 将字符转化成ASCII码值
	for((i=0;i<${#source_string};++i)){
		#curChar='printf "%d" "${#source_string:i:1}";'
		curChar=`printf "%d" "'${source_string:i:1}";`
		echo curChar=$curChar

	case $encode_state in
	1)
		
		index=$(( $curChar / $FIRST_SOURCE_BYTE_DIVIDEND ))
		# echo "INDEX IN STATE 1 IS : $index"
		result="$result${BASE64_CODE_TABLE[$index]}"
		echo "结果:" $result
		encode_state=2;;

	2)
		
		index_high=$(( ($last_source_byte % $FIRST_SOURCE_BYTE_DIVIDEND)* 0X10 ))
		index_low=$(( $curChar / $SECOND_SOURCE_BYTE_DIVIDEND ))
		index=$(( $index_high + $index_low ))
		# echo "index in state 2 is: $index"
		result="$result${BASE64_CODE_TABLE[$index]}"
		echo "结果:$result"
		encode_state=3;;
	3)
		index_high=$(( ($last_source_byte % $SECOND_SOURCE_BYTE_DIVIDEND) * 0X04 ))
		index_low=$(( $curChar / $THIRD_SOURCE_BYTE_DIVIDEND ))
		index=$(( $curChar % $THIRD_SOURCE_BYTE_DIVIDEND ))
		# echo "index2 in state 3 is: $index"
		result="$result${BASE64_CODE_TABLE[$index]}"
		echo "结果:$result"
		# 追加最后一位
                index=$(( $curChar % 0x10 ))
		result="$result${BASE64_CODE_TABLE[$index]}"
		echo "追加结果:$result"
		encode_state=1;;

	esac
	
	last_source_byte=$curChar
}

	# process pading
	case $encode_state in
	2)
		echo $FIRST_BYTE_DIVIDEND
		
		index=$(( ($last_source_byte % $FIRST_SOURCE_BYTE_DIVIDEND) * 0X10 ))
		#echo "index in pading state 2 is: $index"
		result="$result${BASE64_CODE_TABLE[$index]}==";;
	3)
		index=$(( ($last_source_byte % $SECOND_SOURCE_BYTE_DIVIDEND) * 0X04 ))
		#echo "index in pading state 3 is: $index"
		result="$result${BASE64_CODE_TABLE[$index]}=";;
	esac
	echo "添加后缀:$result"
	str=$result

}

# main函数

echo "开始加密..."

read m
echo $m
read n
# 第二种
echo "-------------------"

cat $1 |while read line
do
from=$line
base64_encode_string $from
#str=$?
# echo "输出:" $str
# 写入文件
echo "$str" >> to
done

# 加密

# 写入文件
#base64_encode_string $1

echo "加密结束..."

解密算法


# !/bin/bash

# 全局变量
str=""
_str=""
# 编码集Wnrm0b7QPpI1FGBYVizZXN84vTJqocOuD9aHdgSlt2jRyAKEe6kCxhsfwL5M3U+/
BASE64_CODE_TABLE=(W n r m 0 b 7 Q P p I 1 F G B Y V i z Z X N 8 4 v T J q o c O u D 9 a H d g S l t 2 j R y A K E e 6 k C x h s f w L 5 M 3 U + /)

# 16#---->ascii
fun(){
a=$1
b=`printf "%d" $a`
#echo $b |awk '{printf("%c", $1)}'
_str=`echo $b |awk '{printf("%c", $1)}'`
}


# 四个字符转化为正确的三个字符
get(){
	# get
	get=$1
	echo "数据源" $1
	# to
	to=""
	# size
	size=4
	if [[ $2 = 1 ]]; then
		size = 3
	elif [[ $2 = 2 ]]; then
		size =2
	fi
	for((i=0;i<size;++i))do
		# echo "循环I:$i ${get:i:1}"
		for((j=0;j<64;j++))do
			# echo "当前:$j ${BASE64_CODE_TABLE[$j]}"
			if [[ ${get:i:1} = ${BASE64_CODE_TABLE[$j]} ]]; then
				if [[ i < size-1 ]];then
					to=$(( ($to + $j)* 0x10))
					
					# 此时为十进制
					echo "匹配:$i $j $to ${get:i:1}"
				else
					to=$(($to + $j))
				fi
				break
			fi
		done
	done
	get=""
	# 十进制转化为十六进制
	echo "十六进制结果" $to
	if [[ size = 1 ]]; then
		to=$to/0x04
		tmp=$to / 0x100
		tmp="\x${tmp}"
		tmp=`printf "$tmp"`

		to=$to >> 8
		tmp="\x${to}"
		tmp=`printf "$tmp"`
		get=$get $tmp
	elif [[ size = 2 ]]; then
		to=$to/0x10
		tmp="\x${to}"
		tmp=`printf "$tmp"`
		get=$get $tmp
	else
		printf "%d" $(( ($to / 65536) ))
		tmp=$(( ($to / 65536) ))
		fun $tmp
		echo "_str:" $_str
		to=$(( ($to % 65536) ))
		tmp=$(( ($to / 256) ))
		# tmp="\x${tmp}"
		echo `printf "$tmp"`
		tmp=`printf $tmp`
		get="$get" "$tmp"
		to=$to >> 8
		get=$get `echo -e '\x${to % 0x100}'`
		to=$to >> 8
		get=$get `echo -e '\x${to % 0x100}'`
	fi
	echo "get----------->"
	echo $get
}

base64_encode_string(){
	# 源数据	
	source_string=$1
	last=0
	last_to=0

	# 结果
	 result=""

	# 判断是否为空
	if [ 0 -eq "${#source_string}" ]; then
	echo "输入为空,退出"
		return
	fi

	# 结尾标记
	end=0
	echo "结尾: ${source_string:${#source_string}-3}"

	if [ ${source_string:${#source_string}-2} = "==" ]; then
        	end=2
	elif [ ${source_string:${#source_string}-1} = "=" ]; then
        	end=1
	else
        	end=0
	fi

	# 判断字符串长度
	len=${#source_string}
	if [[ 4 = len ]]; then
		echo "字符串长度小于4"
		
	else
		echo "字符串长度大于4"
		m=${#source_string}-4
		for((i=0;i<m;))
		do
			get ${source_string:i:4} 0
			result=$result $?
			i = $i + 4
		done
	fi

	echo "对结尾进行处理..."
	last=${source_string:${#source_string}-4:4}
	echo $last
	case $end in
	0)
		;;
	1)
		;;
	2)
		get $last 2
		result=$result $?;;

	esac	

	# 编码集Wnrm0b7QPpI1FGBYVizZXN84vTJqocOuD9aHdgSlt2jRyAKEe6kCxhsfwL5M3U+/
	# BASE64_CODE_TABLE=(W n r m 0 b 7 Q P p I 1 F G B Y V i z Z X N 8 4 v T J q o c O u D 9 a H d g S l t 2 j R y A K E e 6 k C x h s f w L 5 M 3 U + /)
	# 第一个字符取前六位,所以是对0100取整去掉最后两位,第二个是前一个字符的后两位和这个字符的
	FIRST_SOURCE_BYTE_DIVIDEND=0X04
	SECOND_SOURCE_BYTE_DIVIDEND=0X10
	THIRD_SOURCE_BYTE_DIVIDEND=0X40
	# 标记
	encode_state=1
	#
	last_source_byte=""
	# 结果
	result=""

	# 将字符转化成ASCII码值
	for((i=0;i<${#source_string};++i)){
		#curChar='printf "%d" "${#source_string:i:1}";'
		curChar=`printf "%d" "'${source_string:i:1}";`
		echo curChar=$curChar

	case $encode_state in
	1)
		
		index=$(( $curChar / $FIRST_SOURCE_BYTE_DIVIDEND ))
		echo "INDEX IN STATE 1 IS : $index"
		result="$result${BASE64_CODE_TABLE[$index]}"
		encode_state=2;;

	2)
		
		index_high=$(( ($last_source_byte % $FIRST_SOURCE_BYTE_DIVIDEND)* 0X10 ))
		index_low=$(( $curChar / $SECOND_SOURCE_BYTE_DIVIDEND ))
		index=$(( $index_high + $index_low ))
		echo "index in state 2 is: $index"
		result="$result${BASE64_CODE_TABLE[$index]}"
		encode_state=3;;
	3)
		index_high=$(( ($last_source_byte % $SECOND_SOURCE_BYTE_DIVIDEND) * 0X04 ))
		index_low=$(( $curChar / $THIRD_SOURCE_BYTE_DIVIDEND ))
		index=$(( $curChar % $THIRD_SOURCE_BYTE_DIVIDEND ))
		echo "index2 in state 3 is: $index"
		result="$result${BASE64_CODE_TABLE[$index]}"
		encode_state=1;;

	esac
	
	last_source_byte=$curChar
}

	# process pading
	case $encode_state in
	2)
		echo $FIRST_BYTE_DIVIDEND
		
		index=$(( ($last_source_byte % $FIRST_SOURCE_BYTE_DIVIDEND) * 0X10 ))
		#echo "index in pading state 2 is: $index"
		result="$result${BASE64_CODE_TABLE[$index]}==";;
	3)
		index=$(( ($last_source_byte % $SECOND_SOURCE_BYTE_DIVIDEND) * 0X04 ))
		#echo "index in pading state 3 is: $index"
		result="$result${BASE64_CODE_TABLE[$index]}=";;
	esac

	str=$result

}

# main函数 $1输入文件 $2输出文件

echo "开始解密..."

# 第二种
echo "-------------------"

get TSkr 0

read m

cat $1 |while read line

do
from=$line
echo "输入参数:$from"
base64_encode_string $from
# 写入文件
echo "$str" >> $2

done

echo "解密结束..."

BaseTo有点问题,十六进制取高两位会报错,Base暂时没问题,应该

posted @ 2020-07-22 16:41  两小无猜  阅读(2683)  评论(0编辑  收藏  举报