1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| import numpy as np
def substitute(state, sub_box): return [sub_box[b & 0xF] | (sub_box[(b >> 4) & 0xF] << 4) for b in state]
def generate_round_keys(base_key, rounds): round_keys = [] temp_key = base_key for _ in range(rounds): round_keys.append(temp_key & 0xFFFFFFFF) temp_key ^= ((temp_key << 1) & 0xFFFFFFFF) | ((temp_key >> 31) & 0x1) return round_keys
def process_state(base_key, state, rounds, encrypt): sub_box = [0x9, 0x4, 0xA, 0xB, 0xD, 0x1, 0x8, 0x5, 0x6, 0x2, 0x0, 0x3, 0xC, 0xE, 0xF, 0x7] inv_sub_box = [0xA, 0x5, 0x9, 0xB, 0x1, 0x7, 0x8, 0xF, 0x6, 0x0, 0x2, 0x3, 0xC, 0x4, 0xD, 0xE]
round_keys = generate_round_keys(base_key, rounds)
if encrypt: for round in range(rounds): state = substitute(state, sub_box) state = [s ^ ((round_keys[round] >> (i * 8)) & 0xFF) for i, s in enumerate(state)] else: for round in range(rounds - 1, -1, -1): state = [s ^ ((round_keys[round] >> (i * 8)) & 0xFF) for i, s in enumerate(state)] state = substitute(state, inv_sub_box)
return state
def encrypt(plaintext, key, rounds=10): length = len(plaintext) padded_length = length if length % 4 == 0 else length + (4 - (length % 4)) plaintext += b'\x00' * (padded_length - length)
ciphertext = bytearray(padded_length) for i in range(0, padded_length, 4): state = list(plaintext[i:i+4]) state = process_state(key, state, rounds, True) ciphertext[i:i+4] = state
return ciphertext
def decrypt(ciphertext, key, rounds=10): length = len(ciphertext) plaintext = bytearray(length) for i in range(0, length, 4): state = list(ciphertext[i:i+4]) state = process_state(key, state, rounds, False) plaintext[i:i+4] = state
return plaintext.rstrip(b'\x00')
Ciphertext='A6B343D2C6BE1B268C3EA4744E3AA9914E29A0789F299022820299248C23D678442A902B4C24A8784A3EA401' Ciphertext=bytes.fromhex(Ciphertext) print(Ciphertext) for key in range(0xECB00000,0xECBFFFFF): out=decrypt(Ciphertext,key) if b'DASCTF' in out: print(key) print(out)
|