diff options
Diffstat (limited to 'pipermail/pycrypto/attachments/20140203/88c7c334/attachment.html')
-rw-r--r-- | pipermail/pycrypto/attachments/20140203/88c7c334/attachment.html | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/pipermail/pycrypto/attachments/20140203/88c7c334/attachment.html b/pipermail/pycrypto/attachments/20140203/88c7c334/attachment.html new file mode 100644 index 0000000..dca1de9 --- /dev/null +++ b/pipermail/pycrypto/attachments/20140203/88c7c334/attachment.html @@ -0,0 +1,104 @@ +<tt> +<div dir="ltr">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')<div><br></div><br> +<div class="gmail_extra"><div><div dir="ltr">--Jeremy</div></div><br> +<br><br><div class="gmail_quote">On Mon, Feb 3, 2014 at 5:35 AM, Dave Pawson <span dir="ltr"><<a href="mailto:dave.pawson@gmail.com" target="_blank">dave.pawson@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br> +Now resolved.<br><br> +Source plain text is created / modified in either DOS or Linux (np to add Mac)<br><br> +<br><br> +read using<br><br> +chunk = unicode(infile.read(chunksize))<br><br> +# replace newline as appropriate<br><br> +chunk=replaceNL(chunk)<br><br> +# Convert to byte string<br><br> +chunk=safe_str(chunk)<br><br> +encrypt and write to disk<br><br> +<br><br> +<br><br> +# for decrypt<br><br> +#Iterate over file to read into string<br><br> +# replace the individual bytes of the Unicode character (u2022 in my case)<br><br> +# with \n for the local machine<br><br> +retval = replaceBullet(retval)<br><br> +<br><br> +code below<br><br> +<br><br> +#<br><br> +#Swap \n for \u2022<br><br> +#<br><br> +def replaceNL(str):<br><br> + # If DOS, replace \r\x0A<br><br> + # If Unix, replace \n<br><br> + lineEnd=u'\n'<br><br> + if string.find(str,'\r\x0a'):<br><br> + lineEnd=u'\r\x0A'<br><br> + return string.replace(str,lineEnd,u'\u2022')<br><br> +<br><br> +#<br><br> +# Replace bullet by \n<br><br> +#<br><br> +def replaceBullet(bstr):<br><br> + return string.replace(bstr,u'\\u2022',u'\n')<br><br> +<br><br> +<br><br> +<br><br> +def safe_str(obj):<br><br> + """ return the byte string representation of obj """<br><br> + try:<br><br> + return str(obj)<br><br> + except UnicodeEncodeError:<br><br> + # obj is unicode<br><br> + return unicode(obj).encode('unicode_escape')<br><br> +<br><br> +HTH others, though it seems messy, it works.<br><br> +<span class="HOEnZb"><font color="#888888"><br><br> +Dave<br><br> +</font></span><div class="HOEnZb"><div class="h5"><br><br> +<br><br> +On 3 February 2014 09:39, Dave Pawson <<a href="mailto:dave.pawson@gmail.com">dave.pawson@gmail.com</a>> wrote:<br><br> +> I'm having a problem 'sharing' an encrypted file between<br><br> +> MSDOS and Linux.<br><br> +><br><br> +> so I thought I'd replace \nl in the plain text with a non ASCII<br><br> +> character prior to encryption.<br><br> +><br><br> +> encryptor = AES.new(key, AES.MODE_CBC, iv)<br><br> +> outfile.write(encryptor.encrypt(chunk))<br><br> +><br><br> +> gives me<br><br> +><br><br> +> File "/usr/lib64/python2.7/site-packages/Crypto/Cipher/blockalgo.py",<br><br> +> line 244, in encrypt<br><br> +> return self._cipher.encrypt(plaintext)<br><br> +> UnicodeEncodeError: 'ascii' codec can't encode character u'\u2022' in<br><br> +> position 34: ordinal not in range(128)<br><br> +><br><br> +><br><br> +> It would seem I can't use non-ASCII characters, at least with AES, is<br><br> +> this right ?<br><br> +><br><br> +> If not, how to address it please?<br><br> +><br><br> +><br><br> +> regards<br><br> +><br><br> +><br><br> +> --<br><br> +> Dave Pawson<br><br> +> XSLT XSL-FO FAQ.<br><br> +> Docbook FAQ.<br><br> +> <a href="http://www.dpawson.co.uk" target="_blank">http://www.dpawson.co.uk</a><br><br> +<br><br> +<br><br> +<br><br> +--<br><br> +Dave Pawson<br><br> +XSLT XSL-FO FAQ.<br><br> +Docbook FAQ.<br><br> +<a href="http://www.dpawson.co.uk" target="_blank">http://www.dpawson.co.uk</a><br><br> +_______________________________________________<br><br> +pycrypto mailing list<br><br> +<a href="mailto:pycrypto@lists.dlitz.net">pycrypto@lists.dlitz.net</a><br><br> +<a href="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto" target="_blank">http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto</a><br><br> +</div></div></blockquote></div><br></div></div><br> + +</tt> |