Iced TEA
Last updated
Was this helpful?
Last updated
Was this helpful?
Locked within a cabin crafted entirely from ice, you're enveloped in a chilling silence. Your eyes land upon an old notebook, its pages adorned with thousands of cryptic mathematical symbols. Tasked with deciphering these enigmatic glyphs to secure your escape, you set to work, your fingers tracing each intricate curve and line with determination. As you delve deeper into the mysterious symbols, you notice that patterns appear in several pages and a glimmer of hope begins to emerge. Time is flying and the temperature is dropping, will you make it before you become one with the cabin?
from Crypto.Util.Padding import unpad
from Crypto.Util.number import bytes_to_long as b2l, long_to_bytes as l2b
from enum import Enum
class Mode(Enum):
ECB = 0x01
CBC = 0x02
class Decipher:
def __init__(self, key, iv=None):
self.BLOCK_SIZE = 64
self.KEY = [b2l(key[i:i+self.BLOCK_SIZE//16]) for i in range(0, len(key), self.BLOCK_SIZE//16)]
self.DELTA = 0x9e3779b9
self.IV = iv
if self.IV:
self.mode = Mode.CBC
else:
self.mode = Mode.ECB
def decrypt(self, ct):
blocks = [ct[i:i+self.BLOCK_SIZE//8] for i in range(0, len(ct), self.BLOCK_SIZE//8)]
pt = b''
if self.mode == Mode.ECB:
for block in blocks:
pt += self.decrypt_block(block)
elif self.mode == Mode.CBC:
X = self.IV
for block in blocks:
decrypted_block = self.decrypt_block(block)
pt += self._xor(X, decrypted_block)
X = block
return unpad(pt, self.BLOCK_SIZE//8)
def decrypt_block(self, block):
m = b2l(block)
msk = (1 << (self.BLOCK_SIZE//2)) - 1
m1 = m
m0 = (m >> (self.BLOCK_SIZE//2))
s = self.DELTA << 5
for i in range(32):
m1 -= ((m0 << 4) + self.KEY[2]) ^ (m0 + s) ^ ((m0 >> 5) + self.KEY[3])
m1 &= msk
m0 -= ((m1 << 4) + self.KEY[0]) ^ (m1 + s) ^ ((m1 >> 5) + self.KEY[1])
m0 &= msk
s -= self.DELTA
return l2b(((m0 << (self.BLOCK_SIZE//2)) + m1) & ((1 << self.BLOCK_SIZE) - 1))
# Provided ciphertext and key
KEY = bytes.fromhex('850c1413787c389e0b34437a6828a1b2')
ct = bytes.fromhex('b36c62d96d9daaa90634242e1e6c76556d020de35f7a3b248ed71351cc3f3da97d4d8fd0ebc5c06a655eb57f2b250dcb2b39c8b2000297f635ce4a44110ec66596c50624d6ab582b2fd92228a21ad9eece4729e589aba644393f57736a0b870308ff00d778214f238056b8cf5721a843')
# Decrypting
decipher = Decipher(KEY)
decrypted_msg = decipher.decrypt(ct)
print("Decrypted Message:", decrypted_msg.decode('utf-8'))
┌──(kali㉿kali)-[~/ctf/crypto/primary_knowledge]
└─$ python decrypt.py
Decrypted Message: HTB{th1s_1s_th3_t1ny_3ncryp710n_4lg0r1thm_____y0u_m1ght_h4v3_4lr34dy_s7umbl3d_up0n_1t_1f_y0u_d0_r3v3rs1ng}
HTB{th1s_1s_th3_t1ny_3ncryp710n_4lg0r1thm_____y0u_m1ght_h4v3_4lr34dy_s7umbl3d_up0n_1t_1f_y0u_d0_r3v3rs1ng}