> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/encoding-and-decoding/rotations/rotn.md).

# RotN

Rather the rotating 3 steps ([Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher)) or 13 steps ([ROT13](https://en.wikipedia.org/wiki/ROT13)) you can rotate any number of steps between 1 and 25 (one less than the number of characters in the English alphabet).

Due to the very low number of combinations you can easily [brute-force](https://en.wikipedia.org/wiki/Brute-force_attack) these encodings.

## Brute-force with the caesar command

The `caesar` tool is one of the tools in the [bsdgames](https://wiki.linuxquestions.org/wiki/BSD_games) package.

You can use it to brute-force encodings with this bash one-liner

```
for i in $(seq 1 25); do echo -n "$i: "; echo '<Encoded_text>' | caesar $i; done
```

## Brute-force with Python

You can also brute-force encodings with the following Python script. It assumes that the encoded text is saved in a file called `encoded.txt` and that the flag begins with `picoCTF`.

```python
#!/usr/bin/python

import string

alphabet = string.ascii_lowercase
alpha_len = len(alphabet)

def shift(cipher_text, key):
    result = ''
    for c in cipher_text:
        if c.islower():
            result += alphabet[(alphabet.index(c) + key) % alpha_len]
        elif c.isupper():
            result += alphabet[(alphabet.index(c.lower()) + key) % alpha_len].upper()
        else:
            result += c
    return result

# Read the encoded flag
with open("encoded.txt", 'r') as fh:
    enc_flag = fh.read().strip()

for i in range(1, alpha_len+1):
    plain = shift(enc_flag, i)
    if ('picoCTF' in plain):
        print("ROT-%02d: %s" % (i, plain))
```

## Resources

Brute-force attack - Wikipedia: <https://en.wikipedia.org/wiki/Brute-force_attack>

caesar - Linux manual page: <https://manpages.debian.org/testing/bsdgames/caesar.6.en.html>

Caesar cipher - Wikipedia: <https://en.wikipedia.org/wiki/Caesar_cipher>

ROT13 - Wikipedia: <https://en.wikipedia.org/wiki/ROT13>
