← Back to Writeups
CUSTOMEasyCrypto

Coffee

XESXOR9/10/20262 min read
#crypto#custom#easy#block_cipher_reversal#tea_decryption

Coffee

Platform: spbctf | Category: Crypto | Type: Challenge | Difficulty: Easy | OS: NA | Author: XESXOR | Date: 2026-03-09 | Status: Solved Techniques: block_cipher_reversal, tea_decryption

Summary

Task: TEA-encrypted ciphertext with known key. Solution: Implement TEA decryption by reversing encryption operations (32 rounds with delta constant).

Coffee — CryptoKids 2.0

Description

Block cipher task with TEA (Tiny Encryption Algorithm) encryption using a known key.

Analysis

  • Algorithm: TEA (Tiny Encryption Algorithm)
  • Key: "spbctf rulezzzzs" (16 bytes = 4 x 32-bit words)
  • Rounds: 32 (standard TEA)
  • Delta: 0x9E3779B9 (golden ratio constant)
  • Ciphertext: 32 bytes = 4 blocks of 8 bytes each

TEA operates on 64-bit blocks (8 bytes) with a 128-bit key (16 bytes). The encryption uses Feistel-like structure with simple operations: XOR, addition, and bit shifts. ‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍

Solution

Since the key is known, we simply implement TEA decryption by reversing the encryption operations:

  1. Start with sum = delta * 32 (final sum after encryption)
  2. Reverse each round: subtract instead of add, reverse the order of operations
  3. Process each 8-byte block independently
python
#!/usr/bin/env python3
import struct

def tea_decrypt(ciphertext, key):
    """TEA decryption with 32 rounds"""
    delta = 0x9E3779B9
    
    # Parse key into 4 32-bit words
    k0, k1, k2, k3 = struct.unpack('<4I', key)
    
    plaintext = b''
    
    # Process each 8-byte block
    for i in range(0, len(ciphertext), 8):
        block = ciphertext[i:i+8]
        v0, v1 = struct.unpack('<2I', block)
        
        # Start with final sum value
        sum_val = (delta * 32) & 0xFFFFFFFF
        
        # 32 rounds of decryption (reverse order)
        for _ in range(32):
            v1 = (v1 - (((v0 << 4) + k2) ^ (v0 + sum_val) ^ ((v0 >> 5) + k3))) & 0xFFFFFFFF
            v0 = (v0 - (((v1 << 4) + k0) ^ (v1 + sum_val) ^ ((v1 >> 5) + k1))) & 0xFFFFFFFF
            sum_val = (sum_val - delta) & 0xFFFFFFFF
        
        plaintext += struct.pack('<2I', v0, v1)
    
    return plaintext
‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍

# Known key
key = b"spbctf rulezzzzs"

# Ciphertext (hex)
ciphertext = bytes.fromhex("...")  # Insert actual ciphertext

plaintext = tea_decrypt(ciphertext, key)
print(plaintext.decode())

‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍