[pycrypto] PyCrypto 2.3 and unicode

Lorenz Quack don at amberfisharts.com
Mon May 16 13:54:35 CST 2011


Hi Thomas,

what about either:

blah=foo.decrypt(baz).decode("utf-8")

or

blah=unicode(foo.decrypt(baz), "utf-8")

cheers,
Lorenz

On 05/16/2011 09:18 PM, Thomas Knox wrote:
> Hello,
>
> I'm building an application in Python 2.7.1 using PyCrypto 2.3, and it's behaving badly with utf-8 and utf-16 strings.
> For example:
>
> from Crypto.Cipher import AES
> foo = AES.new('a1b2c3d4e5f6g7h8', AES.MODE_ECB)
> bar=u''
> for i in range(0,16):
>      bar+=unichr(255)
> # At this point, bar is
> # u'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
> baz=foo.encrypt(bar.encode('utf-8'))
> blah=foo.decrypt(baz)
> # baz is now
> #
> '\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf'
>
> How can I get encrypt and decrypt to use UTF gracefully?
>
> Thank you,
> Tom
>
>



More information about the pycrypto mailing list