def aes_ctr_decrypt(data, key, counter):
 expanded_key = key_expansion(key)
 block_count = int(ceil((float(len(data)) / BLOCK_SIZE_BYTES)))
 decrypted_data = []
 for i in range(block_count):
  counter_block = counter.next_value()
  block = data[(i * BLOCK_SIZE_BYTES):((i + 1) * BLOCK_SIZE_BYTES)]
  block += ([0] * (BLOCK_SIZE_BYTES - len(block)))
  cipher_counter_block = aes_encrypt(counter_block, expanded_key)
  decrypted_data += xor(block, cipher_counter_block)
 decrypted_data = decrypted_data[:len(data)]
 return decrypted_data

def aes_ctr_decrypt ( data , key , counter ) : expanded_key = key_expansion ( key ) block_count = int ( ceil ( ( float ( len ( data ) ) / BLOCK_SIZE_BYTES ) ) ) decrypted_data = [ ] for i in range ( block_count ) : counter_block = counter . next_value ( ) block = data [ ( i * BLOCK_SIZE_BYTES ) : ( ( i + 1 ) * BLOCK_SIZE_BYTES ) ] block += ( [ 0 ] * ( BLOCK_SIZE_BYTES - len ( block ) ) ) cipher_counter_block = aes_encrypt ( counter_block , expanded_key ) decrypted_data += xor ( block , cipher_counter_block ) decrypted_data = decrypted_data [ : len ( data ) ] return decrypted_data
decrypt with aes in counter mode .