Advertisement
pintcat

cypher - encodes & decodes ASCII data

Mar 12th, 2024 (edited)
1,248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.52 KB | Software | 0 0
  1. #!/bin/sh
  2. # cypher v2.11 - encodes & decodes all kinds of files
  3. # Use $CYPHER_KEY to produce a unique encryption. When decrypting, only the exact same key will give a valid result.
  4.  
  5. [ -z "$CYPHER_KEY" ] && CYPHER_KEY="1234abcd"
  6.  
  7. ERROR(){
  8.     printf "\033[0;31mError - $1\033[0m"
  9.     exit 1
  10. }
  11.  
  12. # Dependencies: whrandom.sh, gen.sh, md5sum
  13. WHRANDOM=$(type whrandom.sh) && WHRANDOM="/"${WHRANDOM##* /}
  14. [ -x "$WHRANDOM" ] || ERROR_MSG="whrandom.sh is missing\n"
  15. GEN=$(type gen.sh) && GEN="/"${GEN##* /}
  16. [ -x "$GEN" ] || ERROR_MSG=$ERROR_MSG"gen.sh is either missing or not executable\n"
  17. MD5=$(type md5sum) && MD5="/"${MD5##* /}
  18. [ -x "$MD5" ] || ERROR_MSG=$ERROR_MSG"md5sum is either missing or not executable\n"
  19. [ -n "$ERROR_MSG" ] && ERROR "$ERROR_MSG"
  20. [ -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"
  21. . "$WHRANDOM"
  22.  
  23. GET_ARRAY(){
  24.     IFS="$(printf '\nx')"
  25.     [ -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
  26.         SEG=2
  27.         S1=$(($S1+$(printf $KEY_ARRAY | cut -d" " -f$SEG)))
  28.         SEG=$(($SEG+1))
  29.         S2=$(($S2+$(printf $KEY_ARRAY | cut -d" " -f$SEG)))
  30.         SEG=$(($SEG+1))
  31.         S3=$(($S3+$(printf $KEY_ARRAY | cut -d" " -f$SEG)))
  32.     done
  33.     WH_DEL=" "
  34.     ROT_ARRAY=$(WH_RANDOM $S1 $S2 $S3 | sed 's/.$//')
  35.     DEF_ARRAY=$ROT_ARRAY
  36.     IFS=$DEF_IFS
  37. }
  38.  
  39. DEF_IFS=$IFS
  40. WH_MAX=31
  41. WH_ALGO=WH_OLD
  42.  
  43. if [ -z "$2" ]; then
  44.     WH_LOOP=0
  45.     for NUM in $(tr -s 'A-Zg-z' ' ' < "$1"); do
  46.         if [ -z $S1 ]; then
  47.             S1=$NUM
  48.         elif [ -z $S2 ]; then
  49.             S2=$NUM
  50.         elif [ -z $S3 ]; then
  51.             S3=$NUM
  52.         elif [ $WH_LOOP -eq 0 ]; then
  53.             WH_LOOP=$NUM
  54.             GET_ARRAY
  55.         else
  56.             NUM=$(printf %d 0x$NUM)
  57.             if [ $((SEG=$SEG+1)) -gt 1024 ]; then
  58.                 SEG=1
  59.                 ROT_ARRAY=$DEF_ARRAY
  60.             fi
  61.             ROT=$((${ROT_ARRAY#${ROT_ARRAY%?}}+1))
  62.             ROT_ARRAY=${ROT_ARRAY%" "*}
  63.             printf $(printf '%b' '\\'$(((($NUM << $ROT)/$ROT) >> $ROT)))
  64.         fi
  65.     done
  66. else
  67.     if [ -e "$2" ]; then
  68.         read -r -p "Output file already exists. Overwrite (y/N)? " OPT
  69.         case $OPT in
  70.             y|Y);;
  71.             *)
  72.                 ERROR "Output flile already exists and won't be replaced.\n"
  73.                 ;;
  74.         esac
  75.     fi
  76.     S1=$(($(WH_RANDOM)+1))
  77.     S2=$(($(WH_RANDOM)+1))
  78.     S3=$(($(WH_RANDOM)+1))
  79.     [ $((WH_LOOP=$(stat -c '%s' "$1")+3)) -gt 1024 ] && WH_LOOP=1024
  80.     RL=$("$GEN" -fnsc$WH_LOOP -e97-102) # -d" ")
  81.     DEF_RL=$RL
  82.     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
  83.     GET_ARRAY
  84.     for NUM in $(od --endian=big -to1 -An -v "$1"); do
  85.         if [ $((SEG=$SEG+1)) -gt 1024 ]; then
  86.             SEG=1
  87.             ROT_ARRAY=$DEF_ARRAY
  88.             RL=$DEF_RL
  89.         fi
  90.         ROT=$((${ROT_ARRAY#${ROT_ARRAY%?}}+1))
  91.         ROT_ARRAY=${ROT_ARRAY%" "*}
  92.         NUM=$(((((${NUM#${NUM%%[1-9]*}}+0) << $ROT)*$ROT) >> $ROT))
  93.         printf $(printf %x $NUM)${RL#${RL%?}} >> $2
  94.         RL=${RL%?}
  95.     done
  96.  
  97. # Grouped segments:
  98. #   for CHUNK in $(cat "$1" | fold -w4); do
  99. #       [ $((SEG=$SEG+1)) -gt 1024 ] && SEG=1
  100. #       ROT=$(($(printf "$ROT_ARRAY" | cut -d" " -f$SEG)+1))
  101. #       NUM=$(((($(printf "$CHUNK" | od --endian=big -to1 -An -v | tr -s " " "8") << $ROT)*$ROT) >> $ROT))
  102. #       printf $(printf %x $NUM)$(printf "$RL" | cut -d" " -f$SEG) >> $2
  103. #   done
  104. fi
  105.  
  106. #echo $(($(printf "aBcD" | od --endian=big -to1 -An -v | tr -s " " "8") << 8))
  107. ## converts "aBcD" in "2084303432048154624"
  108.  
  109. #for NUM in $(printf $((2084303432048154624 >> 8)) | tr -s "8" " "); do
  110. #   printf $(printf '%b' '\\'"$NUM")
  111. #done
  112. ## converts "2084303432048154624" back to "aBcD"
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement