How can I decode a file where each letter has been replaced with the letter 13 letters ahead of it in the alphabet?

There's a text affected by ROT13, called rot.txt. ROT13 (rotate by 13 places) replaces a letter with the letter 13 letters after it in the alphabet. How do I write the command to view the actual text?

I tried:

cat rot.txt | tr 'A-Za-z' 'N-ZA-Mn-za-m'

but no success

1

4 Answers

Here is a way with an example

echo "hello world" | tr "$(echo -n {A..Z} {a..z} | tr -d ' ')" "$(echo -n {N..Z} {A..M} {n..z} {a..m} | tr -d ' ')"

Output:

uryyb jbeyq

Make it a function:

function rot13() { cat | tr "$(echo -n {A..Z} {a..z} | tr -d ' ')" "$(echo -n {N..Z} {A..M} {n..z} {a..m} | tr -d ' ')"
}

Usage:

➜ echo "hello world" | rot13
uryyb jbeyq
2

You can also achieve the same output by using the following piece of command:

tr '[a-z][A-Z]' '[n-za-m][N-ZA-M]'

or

tr '[a-zA-Z]' '[n-za-mN-ZA-M]'

Personally, I find this syntax a lot easier.

The easiest way to decrypt is to use the same program used to encrypt the test; ROT13:

An offset of 13 allows the encryption to be reversible. The encryption and decryption method are identical. Applying 2 consecutive encryptions (2 shifts of 13) heads to find the original text.

The link also mentions that numbers can be encrypted by shifting ASCII 5 positions and punctuation characters can be encrypted by shifting ASCII 47 positions.

Rot13 is a (very simple) algorithm to shift every character 13 places.

abcdefghijklmn # notice it ends in "n"
+1234567890123 # 13 places

That's why "a" turns to "n", "b" turns to "o", etc. 'tr' is a tool to "translate" or replace characters. So to translate them to rot13 you pass the text to translate through tr like this

echo "hello world" | tr 'A-Za-z0-9' 'N-ZA-Mn-za-m5-90-4'
# will echo "uryyb jbeyq"

Notice the 2nd set starts with "N", because "N" is A+13. To "un"rot13, (which is what you asked and no one answered) you just switch the sets

echo "uryyb jbeyq" | tr 'N-ZA-Mn-za-m5-90-4' 'A-Za-z0-9'
# will be translated back to "hello world"

An easy way to "store" this would be to create aliases

alias rot13="tr 'A-Za-z0-9' 'N-ZA-Mn-za-m5-90-4'"
alias unrot13="tr 'N-ZA-Mn-za-m5-90-4' 'A-Za-z0-9'"

So, to finaly solve your problem you'll "unencript" your file like this

cat rot.txt | unrot13

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like