def c12(): # Find block size length = crypto.detect_cipher_block_size(crypto.encrypt_append_secret_ECB) print('Determined block size to be {0}.'.format(length)) # Check the cipher uses ECB mode ecb = crypto.detect_ECB(crypto.encrypt_append_secret_ECB) if ecb: print('The cipher uses ECB mode') else: raise ValueError('The cipher does not use ECB mode') # Find number of blocks to crack for the secret num_blocks = int(len(crypto.encrypt_append_secret_ECB(b'')) / length) print ('There are {0} blocks to crack'.format(num_blocks)) print('Finding the secret...') print() secret = bytes(0) for j in range(num_blocks): for i in range(length): block = {} for b in range(0, 255): plain = crypto.str_to_bytes('A' * (length - (i + 1))) + secret + b.to_bytes(1, byteorder='big') block[b] = crypto.encrypt_append_secret_ECB(plain)[j*length:(j+1)*length] match = crypto.encrypt_append_secret_ECB(b'A' * (length - (i + 1)))[j*length:(j+1)*length] byte = [k for k,v in block.items() if v == match] if not byte: # Done - or failed to find a match (i.e padding) break secret += byte[0].to_bytes(1, byteorder='big') print(secret.decode())
def c11(): cipher = crypto.encrypt_ECB_or_CBC result = crypto.detect_ECB(cipher) if result: print('Cipher used ECB') else: print('Cipher used CBC')