Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # cypher v2.11 - encodes & decodes all kinds of files
- # Use $CYPHER_KEY to produce a unique encryption. When decrypting, only the exact same key will give a valid result.
- [ -z "$CYPHER_KEY" ] && CYPHER_KEY="1234abcd"
- ERROR(){
- printf "\033[0;31mError - $1\033[0m"
- exit 1
- }
- # Dependencies: whrandom.sh, gen.sh, md5sum
- WHRANDOM=$(type whrandom.sh) && WHRANDOM="/"${WHRANDOM##* /}
- [ -x "$WHRANDOM" ] || ERROR_MSG="whrandom.sh is missing\n"
- GEN=$(type gen.sh) && GEN="/"${GEN##* /}
- [ -x "$GEN" ] || ERROR_MSG=$ERROR_MSG"gen.sh is either missing or not executable\n"
- MD5=$(type md5sum) && MD5="/"${MD5##* /}
- [ -x "$MD5" ] || ERROR_MSG=$ERROR_MSG"md5sum is either missing or not executable\n"
- [ -n "$ERROR_MSG" ] && ERROR "$ERROR_MSG"
- [ -z "$1" -o ! -e "$1" ] && ERROR "Wrong input path, file name or none at all.\nUsage: cypher.sh [IN] [OUT] to encode [IN]; cypher.sh [IN] to decode [IN].\n"
- . "$WHRANDOM"
- GET_ARRAY(){
- IFS="$(printf '\nx')"
- [ -n "$CYPHER_KEY" ] && for KEY_ARRAY in $(printf "$CYPHER_KEY" | $MD5 -b | cut -d" " -f1 | od --endian=big -td1 -An -v -w3 | tr -s " "); do
- SEG=2
- S1=$(($S1+$(printf $KEY_ARRAY | cut -d" " -f$SEG)))
- SEG=$(($SEG+1))
- S2=$(($S2+$(printf $KEY_ARRAY | cut -d" " -f$SEG)))
- SEG=$(($SEG+1))
- S3=$(($S3+$(printf $KEY_ARRAY | cut -d" " -f$SEG)))
- done
- WH_DEL=" "
- ROT_ARRAY=$(WH_RANDOM $S1 $S2 $S3 | sed 's/.$//')
- DEF_ARRAY=$ROT_ARRAY
- IFS=$DEF_IFS
- }
- DEF_IFS=$IFS
- WH_MAX=31
- WH_ALGO=WH_OLD
- if [ -z "$2" ]; then
- WH_LOOP=0
- for NUM in $(tr -s 'A-Zg-z' ' ' < "$1"); do
- if [ -z $S1 ]; then
- S1=$NUM
- elif [ -z $S2 ]; then
- S2=$NUM
- elif [ -z $S3 ]; then
- S3=$NUM
- elif [ $WH_LOOP -eq 0 ]; then
- WH_LOOP=$NUM
- GET_ARRAY
- else
- NUM=$(printf %d 0x$NUM)
- if [ $((SEG=$SEG+1)) -gt 1024 ]; then
- SEG=1
- ROT_ARRAY=$DEF_ARRAY
- fi
- ROT=$((${ROT_ARRAY#${ROT_ARRAY%?}}+1))
- ROT_ARRAY=${ROT_ARRAY%" "*}
- printf $(printf '%b' '\\'$(((($NUM << $ROT)/$ROT) >> $ROT)))
- fi
- done
- else
- if [ -e "$2" ]; then
- read -r -p "Output file already exists. Overwrite (y/N)? " OPT
- case $OPT in
- y|Y);;
- *)
- ERROR "Output flile already exists and won't be replaced.\n"
- ;;
- esac
- fi
- S1=$(($(WH_RANDOM)+1))
- S2=$(($(WH_RANDOM)+1))
- S3=$(($(WH_RANDOM)+1))
- [ $((WH_LOOP=$(stat -c '%s' "$1")+3)) -gt 1024 ] && WH_LOOP=1024
- RL=$("$GEN" -fnsc$WH_LOOP -e97-102) # -d" ")
- DEF_RL=$RL
- printf $S1$(printf "$RL" | cut -d" " -f1)$S2$(printf "$RL" | cut -d" " -f2)$S3$(printf "$RL" | cut -d" " -f3)$WH_LOOP$(printf "$RL" | cut -d" " -f4) > $2
- GET_ARRAY
- for NUM in $(od --endian=big -to1 -An -v "$1"); do
- if [ $((SEG=$SEG+1)) -gt 1024 ]; then
- SEG=1
- ROT_ARRAY=$DEF_ARRAY
- RL=$DEF_RL
- fi
- ROT=$((${ROT_ARRAY#${ROT_ARRAY%?}}+1))
- ROT_ARRAY=${ROT_ARRAY%" "*}
- NUM=$(((((${NUM#${NUM%%[1-9]*}}+0) << $ROT)*$ROT) >> $ROT))
- printf $(printf %x $NUM)${RL#${RL%?}} >> $2
- RL=${RL%?}
- done
- # Grouped segments:
- # for CHUNK in $(cat "$1" | fold -w4); do
- # [ $((SEG=$SEG+1)) -gt 1024 ] && SEG=1
- # ROT=$(($(printf "$ROT_ARRAY" | cut -d" " -f$SEG)+1))
- # NUM=$(((($(printf "$CHUNK" | od --endian=big -to1 -An -v | tr -s " " "8") << $ROT)*$ROT) >> $ROT))
- # printf $(printf %x $NUM)$(printf "$RL" | cut -d" " -f$SEG) >> $2
- # done
- fi
- #echo $(($(printf "aBcD" | od --endian=big -to1 -An -v | tr -s " " "8") << 8))
- ## converts "aBcD" in "2084303432048154624"
- #for NUM in $(printf $((2084303432048154624 >> 8)) | tr -s "8" " "); do
- # printf $(printf '%b' '\\'"$NUM")
- #done
- ## converts "2084303432048154624" back to "aBcD"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement