pintcat

cypher - encodes & decodes ASCII data

Mar 12th, 2024 (edited)
1,525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.02 KB | Software | 0 0
  1. #!/bin/sh
  2. # cypher v2.10 - encodes & decodes all kinds of files
  3. # Feed $CYPHER_KEY with a password 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. [ -f "$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)
  35.     IFS=$DEF_IFS
  36. }
  37.  
  38. DEF_IFS=$IFS
  39. WH_MAX=31
  40. WH_ALGO=WH_OLD
  41.  
  42. if [ -z "$2" ]; then
  43.     WH_LOOP=0
  44.     for NUM in $(tr -s 'A-Zg-z' ' ' < "$1"); do
  45.         if [ -z $S1 ]; then
  46.             S1=$NUM
  47.         elif [ -z $S2 ]; then
  48.             S2=$NUM
  49.         elif [ -z $S3 ]; then
  50.             S3=$NUM
  51.         elif [ $WH_LOOP -eq 0 ]; then
  52.             WH_LOOP=$NUM
  53.             GET_ARRAY
  54.         else
  55.             NUM=$(printf %d 0x$NUM)
  56.             [ $((SEG=$SEG+1)) -gt 1024 ] && SEG=1
  57.             ROT=$(($(printf "$ROT_ARRAY" | cut -d" " -f$SEG)+1))
  58.             printf $(printf '%b' '\\'$(((($NUM << $ROT)/$ROT) >> $ROT)))
  59.         fi
  60.     done
  61. else
  62.     if [ -e "$2" ]; then
  63.         read -r -p "Output file already exists. Overwrite (y/N)? " OPT
  64.         case $OPT in
  65.             y|Y);;
  66.             *)
  67.                 ERROR "Output flile already exists and won't be replaced.\n"
  68.                 ;;
  69.         esac
  70.     fi
  71.     S1=$(($(WH_RANDOM)+1))
  72.     S2=$(($(WH_RANDOM)+1))
  73.     S3=$(($(WH_RANDOM)+1))
  74.     [ $((WH_LOOP=$(stat -c '%s' "$1")+3)) -gt 1024 ] && WH_LOOP=1024
  75.     RL=$("$GEN" -fnsc$WH_LOOP -e97-102 -d" ")
  76.     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
  77.     GET_ARRAY
  78.     for NUM in $(od --endian=big -to1 -An -v "$1"); do
  79.         [ $((SEG=$SEG+1)) -gt 1024 ] && SEG=1
  80.         ROT=$(($(printf "$ROT_ARRAY" | cut -d" " -f$SEG)+1))
  81.         NUM=$(((((${NUM#${NUM%%[1-9]*}}+0) << $ROT)*$ROT) >> $ROT))
  82.         printf $(printf %x $NUM)$(printf "$RL" | cut -d" " -f$SEG) >> $2
  83.     done
  84. fi
  85.  
  86. #echo $(($(printf "aBcD" | od --endian=big -to1 -An -v | tr -s " " "8") << 8))
  87. ## converts "aBcD" in "2084303432048154624"
  88.  
  89. #for NUM in $(printf $((2084303432048154624 >> 8)) | tr -s "8" " "); do
  90. #   printf $(printf '%b' '\\'"$NUM")
  91. #done
  92. ## converts "2084303432048154624" back to "aBcD"
  93.  
Advertisement
Add Comment
Please, Sign In to add comment