> For the complete documentation index, see [llms.txt](https://ravins-organization.gitbook.io/ctf-writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ravins-organization.gitbook.io/ctf-writeups/2024/gryphons-ctf-2024/re/random-encoder-2.md).

# Random Encoder 2

Okay, I made the encoder more random now. Good luck!

We are given the following script to reverse

```python
import random as r
r.seed("try_this_lol_1234567890!@#$%^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM")

flag = "GCTF24{FLAG_REMOVED}"

encoded = ""
for i in flag:
    a = i.swapcase() if r.randint(1, 2) == 1 else i
    b = chr(ord(a) + r.randint(500, 600))
    encoded += b.swapcase() if r.randint(1, 2) == 1 else b
print(encoded) # Flag is ʘⱮˌɳɸȾɅȷⱤɽɉʍɿɅɚɚɳɰʱɚɤⱯʀꞮ˃ⱥɸɝʢɬʴʪȱʡȨʛʋɅʔɰƩʚ
```

First, lets try to understand this script.

#### Key Components

* Fixed seed
* The character is swapped to uppercase or lowercase based on a random choice&#x20;

  ```python
  a = i.swapcase() if r.randint(1, 2) == 1 else i
  ```
* Offset by Random Large Value

  ```python
  b = chr(ord(a) + r.randint(500, 600))
  ```
* Swapcase Again

  ```python
  encoded += b.swapcase() if r.randint(1, 2) == 1 else b
  ```

After reversing it, we get the following script

```python
import random as r

r.seed("try_this_lol_1234567890!@#$%^&*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM")
encoded = "ʘⱮˌɳɸȾɅȷⱤɽɉʍɿɅɚɚɳɰʱɚɤⱯʀꞮ˃ⱥɸɝʢɬʴʪȱʡȨʛʋɅʔɰƩʚ"

decoded = ""
for char in encoded:
    swap_case_random = r.randint(1, 2)
    add_random = r.randint(500, 600)
    swap_case_final = r.randint(1, 2)
    
    if swap_case_final == 1:
        char = char.swapcase()
    original_ord = ord(char) - add_random
    original_char = chr(original_ord)
    if swap_case_random == 1:
        original_char = original_char.swapcase()
    
    decoded += original_char

print(decoded)

#Produces GCTF24{0rD3R_m47TEr$_!n_r@NDoM_f1x3D_5E3d}
```

Thus the flag is `GCTF24{0rD3R_m47TEr$_!n_r@NDoM_f1x3D_5E3d}`
