Using a byte array as key for AES algorithm in Python
<p>I have a byte array that is a 128 bits AES key and I want to use that one on a Python script to cipher some information using the aforementioned key.</p>

<p>I have the key stored as a hexadecimal string, something like "27821D90D240EA4F56D0E7612396C69E" (obviously this is not the real key, but has the same format).</p>

<p>I have generated a byte array from that key, that is the way I have been using AES keys in other languages (Java, C# and PHP) so far, like this:</p>

<pre><code>AES_KEY = bytearray.fromhex('27821D90D240EA4F56D0E7612396C69E')
</code></pre>

<p>That works fine, but then when I try to use it for creating the cipher, it complains that it wants an string in the first parameter:</p>

<pre><code>cipher = AES.new(AES_KEY, AES.MODE_CBC, os.urandom(16));
</code></pre>

<blockquote>
  <p>TypeError: argument 1 must be string or read-only buffer, not
  bytearray</p>
</blockquote>

<p>I have tried to get an string from the byte array instead, as:</p>

<pre><code>AES_KEY = bytearray.fromhex('27821D90D240EA4F56D0E7612396C69E').decode()
</code></pre>

<p>or </p>

<pre><code>AES_KEY = bytearray.fromhex('27821D90D240EA4F56D0E7612396C69E').decode('utf-8')
</code></pre>

<p>to no avail because there are non-ascii and non-unicode values in that key.</p>

<p>Replacing the key is NOT an option.</p>

<p>Any ideas?</p>

<p>Thanks a lot in advance,</p>