Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Last active August 30, 2023 09:39
Show Gist options
  • Star 61 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save IQAndreas/030b8e91a8d9a407caa6 to your computer and use it in GitHub Desktop.
Save IQAndreas/030b8e91a8d9a407caa6 to your computer and use it in GitHub Desktop.
A really simple Caesar Cipher in Bash (or Shell) using `tr`, can also easily be adjusted to encrypt/decrypt ROT13 instead.
# Caesar cipher encoding
echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[X-ZA-W]'
# output: 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]'
# output: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
# Can also be adjusted to ROT13 instead
echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[N-ZA-M]'
# output: GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT
echo "GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT" | tr '[N-ZA-M]' '[A-Z]'
# output: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
# Case-sensitive version of ROT13
tr '[A-Za-z]' '[N-ZA-Mn-za-m]'
@hannestyden
Copy link

I came here from a google-search. Thanks for sharing this. However, the brackets are superfluous and the invocation can thus be tr 'A-Za-z' 'X-ZA-Wx-za-w'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment