CIPHER #001

Caesar Cipher CTF Challenge

2024-11-10Caesar CipherpicoCTF 2024

The Result

KHOOR ZRUOG
HELLO WORLD
ENCRYPTED← DRAG TO REVEAL →DECRYPTED

The Challenge

A simple Caesar cipher challenge from picoCTF. The encrypted message was provided with a hint that the shift value was 3.

The Analysis

Caesar cipher is a substitution cipher where each letter is shifted by a fixed number of positions. With a shift of 3, A becomes D, B becomes E, and so on. To decrypt, we shift backwards by the same amount.

The Solution

Using a shift of -3 (or +23), we can decrypt the message: K (-3) = H H (-3) = E O (-3) = L O (-3) = L R (-3) = O

The Code

def caesar_decrypt(text, shift):
    result = ""
    for char in text:
        if char.isalpha():
            ascii_offset = ord('A') if char.isupper() else ord('a')
            decrypted_char = chr((ord(char) - ascii_offset - shift) % 26 + ascii_offset)
            result += decrypted_char
        else:
            result += char
    return result

message = "KHOOR ZRUOG"
print(caesar_decrypt(message, 3))

The Flag

CTF{caesar_is_weak}