-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_decoder.sh
executable file
·136 lines (122 loc) · 3.58 KB
/
simple_decoder.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
# decode from Base64
decode_base64() {
if command -v base64 &> /dev/null; then
echo -n "$1" | base64 -d
else
echo "Base64 decoding tool not found."
fi
}
# decode from Base32
decode_base32() {
if command -v base32 &> /dev/null; then
echo -n "$1" | base32 -d
else
echo "Base32 decoding tool not found."
fi
}
# decode from Base16 (Hexadecimal)
decode_base16() {
if command -v xxd &> /dev/null; then
echo -n "$1" | xxd -r -p
else
echo "Hexadecimal decoding tool not found."
fi
}
# decode HTML entities
decode_html() {
echo "$1" | sed 's/&/\&/g; s/</</g; s/>/>/g; s/"/"/g; s/'/'\''/g'
}
# decode URL-encoded strings
decode_url() {
echo "$1" | python3 -c "import urllib.parse, sys; print(urllib.parse.unquote(sys.stdin.read().strip()))"
}
# decode Binary encoded strings
decode_binary() {
binary_string=$(echo "$1" | tr -d ' ')
length=${#binary_string}
decoded_string=""
for ((i=0; i<length; i+=8)); do
byte=${binary_string:i:8}
decimal=$((2#$byte))
decoded_string+=$(printf "\\x%x" "$decimal")
done
echo -e "$decoded_string"
}
# decode Octal encoded strings
decode_octal() {
echo "$1" | awk '{for(i=1;i<=NF;i++)printf("%c",strtonum("0"$i));print""}'
}
# detect encoding type
detect_encoding() {
if echo "$1" | grep -qE '^[0-9A-Fa-f]+$'; then
echo "The string appears to be Base16 (Hexadecimal)."
return 1
elif echo "$1" | grep -qE '^[A-Z2-7=]+$'; then
echo "The string appears to be Base32."
return 2
elif echo "$1" | grep -qE '^[A-Za-z0-9+/=]+$'; then
echo "The string appears to be Base64."
return 3
elif echo "$1" | grep -qE '(&|<|>|"|')'; then
echo "The string appears to be HTML encoded."
return 4
elif echo "$1" | grep -qE '(%[0-9A-Fa-f]{2})+'; then
echo "The string appears to be URL encoded."
return 5
elif echo "$1" | grep -qE '^[01 ]+$'; then
echo "The string appears to be Binary encoded."
return 6
elif echo "$1" | grep -qE '^[0-7 ]+$'; then
echo "The string appears to be Octal encoded."
return 7
else
echo "Unable to detect encoding type."
return 0
fi
}
# Get the string to decode
read -p "Enter the string to decode: " input_string
# detect the encoding type
detect_encoding "$input_string"
encoding_type=$?
# user do you want to decode?
if [ $encoding_type -ne 0 ]; then
read -p "Do you want to decode it? (yes/no) " decode_choice
if [ "$decode_choice" == "yes" ]; then
case $encoding_type in
1)
decoded=$(decode_base16 "$input_string")
;;
2)
decoded=$(decode_base32 "$input_string")
;;
3)
decoded=$(decode_base64 "$input_string")
;;
4)
decoded=$(decode_html "$input_string")
;;
5)
decoded=$(decode_url "$input_string")
;;
6)
decoded=$(decode_binary "$input_string")
;;
7)
decoded=$(decode_octal "$input_string")
;;
*)
echo "Invalid encoding type detected."
exit 1
;;
esac
if [ -z "$decoded" ]; then
echo "Decoding failed or tool not available."
else
echo "Decoded string: $decoded"
fi
else
echo "Decoding aborted."
fi
fi