def decode_byte(b1, server, b_size): decoded_byte = 0 for k in range(0, 256): b1[b_size - 1] = k c_text = server.aes_128_ecb(str(bytes(b1), 'latin1')) if detect_ecb(binascii.hexlify(c_text)): decoded_byte = k break return decoded_byte
# if padding_found: # for k in range(0, last_byte): # b_array.pop() # # return bytes(b_array).decode('ascii') if __name__ == '__main__': s = Server() # find the block size used by the server block_size = find_block_size(s) # try to detect ecb mode two_blocks = [ord('x')] * block_size * 2 ecb_detected = detect_ecb( binascii.hexlify(s.aes_128_ecb(str(bytes(two_blocks), 'latin1')))) # decrypt the unknown string if ecb_detected: match_block = [ord('x') ] * block_size # block used to match unknown bytes plain_blocks = [] i = 0 while True: # controls offset so the next unknown byte is always the last in a block offset_block = make_offset_block(i % block_size, block_size) b = match_block + offset_block
b_array = bytearray(my_encode(plaintext)) # randomly prepend 5-10 bytes random_prepend = os.urandom(1) num_prepend_bytes = 5 + random_prepend[0] % 6 # noOfPrependBytes for i in range(0, num_prepend_bytes): b_array.append(os.urandom(1)[0]) # randomly append 5-10 bytes random_append = os.urandom(1) num_append_bytes = 5 + random_append[0] % 6 # noOfAppendBytes for j in range(0, num_append_bytes): b_array.insert(0, os.urandom(1)[0]) # create modified plaintext plaintext = bytes(b_array) # encrypt if mode == 0: ciphertext = encrypt_ecb(plaintext, random_key, True) else: random_iv = os.urandom(16) ciphertext = bytes(my_encode(encrypt_cbc(plaintext, random_key, random_iv))) # ciphertext = bytes(test_encrypt(plaintext, random_key, random_iv)) # print detected mode if detect_ecb(binascii.hexlify(ciphertext)): print("ECB mode detected") else: print("CBC mode detected")