others-how to base64 decode and urldecode in command line?
1. Purpose
In this post , I will demo how to do base64 decode and url-decode in command line and just use one command to solve this problem.
2. Solution
For example, if we have a base64-encoded and url-encoded string:
JTdCJTBBJTIwJTIwJTIyc3ViJTIyJTNBJTIwJTIyMTIzNDU2Nzg5MCUyMiUyQyUwQSUyMCUyMCUyMm5hbWUlMjIlM0ElMjAlMjJKb2huJTIwRG9lJTIyJTBBJTdE
Do the job of base64-decode is easy:
echo "JTdCJTBBJTIwJTIwJTIyc3ViJTIyJTNBJTIwJTIyMTIzNDU2Nzg5MCUyMiUyQyUwQSUyMCUyMCUyMm5hbWUlMjIlM0ElMjAlMjJKb2huJTIwRG9lJTIyJTBBJTdE"|base64 -d
%7B%0A%20%20%22sub%22%3A%20%221234567890%22%2C%0A%20%20%22name%22%3A%20%22John%20Doe%22%0A%7D%
You can see the result is a url-encoded string, what is url-encode?
URL encoding replaces unsafe ASCII characters with a “%” followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20
We can use python to do the urldecode job:
>>> from urllib.parse import unquote
>>> url = 'example.com?title=%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D0%B2%D0%B0%D1%8F+%D0%B7%D0%B0%D1%89%D0%B8%D1%82%D0%B0'
>>> unquote(url)
'example.com?title=правовая+защита'
Then we can use the linux pipe command to combine the above commands as follows:
echo "JTdCJTBBJTIwJTIwJTIyc3ViJTIyJTNBJTIwJTIyMTIzNDU2Nzg5MCUyMiUyQyUwQSUyMCUyMCUyMm5hbWUlMjIlM0ElMjAlMjJKb2huJTIwRG9lJTIyJTBBJTdE"|base64 -d|python3 -c "import sys; from urllib.parse import unquote; print(unquote(sys.stdin.read()));"
12345678123456781234567812345678
It’s easy!
3. Summary
In this post, I demonstrated how to do url-decode and base64-decode in one command line, the keypoint is to use python as the url-decoder to decode the url-encoded string. That’s it, thanks for your reading.