def aes_decrypt_text(data, password, key_size_bytes):
 NONCE_LENGTH_BYTES = 8
 data = bytes_to_intlist(base64.b64decode(data))
 password = bytes_to_intlist(password.encode(u'utf-8'))
 key = (password[:key_size_bytes] + ([0] * (key_size_bytes - len(password))))
 key = (aes_encrypt(key[:BLOCK_SIZE_BYTES], key_expansion(key)) * (key_size_bytes // BLOCK_SIZE_BYTES))
 nonce = data[:NONCE_LENGTH_BYTES]
 cipher = data[NONCE_LENGTH_BYTES:]
 class Counter:
  __value = (nonce + ([0] * (BLOCK_SIZE_BYTES - NONCE_LENGTH_BYTES)))
  def next_value(self):
   temp = self.__value
   self.__value = inc(self.__value)
   return temp
 decrypted_data = aes_ctr_decrypt(cipher, key, Counter())
 plaintext = intlist_to_bytes(decrypted_data)
 return plaintext

def aes_decrypt_text ( data , password , key_size_bytes ) : NONCE_LENGTH_BYTES = 8 data = bytes_to_intlist ( base64 . b64decode ( data ) ) password = bytes_to_intlist ( password . encode ( u'utf-8' ) ) key = ( password [ : key_size_bytes ] + ( [ 0 ] * ( key_size_bytes - len ( password ) ) ) ) key = ( aes_encrypt ( key [ : BLOCK_SIZE_BYTES ] , key_expansion ( key ) ) * ( key_size_bytes // BLOCK_SIZE_BYTES ) ) nonce = data [ : NONCE_LENGTH_BYTES ] cipher = data [ NONCE_LENGTH_BYTES : ] class Counter : __value = ( nonce + ( [ 0 ] * ( BLOCK_SIZE_BYTES - NONCE_LENGTH_BYTES ) ) ) def next_value ( self ) : temp = self . __value self . __value = inc ( self . __value ) return temp decrypted_data = aes_ctr_decrypt ( cipher , key , Counter ( ) ) plaintext = intlist_to_bytes ( decrypted_data ) return plaintext
decrypt text - the first 8 bytes of decoded data are the 8 high bytes of the counter - the cipher key is retrieved by encrypting the first 16 byte of password with the first key_size_bytes bytes from password - mode of operation is counter .