In the thrilling world of Capture The Flag (CTF) challenges, I recently encountered a puzzle that involved deciphering a ROT13-encrypted message. This intriguing challenge, provided by PicoCTF, required not only problem-solving skills but also a bit of Python magic to crack the code. Here’s a glimpse into my journey of unraveling the secrets hidden behind ROT13 encryption.

A Peek into ROT13 Encryption

Before diving into the solution, let’s explore what ROT13 encryption is. It’s a simple letter substitution cipher that replaces each letter in a message with the letter 13 positions ahead or behind it in the alphabet. It’s often used for obfuscation and is a special case of the Caesar cipher, a technique developed in ancient Rome for secure communication.

The Challenge

The challenge presented me with a ROT13-encrypted message: “cvpbPGS{arkg_gvzr_V’yy_gel_2_ebhaqf_bs_ebg13_jdJBFOXJ}”. My task was to decipher it and reveal the hidden flag.

Launching VS Code from WSL:

With WSL already set up on my Windows machine, I opened the WSL terminal and used the code <pythonfile>.py command. This instantly launched VS Code within my Linux environment for seamless coding. You can find that blog here.

Python to the Rescue

To tackle this challenge, I turned to Python. I crafted a Python script that included both encryption and decryption functions, giving me the flexibility to encrypt or decrypt messages as needed.

Here’s the Python code I used:

def rot13(message):
    result = ''
    for letter in message:
        if letter.isalpha():
            if letter.isupper():
                result += chr((ord(letter) - 65 + 13) % 26 + 65)
            else:
                result += chr((ord(letter) - 97 + 13) % 26 + 97)
        else:
            result += letter
    return result

def rot13_decode(message):
    result = ''
    for letter in message:
        if letter.isalpha():
            if letter.isupper():
                result += chr((ord(letter) - 65 - 13) % 26 + 65)
            else:
                result += chr((ord(letter) - 97 - 13) % 26 + 97)
        else:
            result += letter
    return result

# The ROT13-encrypted message
encrypted_message = "cvpbPGS{arkg_gvzr_V'yy_gel_2_ebhaqf_bs_ebg13_jdJBFOXJ}"

# Decrypting the message
decoded_message = rot13_decode(encrypted_message)

# Encrypting a message if needed
message_to_be_encoded = "Hello World"
encoded_message = rot13(message_to_be_encoded)

# Printing the results
print("Decrypted Message:")
print(decoded_message) #flag picoCTF{next_time_I'll_try_2_rounds_of_rot13_wqWOSBKW}

print("\nEncoded Message:")
print(encoded_message)

# Decoding the encoded message
decoded_message2 = rot13_decode(encoded_message)
print("\nDecoded Encoded Message:")
print(decoded_message2) #Output Hello World

How the Code Works

  • The rot13 function takes a message as input and returns the ROT13-encrypted version of that message.
  • The rot13_decode function, as the name suggests, decrypts a ROT13-encrypted message.
  • I included an additional rot13_encode function to demonstrate how to encrypt a message using ROT13.
  • The provided encrypted message is decrypted, and a sample message is encrypted and then decoded to ensure the functionality works correctly.

Conclusion

This CTF challenge not only introduced me to the fascinating world of ROT13 encryption but also honed my Python skills. With this Python script in hand, I was able to conquer the challenge and unveil the hidden flag. It’s a testament to how practical coding knowledge can enhance problem-solving abilities in the realm of cybersecurity. Whether it’s encrypting or decrypting messages, Python proves to be a powerful ally in the ever-evolving landscape of digital security.