Posted By

crackiron on 05/27/11


Tagged


Versions (?)

Trithemius Encoder/Decoder


 / Published in: Bash
 

  1. #!/bin/bash
  2.  
  3. if !(( $# == 2 )); then
  4. echo "Uso: ${0} [-d|-e] string"
  5. echo "Encode: ${0} -e string"
  6. echo "Decode: ${0} -d string"
  7. exit 2
  8. fi
  9.  
  10. # Decoding
  11. function deTrithemius(){
  12.  
  13. echo "Decoding ... ${1}"
  14. hash1="$1"
  15. table="ABCDEFGHIJKLMNNOPQRSTUVWXYZ"
  16. i=0
  17. pos=""
  18. x=1
  19.  
  20. while (( x<$( echo "`echo $hash1 | wc -c`+1" | bc ) )); do
  21.  
  22. letra=`echo $hash1 | cut -c$(echo "${x}"|bc)`
  23.  
  24. if ( `echo ${letra} | grep "[A-Z]" > /dev/null` ); then
  25. pos=$(echo "`expr index "${table}" "${letra}"`-${i}" | bc)
  26.  
  27. while (( pos<=0 )); do
  28. ((pos=$( echo "${pos}+`echo -n $table | wc -c`" | bc ) ))
  29. done
  30.  
  31. echo -n "${table}" | cut -c${pos} | tr '\n' '\0'
  32. (( i+=1 ))
  33.  
  34. else
  35. echo -n "${letra}"
  36. fi
  37.  
  38. (( x+=1 ))
  39. done
  40.  
  41. echo ""
  42. }
  43.  
  44. # Encoding
  45. function Trithemius(){
  46.  
  47. echo "Encoding ... ${1}"
  48. hash1="$1"
  49. table="ABCDEFGHIJKLMNNOPQRSTUVWXYZ"
  50. i=0
  51. pos=""
  52. x=1
  53.  
  54. while (( x<$( echo "`echo $hash1 | wc -c`+1" | bc ) )); do
  55.  
  56. letra=`echo $hash1 | cut -c$(echo "${x}"|bc)`
  57.  
  58. if ( `echo ${letra} | grep "[A-Z]" > /dev/null` ); then
  59. pos=$(echo "`expr index "${table}" "${letra}"`+${i}" | bc)
  60.  
  61. while (( pos>`echo -n $table | wc -c` )); do
  62. ((pos=${pos}-`echo -n $table | wc -c`))
  63. done
  64.  
  65. echo -n "${table}" | cut -c${pos} | tr '\n' '\0'
  66. (( i+=1 ))
  67.  
  68. else
  69. echo -n "${letra}"
  70. fi
  71.  
  72. (( x+=1 ))
  73. done
  74.  
  75. echo ""
  76. }
  77.  
  78. typeset -u entrada="${2}"
  79.  
  80. case $1 in
  81.  
  82. "-d" ) deTrithemius "$entrada"
  83. ;;
  84.  
  85. "-e" ) Trithemius "$entrada"
  86. ;;
  87.  
  88. * ) echo "Uso: ${0} [-d|-e] string"
  89. echo "Encode: ${0} -e string"
  90. echo "Decode: ${0} -d string"
  91. exit 2
  92. ;;
  93.  
  94. esac

Report this snippet  

You need to login to post a comment.