from Crypto.Util.number import long_to_bytes as l2b from Crypto.Util.number import bytes_to_long as b2l import random import os with open('/home/simplespn/flag','rb') as f: last_XOR_key = f.read(8).encode('hex') class SPN(): def __init__(self): self.BLOCK_SIZE = 8 # each block 8 bytes self.ROUNDS = 4 self.BOX_SIZE = 8 self.BLOCK_BITS = self.BLOCK_SIZE * self.BOX_SIZE self.SBox = [ 0x82, 0xf1, 0xc8, 0x61, 0xaa, 0xed, 0x62, 0xe8, 0xea, 0x38, 0x22, 0x2a, 0x9e, 0x8e, 0x81, 0x45, 0x2c, 0x37, 0xb2, 0xe5, 0xd9, 0x2b, 0xb1, 0xd4, 0x0d, 0x88, 0x86, 0xdd, 0x0f, 0x1f, 0xad, 0x26, 0x7e, 0x9a, 0x58, 0x0b, 0x74, 0xe4, 0x9c, 0xeb, 0x32, 0x33, 0xb5, 0xd2, 0xbf, 0x00, 0xd8, 0xae, 0xbd, 0x12, 0xa6, 0x5c, 0x4c, 0x75, 0xf4, 0xe0, 0x52, 0x8d, 0xf7, 0x47, 0xfb, 0x55, 0xb9, 0x79, 0x57, 0x17, 0x84, 0xd6, 0xda, 0xa5, 0xb3, 0x7f, 0xa8, 0x06, 0x97, 0x1d, 0xcb, 0x90, 0x6f, 0x03, 0x7a, 0xef, 0x28, 0x16, 0xc7, 0xe6, 0x29, 0x1b, 0x6b, 0x44, 0xc3, 0xc0, 0x6d, 0x40, 0x72, 0x09, 0xcf, 0xdc, 0x89, 0x6a, 0x70, 0x0a, 0xc6, 0x08, 0x15, 0x69, 0xfa, 0x7b, 0x50, 0x0c, 0x9b, 0xd0, 0x46, 0x9d, 0xac, 0xe2, 0xfd, 0x60, 0xdf, 0x80, 0xab, 0xf5, 0xaf, 0xd5, 0x94, 0x41, 0xa9, 0x3b, 0x02, 0x71, 0x48, 0xe1, 0x07, 0x85, 0x99, 0x68, 0xd3, 0x20, 0xa2, 0x76, 0x1e, 0x0e, 0xf2, 0x6e, 0xcd, 0x18, 0x21, 0x65, 0x59, 0x24, 0x31, 0x25, 0x7d, 0x04, 0xa4, 0x5d, 0x8f, 0x05, 0xbc, 0xff, 0xfe, 0x27, 0xd7, 0x51, 0x98, 0x64, 0x1c, 0x53, 0x23, 0xba, 0xfc, 0xa1, 0x5a, 0x43, 0xbb, 0x11, 0x34, 0x49, 0x83, 0xd1, 0x4b, 0x73, 0x63, 0x3d, 0xf8, 0x6c, 0x4e, 0x39, 0xf9, 0xee, 0xb6, 0xde, 0xf3, 0x35, 0x01, 0x2e, 0x19, 0xcc, 0x2f, 0x4a, 0x3f, 0x67, 0x8b, 0xe7, 0xbe, 0x10, 0x4f, 0x77, 0x42, 0xc9, 0x30, 0xc5, 0x91, 0x96, 0x3c, 0xe9, 0x95, 0xc4, 0xb0, 0x14, 0xce, 0x56, 0x5b, 0xa0, 0x5e, 0xc2, 0xb4, 0x5f, 0xf0, 0x8a, 0xb7, 0xec, 0x4d, 0xe3, 0x13, 0xb8, 0x3e, 0x8c, 0x92, 0xdb, 0x93, 0xa3, 0x9f, 0x66, 0x3a, 0x87, 0xa7, 0x36, 0x1a, 0xca, 0x2d, 0xf6, 0x54, 0xc1, 0x7c, 0x78 ] self.PTable = [ 0x00, 0x15, 0x0b, 0x3c, 0x21, 0x1f, 0x2b, 0x1a, 0x08, 0x1e, 0x12, 0x2d, 0x17, 0x06, 0x14, 0x3b, 0x10, 0x2c, 0x1c, 0x27, 0x04, 0x36, 0x0e, 0x0f, 0x18, 0x13, 0x11, 0x31, 0x07, 0x29, 0x33, 0x2a, 0x20, 0x2e, 0x26, 0x0d, 0x19, 0x0c, 0x01, 0x22, 0x28, 0x0a, 0x24, 0x1b, 0x3a, 0x05, 0x16, 0x09, 0x30, 0x02, 0x37, 0x3d, 0x3f, 0x32, 0x23, 0x39, 0x38, 0x34, 0x1d, 0x25, 0x35, 0x03, 0x3e, 0x2f ] with open('/home/simplespn/seed', 'rb') as f: self.keys = self.keyGen(seed = f.read(512)) def keyGen(self, seed=''): res = list() if seed != '': random.seed(seed) for i in range(self.ROUNDS): res.append( '%016x' % (random.randint(0,0xffffffffffffffff)) ) return res def checkHexEncoded(self, text): try: text.decode('hex') except: return False return True def bytes_to_bitarray(self, s, size): out = bin(b2l(s)).replace('0b','').replace('L','') if len(out) < size: out = '0'*(size - len(out)) + out assert(len(out) == size) return list([int(x) for x in out]) def bitarray_to_bytes(self, b, size): out = l2b(int(''.join(str(x) for x in b),2)) if len(out) < size: out = '\x00'*(size - len(out)) + out assert(len(out) == size) return out def permutation(self, block): return [block[x] for x in self.PTable] def substitute(self, b): assert len(b) % self.BOX_SIZE == 0 res = list() blocks = self.nsplit(b, self.BOX_SIZE) for i in range(len(blocks)): block = blocks[i] inBOX = b2l(self.bitarray_to_bytes(block, 1)) val = self.SBox[inBOX] res += self.bytes_to_bitarray(l2b(val), 8) return res def xor(self, b1, b2): assert len(b1) == len(b2) return [x^y for x,y in zip(b1,b2)] def keyXor(self, b1, b2): return self.xor(b1, self.bytes_to_bitarray(b2.decode('hex'), self.BLOCK_BITS)) def nsplit(self, block, size): return [block[k:k+size] for k in range(0, len(block), size)] def enc(self, text): if self.checkHexEncoded(text): text = text.decode('hex') else: return "Input must Hex Encoded" if len(text) % self.BLOCK_SIZE != 0: pad_len = self.BLOCK_SIZE - (len(text) % self.BLOCK_SIZE) text += pad_len * chr(pad_len) assert len(text) % self.BLOCK_SIZE == 0 out = '' blocks = self.nsplit(text, self.BLOCK_SIZE) for block in blocks: ba_block = self.bytes_to_bitarray(block, self.BLOCK_SIZE*8) for _round in range(self.ROUNDS-1): ba_block = self.keyXor(ba_block, self.keys[_round]) ba_block = self.substitute(ba_block) ba_block = self.permutation(ba_block) #last round is not permuted ba_block = self.keyXor(ba_block, self.keys[self.ROUNDS-1]) ba_block = self.substitute(ba_block) ba_block = self.keyXor(ba_block, last_XOR_key) out += '%016x' % b2l(self.bitarray_to_bytes(ba_block, 8)) return out