2022-05-20
Bash - Encode, Decode Base64 and Hex
Tools
xxd
- make a hexdump or do the reverse.
base64
- base64 encode/decode data and print to standard output
Encode hex
echo "hello" | xxd -p
68656c6c6f0a
Decode hex
echo "68656c6c6f0a" | xxd -r -p
Encode base64
echo "hello" | base64
aGVsbG8K
Decode base64
echo "aGVsbG8K" | base64 --decode
Encode/decode ROT13
echo "Hello" | tr '[A-Za-z]' '[N-ZA-Mn-za-m]'
Caesar cipher encoding
(from: https://gist.github.com/IQAndreas/030b8e91a8d9a407caa6) The Caesar Cipher shifts plaintext three letters to the left to create ciphertext.
In this case, tr '[A-Z]' '[X-ZA-W]'
, tr translates all occurrences of 'A' to 'X', 'B' to 'Y', 'C' to 'Z', 'D' to 'A', etc.
[X-ZA-W]
just means that your output starts with the letter X and continues through the letter Z, then continues with the letter A through the letter W. You could also use [XYZABCDEFGHIJKLMNOPQRSTUVW]
instead but tr understands the hyphen '-' to mean 'through'.
echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[X-ZA-W]'
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Caesar cipher decoding
echo "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" | tr '[X-ZA-W]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG