Return to Snippet

Revision: 46893
at May 27, 2011 07:28 by crackiron


Initial Code
#!/bin/bash

if !(( $# == 2 )); then
	echo "Uso: ${0} [-d|-e] string"
	echo "Encode: ${0} -e string"
	echo "Decode: ${0} -d string"
	exit 2
fi

# Decoding
function deTrithemius(){

	echo "Decoding ... ${1}"
	hash1="$1"
	table="ABCDEFGHIJKLMNNOPQRSTUVWXYZ"
	i=0
	pos=""
	x=1

	while (( x<$( echo "`echo $hash1 | wc -c`+1" | bc ) )); do

		letra=`echo $hash1 | cut -c$(echo "${x}"|bc)`

		if ( `echo ${letra} | grep "[A-Z]" > /dev/null` ); then
			pos=$(echo "`expr index "${table}" "${letra}"`-${i}" | bc)
			
			while (( pos<=0 )); do
				((pos=$( echo "${pos}+`echo -n $table | wc -c`" | bc ) ))
			done
			
			echo -n "${table}" | cut -c${pos} | tr '\n' '\0'
			(( i+=1 )) 

		else
			echo -n "${letra}"
		fi

		(( x+=1 ))
	done

	echo ""
}

# Encoding
function Trithemius(){

	echo "Encoding ... ${1}"
        hash1="$1"
        table="ABCDEFGHIJKLMNNOPQRSTUVWXYZ"
        i=0
        pos=""
        x=1

        while (( x<$( echo "`echo $hash1 | wc -c`+1" | bc ) )); do

                letra=`echo $hash1 | cut -c$(echo "${x}"|bc)`

		if ( `echo ${letra} | grep "[A-Z]" > /dev/null` ); then
                        pos=$(echo "`expr index "${table}" "${letra}"`+${i}" | bc)

                        while (( pos>`echo -n $table | wc -c` )); do
                                ((pos=${pos}-`echo -n $table | wc -c`))
                        done

                        echo -n "${table}" | cut -c${pos} | tr '\n' '\0'
                        (( i+=1 ))

                else
                        echo -n "${letra}"
                fi

                (( x+=1 ))
        done

        echo ""
}

typeset -u entrada="${2}"

case $1 in

	"-d" )	deTrithemius "$entrada"
		;;

	"-e" )	Trithemius "$entrada"
		;;

	* )	echo "Uso: ${0} [-d|-e] string"
        	echo "Encode: ${0} -e string"
        	echo "Decode: ${0} -d string"
        	exit 2
		;;

esac

Initial URL


Initial Description


Initial Title
Trithemius Encoder/Decoder

Initial Tags


Initial Language
Bash