Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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]'
@bluefangs

This comment has been minimized.

Copy link

@bluefangs bluefangs commented Mar 20, 2018

Hey. I came across this gist while I was looking for a simple way to substitute characters. I would like to know what [X-ZA-W] in tr '[A-Z]' '[X-ZA-W]' means.

@neilraustinii

This comment has been minimized.

Copy link

@neilraustinii neilraustinii commented Mar 22, 2018

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'.

@akashchandwani

This comment has been minimized.

Copy link

@akashchandwani akashchandwani commented Apr 22, 2019

Thanks for sharing this.

However, you don't need a separate translate command for decoding. The tr '[A-Za-z]' '[N-ZA-Mn-za-m]' could encode as well as decode the text.

@mqashoo77

This comment has been minimized.

Copy link

@mqashoo77 mqashoo77 commented Aug 17, 2020

can i change the root acoording to number from me ?

@martinfidjeland

This comment has been minimized.

Copy link

@martinfidjeland martinfidjeland commented Aug 7, 2021

Thank you🤘🏼

@N1H4R

This comment has been minimized.

Copy link

@N1H4R N1H4R commented Sep 19, 2021

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'.

Thank you so much. Really helped me in understanding.

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