[pycrypto] Unicode?

Jeremy Gray jrgray at gmail.com
Mon Feb 3 04:40:14 PST 2014


I may not understand the constraints of your situation, but wouldn't
python's "universal newlines" file-open mode achieve what you want?  infile
= open(filename, 'Urb')

--Jeremy


On Mon, Feb 3, 2014 at 5:35 AM, Dave Pawson <dave.pawson at gmail.com> wrote:

> Now resolved.
> Source plain text is created / modified in either DOS or Linux (np to add
> Mac)
>
> read using
> chunk = unicode(infile.read(chunksize))
> # replace newline as appropriate
> chunk=replaceNL(chunk)
> # Convert to byte string
> chunk=safe_str(chunk)
> encrypt and write to disk
>
>
> # for decrypt
> #Iterate over file to read into string
> # replace the individual bytes of the Unicode character (u2022 in my case)
> # with \n for the local machine
> retval = replaceBullet(retval)
>
> code below
>
> #
> #Swap \n for \u2022
> #
> def replaceNL(str):
>     # If DOS, replace \r\x0A
>     # If Unix, replace \n
>     lineEnd=u'\n'
>     if string.find(str,'\r\x0a'):
>         lineEnd=u'\r\x0A'
>     return string.replace(str,lineEnd,u'\u2022')
>
> #
> # Replace bullet by \n
> #
> def replaceBullet(bstr):
>     return string.replace(bstr,u'\\u2022',u'\n')
>
>
>
> def safe_str(obj):
>     """ return the byte string representation of obj """
>     try:
>         return str(obj)
>     except UnicodeEncodeError:
>         # obj is unicode
>         return unicode(obj).encode('unicode_escape')
>
> HTH others, though it seems messy, it works.
>
> Dave
>
>
> On 3 February 2014 09:39, Dave Pawson <dave.pawson at gmail.com> wrote:
> > I'm having a problem 'sharing' an encrypted file between
> > MSDOS and Linux.
> >
> > so I thought I'd replace \nl in the plain text with a non ASCII
> > character prior to encryption.
> >
> > encryptor = AES.new(key, AES.MODE_CBC, iv)
> > outfile.write(encryptor.encrypt(chunk))
> >
> > gives me
> >
> >  File "/usr/lib64/python2.7/site-packages/Crypto/Cipher/blockalgo.py",
> > line 244, in encrypt
> >     return self._cipher.encrypt(plaintext)
> > UnicodeEncodeError: 'ascii' codec can't encode character u'\u2022' in
> > position 34: ordinal not in range(128)
> >
> >
> > It would seem I can't use non-ASCII characters, at least with AES, is
> > this right ?
> >
> > If not, how to address it please?
> >
> >
> > regards
> >
> >
> > --
> > Dave Pawson
> > XSLT XSL-FO FAQ.
> > Docbook FAQ.
> > http://www.dpawson.co.uk
>
>
>
> --
> Dave Pawson
> XSLT XSL-FO FAQ.
> Docbook FAQ.
> http://www.dpawson.co.uk
> _______________________________________________
> pycrypto mailing list
> pycrypto at lists.dlitz.net
> http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.dlitz.net/pipermail/pycrypto/attachments/20140203/88c7c334/attachment.html>


More information about the pycrypto mailing list