ctf Lesson 21 25 min read

NCL: Cryptography

Identifying and reversing encodings, classical ciphers, and weak RSA

Cryptography

NCL cryptography challenges follow a consistent pattern: you are given ciphertext or encoded data and must recover the plaintext. The process is always identify-then-decode — figure out what type of encoding or cipher was used, then apply the correct tool or technique to reverse it.

This page covers every encoding and cipher type that appears in NCL, from Base64 through RSA, with the exact tools and commands for each.


1. Encoding vs. Encryption

This distinction is fundamental and NCL tests it directly.

Encoding transforms data into a different format. Anyone can reverse it — no key needed. Think of it like writing English words using Morse code. The information is not secret; it is just in a different representation.

Encryption scrambles data using a key. Without the key, you cannot recover the original. This is actual security.

  Encoding Encryption
Key needed? No Yes
Purpose Format conversion Confidentiality
Examples Base64, hex, Morse, URL encoding Caesar, Vigenere, AES, RSA
Reversible by anyone? Yes No (without key)

Key Insight: In NCL, if the challenge says “decode” it is probably encoding. If it says “decrypt” or “crack,” it is probably encryption. The verb tells you the approach.


Recognizing What You Are Looking At

Before you can break anything, you need to identify it. This decision tree handles most NCL crypto challenges:

Is it dots and dashes?             → Morse code
Is it all hex digits (0-9, A-F)?   → Hex encoding
Does it end with = or ==?          → Base64
Is it %XX patterns?                → URL encoding
Is it & or A patterns?     → HTML entities
Is it all letters, shifted?        → Caesar cipher (try ROT13)
Is it letters with a known key?    → Vigenere cipher
Does it look zigzag-scrambled?     → Rail Fence cipher
Are there large numbers (n, e, c)? → RSA

Let’s practice. Our intercepted message was: Wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj

  • Not dots and dashes (not Morse)
  • Not hex (contains letters past F)
  • Does not end with = (not Base64)
  • All letters, with word structure preserved — this looks like a substitution cipher

Since every letter is shifted by the same amount, this is a Caesar cipher. Let’s crack it.

Quick Check: A string reads "SGVsbG8gV29ybGQ=". What encoding is this and what does it decode to?

The = at the end and the character set (A-Z, a-z, 0-9) identify this as Base64. Decode with echo "SGVsbG8gV29ybGQ=" | base64 -d to get “Hello World”.


Breaking Classical Ciphers

Caesar Cipher — The 25-Guess Attack

A Caesar cipher shifts every letter by a fixed number. With only 25 possible shifts, brute force is trivial.

Our ciphertext: Wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj

In CyberChef, apply “ROT13 Brute Force” to try all shifts. Or on the command line:

# Try shift of 3 (the most common Caesar shift)
echo "Wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj" | tr 'A-Za-z' 'X-ZA-Wx-za-w'

Output: “The quick brown fox jumps over the lazy dog” — shift of 3, the original Caesar cipher shift used by Julius Caesar himself.

The Trick: In NCL, always try shift 13 (ROT13) first — it is the most common in CTF challenges because applying it twice returns the original.

# ROT13 — the one-liner every CTF player memorizes
echo "text" | tr 'A-Za-z' 'N-ZA-Mn-za-m'

Vigenere Cipher — When One Shift Is Not Enough

Vigenere uses a keyword where each letter determines a different shift. With keyword “KEY”: K shifts by 10, E by 4, Y by 24, repeating.

If NCL gives you the key, decryption is mechanical:

def vigenere_decrypt(ciphertext, key):
    result = []
    for i, char in enumerate(ciphertext):
        if char.isalpha():
            shift = ord(key[i % len(key)].lower()) - ord('a')
            base = ord('A') if char.isupper() else ord('a')
            result.append(chr((ord(char) - base - shift) % 26 + base))
        else:
            result.append(char)
    return ''.join(result)

print(vigenere_decrypt("RIJVS", "KEY"))  # → HELLO

Or use dcode.fr — paste ciphertext and key, get plaintext instantly.

Quick Check: You are given ciphertext and told the key is "qizkwcgqbs". What cipher is this?

A key that long with the instruction to “decrypt” strongly suggests Vigenere cipher. The key length (10 characters) means the shift pattern repeats every 10 letters. Use dcode.fr or the Python function above.


RSA — The Big Numbers Challenge

RSA is the advanced NCL crypto challenge. You are given large numbers and must decrypt a message. The math sounds scary, but in CTF, RSA is always weakened — the primes are small enough to factor.

You will see parameters like:

  • n = 3233 (the public modulus)
  • e = 17 (the public exponent)
  • c = 2790 (the ciphertext)

The attack: factor n to get p and q, then compute the private key.

from sympy import factorint

n = 3233
e = 17
c = 2790

# Step 1: Factor n
factors = factorint(n)  # {61: 1, 53: 1}
p, q = list(factors.keys())
print(f"p={p}, q={q}")  # p=53, q=61

# Step 2: Compute phi(n) = (p-1)(q-1)
phi = (p - 1) * (q - 1)  # 3120

# Step 3: Compute private key d = e^(-1) mod phi
d = pow(e, -1, phi)  # 2753

# Step 4: Decrypt
plaintext = pow(c, d, n)  # 65
print(chr(plaintext))  # 'A'

For larger numbers, use factordb.com to factor n, or the automated RsaCtfTool.

Key Insight: In real RSA, n is so large (2048+ bits) that factoring is infeasible. In CTF RSA, n is deliberately small or uses weak primes. The challenge is recognizing RSA and knowing the attack steps, not doing hard math.


Why Cryptography Matters

Every secure connection you use — HTTPS, SSH, encrypted messaging — relies on the same mathematical foundations you just practiced. Caesar and Vigenere are historically important but trivially broken. RSA is still used in production, and understanding its math helps you understand why key sizes matter and what “breaking encryption” actually means.

In NCL, crypto challenges are worth 10-55 points each. The easy ones (encoding identification, Caesar, Morse) are free points with the right pattern recognition. RSA is harder but high-value. Practice the decision tree until identification is instant.


Learning Resources

Video Tutorials

Practice Platforms