Search code examples
pythonpython-3.xtypespycrypto

Python 3 - Pycrypto - Read only pinned buffer


So, I'm using Pycrypto to do some AES encryption and I am now porting my Python 2.7 code to Python 3.4. I am encoutering following error which I just cannot get my head around :

TypeError : argument must be read-only pinned buffer, not bytearray.

This occurs when I am trying to encrypt the content of a variable apparently. The exact line is :

token = b"\0" * 16
final_token = cipher.encrypt(token)

(token is obviously not 000... but i'm simplifying)

I have searched the internets for a solution and found that this error occured a lot in the websocket lib in python 2.6, but the solution (using memoryview(token) instead of token) doesn't help. Can someone explain what is happening there ? I am positively lost...


Solution

  • As Pycrypto uses the encode method, it requires a s# read-only buffer. The buffer should not be resizeable, which isn't the case of bytearray. Such a buffer can be built by using the bytes function. Python 2 str was already immutable and so it was aceptable. The new line should be :

    final_token = cipher.encrypt(bytes(token))
    

    (all credits for this answer goes to @eryksun, see comments)