> For the complete documentation index, see [llms.txt](https://rayyanwong.gitbook.io/rayyan/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rayyanwong.gitbook.io/rayyan/ctfs/lag-n-crash-2022-march/lnc2022-writeups/enhanced-rot13-crypto.md).

# Enhanced ROT13 ( Crypto )

Challenge: enhanced rot13

## Code given by question:

```python
lowercase_offset = ord("m") 
uppercase_offset = ord("M") 
alphabet = "abcdefghijklmnopqrxtuvwxyz"
 
## flag example is not the flag 
Flag_example = "qrrqnururqwrbtapqrnqatqqru" 
 
def b16(plain):  
 
    enc = ""  
 
    for a in plain:  
         binary = "{0:08b}".format(ord(a))  
         enc += alphabet[int(binary[4:], 2)]  
         enc += alphabet[int(binary[:4], 2)]  
    return enc  
 
def ROT13(b16):  
    result = ""  
    for i in b16:  
        c = ord(i)  
        if c >= ord('a') and c <= ord('z'):  
            if c > lowercase_offset:  
                c -= 13  
            else:  
                c += 13  
        elif c >= ord('A') and c <= ord('Z'):  
            if c > uppercase_offset:  
                c -= 13  
            else:  
                c += 13  
        result += chr(c)  
    return result  
flag=Flag_example 

b16 = b16(flag) 
print("Flag:  LNC2022{"+ROT13(b16)+ "}" )
```

## Analysis:

ROT13 can easily be reversed by ROT13-ing it again. But what about reversing b16?

As we can see, each char in enc is made out of alphabet\[num] ; where num is the converted binary to denary from an 8-bit binary ( in the order \[4:] then \[:4] )

Thus, by understanding this code, we can easily create a code to reverse whatever has been done to our flag.

```python
x1 = (ROT13("qrrqnururqwrbtapqrnqatqqru"))

output = ""
lst = []
for i in range(len(x1)):
    temp = alphabet.index(x1[i])
    lst.append( "{0:04b}".format(temp))

for i in range(1,len(lst),2):
    output += lst[i] + lst[i-1] + " "
     
print(output)
```

Here, output prints out the flag in binary form, we can just put it in a bin-text converter and get our flag from there :)
