Advertisement
pintcat

cypher - encodes & decodes ASCII data

Mar 12th, 2024 (edited)
1,081
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.21 KB | Software | 0 0
  1. #!/bin/sh
  2. # cypher v2.4 - encodes & decodes ASCII data
  3. # Use this key to produce a unique encryption. When decrypting, only the exact same key will give a valid result.
  4. KEY="1234abcd"
  5.  
  6. . whrandom.sh
  7.  
  8. ERROR(){
  9.     printf "\033[0;31mError - $1\033[0;32m\n"
  10.     exit 1
  11. }
  12.  
  13. GET_ARRAY(){
  14.     IFS="$(printf '\nx')"
  15.     [ -n "$KEY" ] && for KEY_ARRAY in $(printf "$KEY" | md5sum -b | cut -d" " -f1 | od --endian=big -td1 -An -v -w3 | tr -s " "); do
  16.         SEG=2
  17.         S1=$(($S1+$(printf $KEY_ARRAY | cut -d" " -f$SEG)))
  18.         SEG=$(($SEG+1))
  19.         S2=$(($S2+$(printf $KEY_ARRAY | cut -d" " -f$SEG)))
  20.         SEG=$(($SEG+1))
  21.         S3=$(($S3+$(printf $KEY_ARRAY | cut -d" " -f$SEG)))
  22.     done
  23.     WH_DEL=" "
  24.     ROT_ARRAY=$(WH_RANDOM $S1 $S2 $S3)
  25.     IFS=$DEF_IFS
  26. }
  27.  
  28. [ -z "$1" -o ! -e "$1" ] && ERROR "Wrong input path or file name or none at all.\nUsage: cypher.sh [IN] [OUT] to encode [IN]; cypher.sh [IN] to decode [IN]."
  29.  
  30. DEF_IFS=$IFS
  31. WH_MAX=31
  32. WH_ALGO=WH_OLD
  33.  
  34. if [ -z "$2" ]; then
  35.     WH_LOOP=0
  36.     for NUM in $(tr -s 'A-Zg-z' ' ' < "$1"); do
  37.         if [ -z $S1 ]; then
  38.             S1=$NUM
  39.         elif [ -z $S2 ]; then
  40.             S2=$NUM
  41.         elif [ -z $S3 ]; then
  42.             S3=$NUM
  43.         elif [ $WH_LOOP -eq 0 ]; then
  44.             WH_LOOP=$NUM
  45.             GET_ARRAY
  46.         else
  47.             NUM=$(printf %d 0x$NUM)
  48.             [ $((SEG=$SEG+1)) -gt 1024 ] && SEG=1
  49.             ROT=$(($(printf "$ROT_ARRAY" | cut -d" " -f$SEG)+1))
  50.             printf $(printf '%b' '\\'$(((($NUM << $ROT)/$ROT) >> $ROT)))
  51.         fi
  52.     done
  53. else
  54.     if [ -e "$2" ]; then
  55.         read -r -p "Output file already exists. Overwrite (y/N)? " OPT
  56.         case $OPT in
  57.             y|Y);;
  58.             *)
  59.                 ERROR "Output flile already exists and won't be replaced."
  60.                 ;;
  61.         esac
  62.     fi
  63.     S1=$(($(WH_RANDOM)+1))
  64.     S2=$(($(WH_RANDOM)+1))
  65.     S3=$(($(WH_RANDOM)+1))
  66.     [ $((WH_LOOP=$(stat -c '%s' "$1")+3)) -gt 1024 ] && WH_LOOP=1024
  67.     RL=$(gen.sh -fnsc$WH_LOOP -e97-102 -d" ")
  68.     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
  69.     GET_ARRAY
  70.     for NUM in $(od --endian=big -to1 -An -v "$1"); do
  71.         [ $((SEG=$SEG+1)) -gt 1024 ] && SEG=1
  72.         ROT=$(($(printf "$ROT_ARRAY" | cut -d" " -f$SEG)+1))
  73.         NUM=$(((((${NUM#${NUM%%[1-9]*}}+0) << $ROT)*$ROT) >> $ROT))
  74.         printf $(printf %x $NUM)$(printf "$RL" | cut -d" " -f$SEG) >> $2
  75.     done
  76. fi
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement